131

I want to rollback to a previous commit, and then publish that code, then go back to the latest commit.

i.e. so my master is pointing to an older commit version just so I can pulish that version, then I want to go back to the latest commit I was one initially.

How can I do this?

8 Answers 8

113

If you want to do this and revert the master to the previous commit:

git checkout master~1            # Checkout previous commit on master
git checkout -b new_master       # Create branch for new master
git branch -D master             # Delete old master
git branch -mv new_master master # Make new_master master

Alternatively:

git reset --hard master~1        # Reset current branch to one commit ago on master
7
  • 1
    So a year later i'm looking at this and thinking this is an awful way to do it. Oh well at least it's somewhat easy to understand. Dec 13, 2013 at 1:04
  • 1
    yes this is why I upvoted, easy to understand, maybe you should have presented a link to a better way? Mar 15, 2015 at 19:33
  • 1
    I had to do the same using SourceTree. The process is similar. 1) create new branch for current master state, so you won't lose anything. 2) delete master 3) create new master branch, selecting the desired commit Feb 12, 2016 at 10:00
  • 1
    What happens to the commit we "removed" from master after this? Jan 6, 2018 at 14:54
  • 1
    This should be avoided if you have a remote repo. It causes some errors if you do, because the address of the first master differs from the new master you created recently. Aug 10, 2018 at 16:23
104

If you want to avoid force pushing, here's how to revert your repo to an older commit and preserve all intervening work:

git checkout 307a5cd        # check out the commit that you want to reset to 
git checkout -b fixy        # create a branch named fixy to do the work
git merge -s ours master    # merge master's history without changing any files
git checkout master         # switch back to master
git merge fixy              # and merge in the fixed branch
git push                    # done, no need to force push!

Done! Replace 307a5cd with whatever commit you want in your repo.

(I know the first two lines can be combined, but I think that makes it less clear what's going on)

Here it is graphically:

c1 -- c2 -- c3 -- c4 -- c2' -- c5 ...
        \              /
         '------------'

You effectively remove c3 and c4 and set your project back to c2. However, c3 and c4 are still available in your project's history if you ever want to see them again.

7
  • 7
    This worked without a hitch! If you have come down this far and are reading this -- this appears to be the cleanest way to revert master to a deeper commit.
    – ddisqq
    Feb 7, 2020 at 20:29
  • 5
    I can not find the words to explain how much I love you for this, I am greatful beyond words for getting to fix something that had me bugged for 6h now and caused me so much headache. Thank you sir!!!!!!! May 12, 2020 at 15:29
  • 10
    For anyone scratching their head over 'ours' mentioned above. Its a built-in git command.. howchoo.com/git/git-merge-conflicts-rebase-ours-theirs
    – Evolve
    Jun 23, 2021 at 4:26
  • 4
    @Blankman I suggest you make this the accepted answer it covers everything people are needing and it's safe unlike the current top answer.
    – Evolve
    Jun 23, 2021 at 4:27
  • 2
    This is the answer I was looking for! Dear reader, this is the answer you should try. Apr 5, 2022 at 16:39
74

Your question is unclear. I think what you are asking for is this:

git push -f origin $old_commit_id:master

What will this do? It will push the $old_commit_id commit to origin as the new head of origin’s master branch.

If that is what you wanted, you do not need to touch your local master branch at all.

6
  • This caused a master (non-fast-forward) failure for me. @jtdubs solution worked. Jan 30, 2013 at 19:49
  • 3
    Just pass -f to force it – though the remote repo may be configured to forbid that. I’ve updated the answer. Apr 3, 2013 at 5:40
  • doesn't work for me, the accepted answer does though
    – MobileMon
    May 18, 2015 at 12:04
  • 2
    You do appear to lose past history on remote with this approach, was fine for me. Just calling it out.
    – Shawn
    May 28, 2019 at 13:56
  • 1
    Yes. The question seemed to be how to unpublish some history, and that’s what this answer does, in the most direct way possible. Jun 8, 2019 at 2:40
51

use git reset --hard <old commit number>

it will reset the HEAD to this old commit.

additionally, you need to use git push -f origin to alter the remote repo too.

10

You can just git checkout <commit-id>, do whatever you need to do, then git checkout master to get back to the new code.

If you actually need to modify the old code to release it, then you should probably:

git checkout -b my_release <commit-id>
... prepare code for release ...
... release code ...
git checkout master
git merge my_release

Also, I can't recommend git flow enough. It makes all of this quite easy.

10

Assuming a commit graph like so:

| (A) ---------> (B) ----------> (C)
|                                 ^
|                              (master)

You want to first checkout master and create a branch that points to where master currently is:

git checkout master
git branch pointer master

Should look like this now:

| (A) ---------> (B) ----------> (C)
|                                 ^
|                       (HEAD, master, pointer)

Now that you're already on master, we'll tell the master branch to move backward one commit:

git reset master~1

Now, master should be moved back one space, but the pointer branch is still on the most recent commit :

| (A) ---------> (B) ----------> (C)
|                 ^               ^
|           (HEAD, master)    (pointer)

At this point, you can push master to a remote, or where ever, then fast forward merge it back up to the pointer branch. You can kill the pointer branch at that point :

git push origin master
git merge --ff-only pointer
git branch -D pointer

Final :

| (A) ---------> (B) ----------> (C)
|                 ^               ^
|         [ origin/master ]    (HEAD, master)
1
  • 1
    If origin/master is at C, git push origin master will fail with the tip of your current branch is behind its remote counterpart. You should also pass a -f flag. Jun 29, 2016 at 21:14
1

To move to a previous version:

git checkout <version hash>

do your work here and commit it with

git commit --amend

To go back to master:

git checkout master

3
  • 1
    That won't work. git reset --hard pointed his master branch back to the old commit, so git checkout master will do nothing.
    – jtdubs
    Dec 5, 2010 at 16:32
  • True, thanks for the correction. +1 to you, would remove my answer too but I think the commit --amend step is useful enough Dec 6, 2010 at 2:22
  • What's --amend ?
    – Shafizadeh
    Dec 31, 2016 at 18:54
0

checkout to your old commit:

git reset --hard <your old commit>

create a new branch:

git checkout -b BugfixingV1 

now merge it with the master branch and keep your changes by conflicts:

git merge -s ours master

Now our new Branch is ready to be our new master branch, so let's switch again to the master branch:

git checkout master

and now we need to merge the new branch into the master!

git merge BugfixingV1 

Finally, we'll push the the changes into the Repository:

git push

Your Answer

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

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