views:

55

answers:

2

Hi,

is there a way to remove all commits before a specified commit and use that commit as the initial?

Regards, Erik

A: 

Depending on the size of the history could you use an interactive rebase?:

1) check out the commit you want to use.

2) git rebase -i HEAD~{THE NUMBER OF COMMITS TO GO BACK}

3) replace all the 'pick's' underneath the first on with squash and it will create a single commit containing all of the changes.

4) DONT DO THIS ON A REMOTE REPOSITORY!

Leom Burke
i know this, but that's the opposite way round. i don't need to merge commits at the end but at the front :)
Erik Aigner
+2  A: 

Let's say the new oldest commit's hash is X and we can use "oldroot" and "newroot" temporarily:

git checkout -b oldroot X
TREE=`git write-tree`
COMMIT=`echo "Killed history" | git commit-tree "$TREE"`
git checkout -b newroot "$COMMIT"
git rebase --onto newroot oldroot master
# repeat for other branches than master that should use the new initial commit
git checkout master
git branch -D oldroot
git branch -D newroot
git gc # WARNING: if everything's done right, this will actually delete your history from the repo!

That will create a 'newroot' commit with the same contents as the 'oldroot' commit, but without any parents. Then, it rebases all the other branches onto the new root, which should be in the history of all of them.

EDIT: tested and fixed; slightly later, refined a bit

Walter Mundt
Note that "Killed history" becomes the new commit message for the initial commit. If you want something else, like the original message for example, just pipe it in there.
Walter Mundt