tags:

views:

801

answers:

3

I'm not sure why this doesn't make sense to me, I'm not clear on how git revert works, for example I want to revert to a commit (like 6 commits behind the head).

Say it's sha hash is: 56e05fced214c44a37759efa2dfc25a65d8ae98d

Why can't I just do something like:

git revert 56e05fced214c44a37759efa2dfc25a65d8ae98d
+8  A: 

It reverts the said commit, that is adds commit opposite to it. If you want to checkout earlier revision you do the

git checkout 56e05fced214c44a37759efa2dfc25a65d8ae98d
Michael Krelin - hacker
then I can just merge this with the head? What if I anticipate having TONS of conflicts, can I just force this commit to be the head "as-is" and just overwrite any conflicts?
Joseph Silvashy
also btw... you got it.
Joseph Silvashy
I'm not sure what head you're talking about. You can just *move* your head back to this commit. (for instance by deleting and creating branch). If you want to do a "merge" commit into the head, which is effectively the reversal of the intermediate commits, you can use merge with "ours" strategy. Pick your option and read manpages. The power is waiting for you to use it ;-)
Michael Krelin - hacker
That makes sense, the reason I ask is that git now tells me that I'm not on any branch.
Joseph Silvashy
because you aren't. if you type `git branch` you will clearly see it. You can do for instance `git checkout -b mybranch 56e05` to get it with branch.
Michael Krelin - hacker
+2  A: 

If you want to commit on top of the current HEAD with the exact state at a different commit, undoing all the intermediate commits, then you can use reset to create the correct state of the index to make the commit.

# reset the index to the desired tree
git reset 56e05fced

# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}

git commit -m "Revert to 56e05fced"

# Update working copy to reflect the new commit
git reset --hard
Charles Bailey
+3  A: 

What git-revert does is create a commit which undoes changes made in given commit, creating a commit which is reverse (well, reciprocal) of a given commit. Therefore git revert <SHA-1> should and does work.

If you want to rewind back to specified commit, and you can do this because this part of history was not yet published, what you need to use is git-reset, not git-revert: git reset --hard <SHA-1> (note that -hard would make you loose any not comitted changes in working directory).


By the way, perhaps it is not obvious, but everywhere where documentation says <commit> or <commit-ish> (or <object>) you can put SHA-1 identifier (full or shortened) of commit.

Jakub Narębski
With SO, last is best.
mitjak