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 SHA-1 fragment (23b6772
) of the commit that I need to move it to. How can I get back to this commit?
This question is related to
git
git-checkout
git-reset
git-revert
git-reflog
This may not be a technical solution, but it works. (if anyone of your teammate has the same branch in local)
Let's assume your branch name as branch-xxx.
Steps to Solve:
Note: Again, this is not a technical solution, but it will help for sure.
First reset
locally:
git reset 23b6772
To see if you're on the right position, verify with:
git status
You will see something like:
On branch master Your branch is behind 'origin/master' by 17 commits, and can be fast-forwarded.
Then rewrite history on your remote tracking branch to reflect the change:
git push --force-with-lease // a useful command @oktober mentions in comments
Using --force-with-lease
instead of --force
will raise an error if others have meanwhile committed to the remote branch, in which case you should fetch first. More info in this article.
When you run the command git checkout commit_id
then HEAD detached from 13ca5593d(say commit-id)
and branch will be on longer available.
Move back to previous location run the command step wise -
git pull origin branch_name
(say master)git checkout branch_name
git pull origin branch_name
You will be back to the previous location with an updated commit from the remote repository.
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 straightforward 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.
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, I learned two things with one stone.
Use git checkout -
You will see Switched to branch <branch_name>
. Confirm it's the branch you want.
Brief explanation: this command will move HEAD back to its last position. See note on outcomes at the end of this answer.
Mnemonic: this approach is a lot like using cd -
to return to your previously visited directory. Syntax and the applicable cases are a pretty good match (e.g. it's useful when you actually want HEAD to return to where it was).
The quick approach solves the OP's question. But what if your situation is slightly different: say you have restarted Bash then found yourself with HEAD detached. In that case, here are 2 simple, easily remembered steps.
Use git branch -v
You see a list of existing local branches. Grab the branch name that suits your needs.
Use git checkout <branch_name>
You will see Switched to branch <branch_name>
. Success!
With either method, you can now continue adding and committing your work as before: your next changes will be tracked on <branch_name>
.
Note that both git checkout -
and git checkout <branch_name>
will give additional instructions if you have committed changes while HEAD was detached.
Today, I mistakenly checked out on a commit and started working on it, making some commits on a detach HEAD state. Then I pushed to the remote branch using the following command:
git push origin HEAD: <My-remote-branch>
Then
git checkout <My-remote-branch>
Then
git pull
I finally got my all changes in my branch that I made in detach HEAD.
Source: Stackoverflow.com