How can I switch my git repository to a particular commit

Git

Git Problem Overview


In my git repository, I made 5 commits, like below in my git log:

commit 4f8b120cdafecc5144d7cdae472c36ec80315fdc
Author: Michael 
Date:   Fri Feb 4 15:26:38 2011 -0800

commit b688d46f55db1bc304f7f689a065331fc1715079
Author: Michael
Date:   Mon Jan 31 10:37:42 2011 -0800

commit b364f9dcec3b0d52666c4f03eb5f6efb7e1e7bda
Author: Michael
Date:   Wed Jan 26 13:33:17 2011 -0800

commit 4771e26619b9acba3f059b491c6c6d70115e696c
Author: Michael 
Date:   Wed Jan 26 11:16:51 2011 -0800

commit 6e559cb951b9bfa14243b925c1972a1bd2586d59
Author: Michael 
Date:   Fri Jan 21 11:42:27 2011 -0800

How can I roll back my previous 4 commits locally in a branch? In other words, how can I create a branch without my latest 4 commits (assume I have the SHA of that commit from git log)?

Git Solutions


Solution 1 - Git

To create a new branch (locally):

  • With the commit hash (or part of it)

     git checkout -b new_branch 6e559cb
    
  • or to go back 4 commits from HEAD

     git checkout -b new_branch HEAD~4
    

Once your new branch is created (locally), you might want to replicate this change on a remote of the same name: https://stackoverflow.com/questions/4697326/how-can-i-push-my-changes-to-a-remote-branch


For discarding the last three commits, see Lunaryorn's answer below.


For moving your current branch HEAD to the specified commit without creating a new branch, see Arpiagar's answer below.

Solution 2 - Git

All the above commands create a new branch and with the latest commit being the one specified in the command, but just in case you want your current branch HEAD to move to the specified commit, below is the command:

 git checkout <commit_hash>

It detaches and point the HEAD to specified commit and saves from creating a new branch when the user just wants to view the branch state till that particular commit.


You then might want to go back to the latest commit & fix the detached HEAD:

https://stackoverflow.com/questions/10228760/fix-a-git-detached-head

Solution 3 - Git

If you want to throw the latest four commits away, use:

git reset --hard HEAD^^^^

Alternatively, you can specify the hash of a commit you want to reset to:

git reset --hard 6e559cb

Solution 4 - Git

Just checkout the commit you wants your new branch start from and create a new branch

git checkout -b newbranch 6e559cb95

Solution 5 - Git

> How can I roll back my previous 4 commits locally in a branch?

Which means, you are not creating new branch and going into detached state. New way of doing that is:

git switch --detach revison

Solution 6 - Git

you can use git switch with respective commit id

git switch <commit id>

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
QuestionmichaelView Question on Stackoverflow
Solution 1 - GitArtefactoView Answer on Stackoverflow
Solution 2 - GitarpiagarView Answer on Stackoverflow
Solution 3 - GitlunaryornView Answer on Stackoverflow
Solution 4 - GitKingCrunchView Answer on Stackoverflow
Solution 5 - GitradzimirView Answer on Stackoverflow
Solution 6 - GitAkram SheriffView Answer on Stackoverflow