Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove commits before specific commit

Tags:

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

like image 967
Era Avatar asked Jun 17 '10 15:06

Era


People also ask

How do you remove changes from a specific commit?

To undo changes associated with a specific commit, developers should use the git revert command. To undo every change that has happened since a given commit occurred, use git reset.

How do you remove a commit before the last?

In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”).


1 Answers

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

like image 195
Walter Mundt Avatar answered Sep 20 '22 19:09

Walter Mundt