How to move HEAD back to a previous location? (Detached head)

In git, I was trying to do a squash commit by merging in another branch and then resetting HEAD to the previous place via:

git reset origin/master

But I need to step out of this. How can I move HEAD back to the previous location?

I have the SHA1 frag ( 23b6772 ) of the commit that I need to move it to.
How can i get back to this commit?


Before answering lets add some background, explaining what is this HEAD .

First of all what is HEAD?

HEAD is simply a reference to the current commit (latest) on the current branch.
There can only be a single HEAD at any given time. (excluding git worktree )

The content of HEAD is stored inside .git/HEAD and it contains the 40 bytes SHA-1 of the current commit.


detached HEAD

If you are not on the latest commit - meaning that HEAD is pointing to a prior commit in history its called detached HEAD .

在这里输入图像描述

On the command line it will look like this- SHA-1 instead of the branch name since the HEAD is not pointing to the the tip of the current branch

在这里输入图像描述


A few options on how to recover from a detached HEAD:


git checkout

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point you can create a branch and start to work from this point on.

# Checkout a given commit. 
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# create a new branch forked to the given commit
git checkout -b <branch name>

git reflog

You can always use the reflog as well.
git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

Every time the HEAD is modified there will be a new entry in the reflog

git reflog
git checkout HEAD@{...}

This will get you back to your desired commit

在这里输入图像描述


git reset HEAD --hard <commit_id>

"Move" your head back to the desired commit.

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.
  • Note: (Since Git 2.7)
    you can also use the git rebase --no-autostash as well.


  • git revert <sha-1>

    "Undo" the given commit or commit range.
    The reset command will "undo" any changes made in the given commit.
    A new commit with the undo patch will be commited while the original commit will remain in the history as well.

    # add new commit with the undo of the original one.
    # the <sha-1> can be any commit(s) or commit range
    git revert <sha-1>
    

    This schema illustrate which command does what.
    As you can see there reset && checkout modify the HEAD .

    在这里输入图像描述


    Here's an approach that may be very simple and easy to remember. Check 2 conditions and finish with 1 command. Then you're back on track.

    If

    you are in 'detached head'
    (ie type git status ; you see HEAD detached at <commit_id> )

    And

    an existing branch suits your needs
    (ie type git branch -v ; you see a branch name with a related commit messages representing the work you want to continue)

    Then

    simply check out that branch (ie type git checkout <branch_name> ; you see Switched to branch <branch_name> ).

    Outcomes

    You can now continue adding and committing your work as before; changes will be tracked on <branch_name> .

    Note that if you had saved work while HEAD was detached, in most cases that work will be merged automatically in the above process. If you see a message about a merge conflict, don't panic. There are several great tutorials with simple steps for fixing the conflict and completing the merge.


    The question can be read as:

    I was in detached-state with HEAD at 23b6772 and typed git reset origin/master (because I wanted to squash). Now I've changed my mind, how do I go back to HEAD being at 23b6772 ?

    The straight-forward answer being: git reset 23b6772

    But I hit this question because I got sick of typing (copy & pasting) commit hashes or its abbreviation each time I wanted to reference the previous HEAD and was Googling to see if there were any kind of shorthand.

    It turns out there is!

    git reset - (or in my case git cherry-pick - )

    Which incidentally was the same as cd - to return to the previous current directory in *nix! So hurrah, learned two things with one stone.

    链接地址: http://www.djcxy.com/p/404.html

    上一篇: 在Git中恢复由SHA哈希提交?

    下一篇: 如何将HEAD移回到以前的位置? (分离头)