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!"""
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!"""
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.
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
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.