Want to change my master to an older commit, how can I do this?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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)

Solution 7 - Git

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

Solution 8 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankmanView Question on Stackoverflow
Solution 1 - GitTyler BrockView Answer on Stackoverflow
Solution 2 - GitbronsonView Answer on Stackoverflow
Solution 3 - GitAristotle PagaltzisView Answer on Stackoverflow
Solution 4 - GitKawaiKxView Answer on Stackoverflow
Solution 5 - GitjtdubsView Answer on Stackoverflow
Solution 6 - GitC0M37View Answer on Stackoverflow
Solution 7 - GitPablo FernandezView Answer on Stackoverflow
Solution 8 - GitMustafa MadoView Answer on Stackoverflow