tags:

views:

64

answers:

3

Hi, on Git, say I mess up my commits, and I want to make the version 3 commits ago as the new version. If I do "git checkout xxxx", it creates a new branch and it seems like I can only merge it? Could I make this the new "master version"?

I want:

A-B-C-D-E

to become

A-B-C-D-E-F

where F has exactly the same content as C

If I use "git revert xxxx" instead, it seems like it definitely will have conflicts and I need to manually resolve it. What I really want is just make the old commit at some point the new commit, irregardless of what's in my working directory or the latest commit. How would I go about doing this? Thank you.

+2  A: 

It sounds like you just want to reset to C; that is make the tree:

A-B-C

You can do that with reset:

git reset --hard HEAD~3

(Note: You said three commits ago so that's what I wrote; in your example C is only two commits ago, so you might want to use HEAD~2)


You can also use revert if you want, although as far as I know you need to do the reverts one at a time:

git revert HEAD     # Reverts E
git revert HEAD~2   # Reverts D

That will create a new commit F that's the same contents as D, and G that's the same contents as C. You can rebase to squash those together if you want

Michael Mrozek
Hi thanks for the answer. But what if I don't want to do the hard reset since it's already pushed onto a public repository?
huggie
@huggie Ah. Then you probably want to use the revert method
Michael Mrozek
A: 
git rm -r .
git checkout HEAD~3 .
git commit

After the commit, files in the new HEAD will be the same as they were in the revision HEAD~3.

svick
This seem to do what I want. Thanks. If I want to refer to the version by SHA-1 hash, should I be doing git checkout HEAD~XXXXX?
huggie
No, just `git checkout XXXXX`. `rev~n` means *n* revisions before `rev`.
svick
+1  A: 

The other answers so far create new commits that undo what is in older commits. It is possible to go back and "change history" as it were, but this can be a bit dangerous. You should only do this if the commit you're changing has not been pushed to other repositories.

The command you're looking for is git rebase --interactive

If you want to change HEAD~3, the command you want to issue is git rebase --interactive HEAD~4. This will open a text editor and allow you to specify which commits you want to change.

Practice on a different repository before you try this with something important. The man pages should give you all the rest of the information you need.

haydenmuhl