0

I have to revert to an earlier commit after my stuff has been already pushed and deployed.

I have used git checkout HEAD^

to check it out, but how to push it now? it says branch is uptodate!

Any help is greatly appreciated!"""

akmur
  • 1,465
  • 2
  • 24
  • 37
  • 1
    possible duplicate of [GIT revert to previous commit... how?](http://stackoverflow.com/questions/4114095/git-revert-to-previous-commit-how) – Stuart Golodetz Jan 13 '12 at 17:28
  • My answer on the duplicate question should help you. You'll subsequently need to push, and if you've chosen to modify history (e.g. removing a commit from history with `git reset --hard HEAD^`) you'll need to use `git push -f`, noting that it will mess with anyone else who's pulled from the public repo. – Cascabel Jan 13 '12 at 17:34

3 Answers3

3

You have two options. You can revert the erroneous commit with git revert HEAD. This will generate a new commit which undoes the previous commit. You can then push that as with any other commit.

If you want the history to show the commit never existed, you can remove the commit with git reset --hard HEAD^. (Note that this will discard any changes you made to the working directory. You can then push with the -f flag to force the commit to be removed from the remote repository. Note when doing this that others may run into problems if they have fetched the commit you remove. More importantly, if anyone has pushed anything since your commit, those commits will be lost. If you're not sure if anyone else has accessed the repo, the first options is safest.

EDIT

As Jefromi mentions, you will want to check out the branch you were working on before doing either of the above or you will cause additional problems.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
  • 2
    Note that the OP needs to `git checkout ` before reverting or resetting, since after running `git checkout HEAD^`, there is no branch checked out. – Cascabel Jan 13 '12 at 17:33
  • This answer and comment is most correct. Once you've pushed to a central repository, editing history is not a good choice. The right thing to do is revert the change and push the reverted change. That is, don't go back in time, go forward (back to the future). – Bill Door Jan 13 '12 at 18:07
2
git checkout

just checkout the old version. It doesn't really change anything. So to revert a commit you need to commit an opposite changes. Hopefully, there is a speciall command for this

git revert *commit-id*

So to revert the last commit type

git revert HEAD

But you can't revert merge commit that way. To do this you need to specify the parent to which to revert. The list of parent can be viewed by this command

git cat-file -p HEAD

Determine the number of the desired commit (the first or the second) and then run

git revert -m 1 HEAD

or

git revert -m 2 HEAD
Oleksandr Pryimak
  • 1,561
  • 9
  • 11
  • I keep getting errors like this fatal: Commit d967dab95cebf3284c0e86453353eeec5260b687 is a merge but no -m option was given. – akmur Jan 13 '12 at 17:42
  • Thanks! I really was panicking yesterday when i posted this - now with a cool mind i can see that the solution to my problems was to: keep cool, git checkout a working version of my page, copy the file(s) i needed to change, checkout my master and apply changes. yes? – akmur Jan 14 '12 at 16:48
  • @Alex Are you suggesting to revert to HEAD^1 manually? You can do this, but why? – Oleksandr Pryimak Jan 14 '12 at 19:58
  • No i'm just says, travel through commits using checkout, find the file you need, travel back to your current checkout and replace the file(s) that's causing the issue – akmur Jan 14 '12 at 20:15
0

You have to commit the changes first.

Also, if you only want to revert a single commit, you can use git revert <commit> instead of git checkout.


If you did just git checkout HEAD^, there'll be nothing to commit anyway; you will be on a "detached branch". If you did something like git checkout HEAD^ -- . then you will have to commit the staged changes before pushing.

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • If the OP used `git checkout HEAD^`, there's nothing to commit. – Michael Mior Jan 13 '12 at 17:29
  • @MichaelMior: True. I was thinking that the OP did something like `git checkout HEAD^ -- .`, but that probably isn't the case. – mipadi Jan 13 '12 at 17:34
  • yeah indeed it says there is nothing to commit – akmur Jan 13 '12 at 17:34
  • @Alex: That'll revert the HEAD's parent commit. If you want to revert to the state before the HEAD commit (i.e., the HEAD's parent), then you should do `git revert HEAD`. – mipadi Jan 13 '12 at 17:39