Take the 2-minute tour ×

I've done some work on a project but then realized it wasn't the way to go. So I want to go back to an earlier, clean, revision. However, I do not want to lose the work I've done between this clean revision and today, so that I can refer to it later if I change my mind.

What would be the best way to handle this in git?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

You have two reasonable options:

  1. Use git-stash. Just type git stash to stash, or git stash save some descriptive message to give it a descriptive message. You can then reapply this later with git stash apply (or git stash pop).

  2. Make a commit, drop a tag on it, then reset back to parent. You can then retrieve your work later by accessing the tag. This would be git commit -m 'Temp work'; git tag tempWork; git reset --hard HEAD^

share|improve this answer
Since I've already committed my changes, I think I will do your second method. However, if I do a reset, won't I lose the tags that are between the reset position and today? –  this.lau_ Dec 18 '11 at 7:22
@Laurent: No. Reset doesn't touch tags, just like it doesn't touch branches (besides the checked-out one). In fact, tags and branches are really the exact same thing under the hood (they're both just named references), except git recognizes the naming pattern of tags and doesn't let you checkout out a tag without producing a detached HEAD. –  Kevin Ballard Dec 18 '11 at 8:41

This is precisely why git stash exists:

  1. git stash
  2. git checkout earlier_point
  3. git stash pop
share|improve this answer

Assuming your "bad" commits were done on master, they have NOT been shared with anyone and 13d7 represents the last "good" commit:

git checkout -b my_bad_branch
//create a new branch that contains everything we've done thusfar
git checkout master
git reset --hard 13d7
//reset the head pointer on master to point to the known good commit

Or, if they have been shared:

git checkout -b my_bad_branch
git checkout master
git revert 13d7
//create new commits which undo the bad commits after 13d7
git push
//make sure everyone gets master that fixes the bad commits

share|improve this answer

Your Answer


