0

Setting up a git repository with some messy commits. Everything is working and all good, but I would like to remove/delete the messy commits - how do you go about doing that?

Thanks

Chris G.
  • 23,930
  • 48
  • 177
  • 302
  • 1
    could you be more specific? Say, add an example? – Nate Jul 25 '11 at 17:12
  • possible duplicate of [Git: removing selected commits from repository](http://stackoverflow.com/questions/495345/git-removing-selected-commits-from-repository) – CharlesB Jul 25 '11 at 17:57

2 Answers2

4

If you want to go through the entire history, I think the best way would be an interactive rebase

git checkout master
git tag oldmaster
git branch newbranch sha1ofRootCommit
git rebase -i newbranch

Your editor should pop up. Delete the lines of the commits you don't want. If everything goes well and there were no conflicts, master should contain your clean history.

Here is a link to a similar question

As CharlesB said you can squash commits into a single one if you still want to keep the changes by replacing pick with squash instead of deleting the line.

Community
  • 1
  • 1
Andy
  • 44,610
  • 13
  • 70
  • 69
  • 1
    This will remove commits, maybe it's not what the OP wants! Better squash them when in doubt – CharlesB Jul 25 '11 at 17:29
  • Thanks; why "git tag oldmaster"? Also I get noop in the -i ? – Chris G. Jul 25 '11 at 22:53
  • Could this be the way(on the master) "git rebase -i HEAD~10" removing all the picks.. Then I end up with just two commits? – Chris G. Jul 25 '11 at 23:11
  • The tag for oldMaster is just to keep your place. You can delete it with `git tag -d oldMaster` if you know you don't want it. – Andy Jul 26 '11 at 01:17
  • `git rebase -i HEAD~10` would let you rewrite over the top of your head, but if you mess something up, you lose your original branch. This is why my example does it in a different branch. If you mess something up, you can just go back to `oldMaster`. – Andy Jul 26 '11 at 01:40
  • you will run into problems if you try to rebase the initial commit. So don't try this unless you've reference some other answers on the site related to the initial commit. – cmcginty Jul 26 '11 at 03:12
  • Thanks - I set this as accepted - not really used stackoverflow much, so I hope this is the way. – Chris G. Jul 26 '11 at 08:02
1

Your TREE

A--B--C--D--E--HEAD

If you want to reset to commit C, say the final outcome

A--B--C--HEAD, you can do

git reset --hard <shasum of your commit C>

If you want to remove commit C only,

A--B--D--E--HEAD, you can do

git rebase --onto <shasum of B> <shasum of C>

Hope it helps

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158