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?
Do
To see if you on the right position:
You will see something
Then fix the HEAD to current 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. (excludinggit 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 calleddetached 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 branchA few options on how to recover from a detached HEAD:
git checkout
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.
git reflog
You can always use the
reflog
as well.git reflog
will display any change which updated theHEAD
and checking out the desired reflog entry will set theHEAD
back to this commit.Every time the HEAD is modified there will be a new entry in the
reflog
This will get you back to your desired commit
git reset --hard <commit_id>
"Move" your HEAD back to the desired commit.
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.
This schema illustrate which command does what.
As you can see there
reset && checkout
modify theHEAD
.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'
(i.e. type
git status
; you seeHEAD detached at <commit_id>
)And
an existing branch suits your needs
(i.e. 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 (i.e. type
git checkout <branch_name>
; you seeSwitched 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
at23b6772
and typedgit reset origin/master
(because I wanted to squash). Now I've changed my mind, how do I go back toHEAD
being at23b6772
?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 casegit 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.