348

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)?

6 Answers 6

406

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: 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.

0
311

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:

Fix a Git detached head?

5
  • 2
    This would detach the head. Not convenient. May 19, 2016 at 19:27
  • even if the <commit_hash> is in different branch which is ahead ? May 23, 2018 at 8:35
  • 3
    The linked question just says to git checkout master, alongside one or two edge cases that may apply to certain situations.
    – i336_
    Jun 26, 2019 at 10:03
  • how to again apply a specific commit hash when the branch is in a detached HEAD state. Jun 5 at 17:02
  • I was looking for this.
    – bit
    Jun 18 at 13:49
115

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
5
  • 10
    This leaves out the branching step. If he runs exactly what is shown here, he will permanently lose those top commits.
    – Jimmy
    Feb 9, 2011 at 0:19
  • 4
    Well, not necessarily permanently - one could get the SHA of the previous head via git reflog and reset back to it - but it would be throwing them away, yes.
    – Amber
    Feb 9, 2011 at 0:21
  • 2
    If the commits are pushed first then reset, they can be retrieved again with just a pull.
    – Rick Smith
    Mar 19, 2018 at 5:02
  • I would recommend to switch to a a new branch first before running this command because this will remove all you recent changes and move your branch to a specific commit in the past Nov 26, 2020 at 6:55
  • it will delete your all-headed commits, I think the best is to create a new branch at particular commit. Jul 1, 2021 at 4:09
28

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

1
  • 1
    This is what I was looking for. checkout is an operation which has too many different capabilities.
    – Smiley1000
    Jan 10, 2022 at 10:15
22

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

git checkout -b newbranch 6e559cb95
12

With the new git switch command, we can either:

  • git switch -c <new-branch> <commit> to create a new branch named <new-branch> starting at <commit>
  • git switch --detach <commit> to switch to a commit for inspection and discardable experiments. See DETACHED HEAD for details.

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.