tags:

views:

186

answers:

2

So I've done many google searches, read through the man pages, and even asked in git's irc chat room but I still don't know how I could go about rolling back to a specific commit in git. The best answer someone could give me was to use git revert x times until i reach the desired commit. So let's say I want to revert back to a commit that's 20 commits old I'd have to run it 20 times. Is there an easier way to do this? I can't use reset cause this repo is public.

+2  A: 

Well, I guess the question is, what do you mean by 'roll back'? If you can't reset because it's public and you want to keep the commit history intact, do you mean you just want your working copy to reflect a specific commit? Use git checkout and the commit hash.

Edit: As was pointed out in the comments, using git checkout without specifying a branch will leave you in a "no branch" state. Use git checkout <commit> -b <branchname> to checkout into a branch, or git checkout <commit> . to checkout into the current branch.

Ben
Doesn't this put you into the weird 'Not currently on any branch' state? How do you commit changes to complete the rollback?
Alex Reisner
Well, I'm just suggesting the use of `git checkout` -- he's free to check out into any branch (current or new) that he wishes. I'll update my answer so it's not ambiguous.
Ben
I tried this but I don't think this is the proper way to do it cause it leaves stagnate files. This doesn't delete files that weren't in that last commit.
David Salazar
If you're in a working directory and you're staying in master, you need `git reset` to delete those files, which you say you don't want to do. Try doing it into a separate branch: `git checkout <commit> -b <branchname>`, you won't have stagnant files *in that branch*.
Ben
that's definitely one solution. What about if someone were to write a bash script that would accept would run reverts until the desired hash is reached.
David Salazar
+1  A: 

Try this:

git checkout [revision] .

Don't forget the . at the end--very important. This will apply changes to the whole tree. Then commit and you should be good.

Alex Reisner