547

My repo has 100 commits in it right now. I need to rollback the repository to commit 80, and remove all the subsequent ones.

Why?

This repo is supposed to be for merging from miscellaneous users. A bunch of merges went in as commits from me, due to excessive editing. That was due to a mislabeling of my remote branches, where 3 developers were labeled as each other. I need to reset to that point, and then pull forwards.

I wanted to rebase, as in this example: How can I remove a commit on GitHub?

However, git wants me to do a lot of conflict management. Is there a simpler way?

0

7 Answers 7

1130
git reset --hard <old-commit-id>
git push -f <remote-name> <branch-name>

Note: As written in comments below, Using this is dangerous in a collaborative environment: you're rewriting history

10
  • 65
    people, do git push -f origin branch. I just had a bad time, because I missed that. Commented Mar 26, 2014 at 18:59
  • 31
    BEWARE of this ! you will lose all your commits locally, and if you push there will be no way back
    – Thomas
    Commented Apr 14, 2014 at 20:35
  • 10
    That is not true, you can get the old commits using git reflog Commented Feb 8, 2015 at 18:28
  • 34
    you may need to do git push origin HEAD --force instead of git push -f origin branch as that was giving me an error: src refspec branch does not match any. error
    – sprocket12
    Commented Feb 18, 2015 at 20:38
  • 2
    Yep, thank you guys. I managed to do what I wanted. I first created a separate branch from the one ahead of the commit I wanted to rollback. Cloned it locally. Then applied your recommendations on that copy, this way, I did not compromised my main branch... Commented Jun 1, 2015 at 22:52
46

To undo the most recent commit I do this:

First:

git log

get the very latest SHA id to undo.

git revert SHA

That will create a new commit that does the exact opposite of your commit. Then you can push this new commit to bring your app to the state it was before, and your git history will show these changes accordingly.

This is good for an immediate redo of something you just committed, which I find is more often the case for me.

As Mike metioned, you can also do this:

git revert HEAD
4
  • 11
    git revert HEAD will revert the last commit without having to look up the hash. Commented Jul 1, 2016 at 16:19
  • This is a solution if you don't want to delete commits at all. However, in this case, all the garbage commits will still be there, and allow a cloner to reset or checkout to them. If you want to prevent that, you'll HAVE to force-push.
    – cst1992
    Commented Nov 5, 2016 at 7:06
  • 1
    Note: These reverts can stack, meaning if you accidentally revert you can revert the revert with another revert.
    – Ginger
    Commented Mar 6, 2021 at 12:09
  • The accepted answer is fantastic, but I think this answer needs to be seen more b/c there is a difference between RESET and REVERT that may be very important for people if they are working in collaborative environments. Here is a good explanation: stackoverflow.com/questions/8358035/…
    – jcodes
    Commented Oct 18, 2021 at 17:18
19

Another way:

Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree, I right-clicked on the and selected "Reset BRANCHNAME to this commit".

Then navigate to your repository's local directory and run this command:

git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f -- tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME 

This will erase all commits after the current one in your local repository but only for that one branch.

2
  • 1
    FYI "REPOSITORY_NAME" will be something like https:/[email protected]/me/repo_name.git
    – tim
    Commented Feb 2, 2016 at 17:17
  • 4
    It'd be more helpful if you would break down that command and explain it.
    – cst1992
    Commented Nov 5, 2016 at 7:04
11

Most suggestions are assuming that you need to somehow destroy the last 20 commits, which is why it means "rewriting history", but you don't have to.

Just create a new branch from the commit #80 and work on that branch going forward. The other 20 commits will stay on the old orphaned branch.

If you absolutely want your new branch to have the same name, remember that branch are basically just labels. Just rename your old branch to something else, then create the new branch at commit #80 with the name you want.

2
  • 1
    I'm guessing this also problematically messes with everyone's local versions of the history of the branch of interest. Commented May 18, 2020 at 23:24
  • if such big rollback is required, then certainly everyone's local versions need to pull the latest (prepared by the first person) again . It would be safer in case you want to "rollback the rollback".
    – F. Kam
    Commented Jun 1, 2021 at 15:07
5

When doing branch updates from master, I notice that I sometimes over-click, and cause the branch to merge into the master, too. Found a way to undo that.

If your last commit was a merge, a little more love is needed:

git revert -m 1 HEAD

1
  • 1
    Worked nicely for me when I was reverting the commit on remote repo.
    – drmaa
    Commented Jan 7, 2021 at 9:38
3

In github, the easy way is to delete the remote branch in the github UI, under branches tab. You have to make sure remove following settings to make the branch deletable:

  1. Not a default branch
  2. No opening poll requests.
  3. The branch is not protected.

Now recreate it in your local repository to point to the previous commit point. and add it back to remote repo.

git checkout -b master 734c2b9b   # replace with your commit point

Then push the local branch to remote

git push -u origin master

Add back the default branch and branch protection, etc.

3
git log

Switching branches: (Note 1)

git checkout branch_name

Local fallback:

git reset --soft commitID  

This command only removes the commit log after the specified commitID, while keeping the changes made to the code.

Or

git reset --hard commitID  

This command completely removes the changes made after the specified commitID, effectively reverting the code back to its previous state.

Synchronize the remote repository: (note 2)

git push origin HEAD --force

*(Note 1) Git prompts you to commit or stash your changes before switching branches. If you attempt to switch to another branch without committing your changes, you will be prompted with the message "Please commit your changes or stash them before you switch branches". However, if you haven't finished developing the features of your current branch yet, committing may feel incomplete. In this case, you can use the "git stash" command to hide the modified but uncommitted code. This will revert the code to the state of the last commit without showing the modified code. When you return to the branch you were working on, you can use the "git stash pop" command to restore the hidden code.

(Note 2) If you encounter the error "fatal: unable to access 'https://github.com/xxxxx/xxxxxxx.git/': OpenSSL SSL_read: Connection was reset, errno 10054", it may be due to the server's SSL certificate not being signed by a third-party. To resolve this issue, you can disable SSL verification by typing git config --global http.sslVerify "false" in Git Bash before attempting the operation again.*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.