How can I move HEAD back to a previous location? (Detached head) & Undo commits

git detached head go back
git move head to previous commit
head detached at origin/master
git reset
git reset head
git revert to previous commit
git move head to latest commit
git rebase

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?

Git: How to return from 'detached HEAD' state, to it. Use git checkout <branch_name> You will see Switched to branch <branch_name> . Success! A detached HEAD is simply a HEAD containing the raw hash ID of a commit. As noted in the comments, it's generally pretty reasonable to use this for a build system, whether that's some sort of continuous integration or not: you might check out a specific commit by hash ID, or check out a tag name, but either way HEAD winds up containing the commit hash ID and is now guaranteed to be steady.


git reset 23b6772

To see if you on the right position:

git status

You will see something

On branch master Your branch is behind 'origin/master' by 17 commits, and can be fast-forwarded.

Then rewrite history on your remote to reflect the change:

git push --force-with-lease // a useful command @oktober mentions in comments

Git basics: checkout vs. reset, git checkout master -f Previous HEAD position was 50a7153d7 Merge branch '​hotfix/1.87.1' Switched to branch 'master' Your branch is up-to-  You get into a detached HEAD state because origin/lexer is a remote tracking branch, and thus read only. You want to just checkout lexer, and do your work on that. Then, when you push, origin/lexer will be brought up to date.

Quickest possible solution (just 1 step)

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

More methodical solution (2-steps, but memorable)

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.

1. Pick the branch you need

Use git branch -v

You see a list of existing local branches. Grab the branch name that suits your needs.

2. Move HEAD to it

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.

What's a "detached HEAD" in Git?, A commit holds a certain state of a directory and a pointer to its antecedent commit. If you are in detached HEAD state, git reset does only move HEAD . After resetting to a previous commit xyz789 (e.g. with git reset HEAD~2 ), you have no To move the branch pointer 'back to the front', you can't use git checkout , as it  Overview. Retinal detachment. Open pop-up dialog box. Close. Retinal detachment. Retinal detachment. Retinal detachment describes an emergency situation in which a thin layer of tissue (the retina) at the back of the eye pulls away from the layer of blood vessels that provides it with oxygen and nutrients.

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.

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, I learned two things with one stone.

What is detached HEAD state and how do I get out of it?, The HEAD pointer in Git determines your current working revision (and thereby the files that are placed in your project's working directory). might spring to mind: what about going back in time to try out an older version of your project? What happens if we want to roll back to a previous commit. Simple—we can just move the branch pointer. Git supplies the reset command to do this for us. For example, if we want to reset master to point to the commit two back from the current commit, we could use either of the following methods:

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 -

  1. git pull origin branch_name (say master)
  2. git checkout branch_name
  3. git pull origin branch_name

You will be back to the previous location with an updated commit from the remote repository.

How to move HEAD back to a previous location? (Detached head), To get out of detached HEAD state and go back to master, do git checkout master . whose last commit has a hash of bond007 , your HEAD will move to bond007 . your new branch] will create a new branch from your current HEAD position. You will see, the changes in the last commit are preserved, however, the HEAD pointer is moved back to the previous position. Still unclear, let us go through adding files in a branch, committing the changes and then doing hard and soft operations for undoing changes in the example below.

Git Commits and Detached HEAD - Katie Pennachio, 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/  A cervicogenic headache is one that begins in the neck, but a person feels it in the head. In this article, we discuss the causes, symptoms, and treatments for cervicogenic headaches.

Resetting, Checking Out & Reverting, With the command “git log” you can see all of your previous commits in the terminal. a lot of advice (and scary warnings!) on how to go back to a previous commit. Git tells me immediately that I'm in a 'detached HEAD' state and I can Or in other words, I want project-branch to move forward, but with the  Experiencing pressure in your head can be unpleasant or even debilitating. Based on the location of the pressure and pain, a doctor can start to make a diagnosis on what’s causing the problem.

Git, Compare the most common configurations of git reset, git checkout, and git revert. Walk away with The HEAD ref and master branch ref currently point to commit d. git checkout, File-level, Discard changes in the working directory For example, the following command moves the hotfix branch backwards by two commits. You can move freely even with the head detached and continue to attack after it latches onto an enemy. The head is attached by a vine, and it will return to your body if the vine is stretched too far.

  • HEAD is just a pointer to your current location (or revision to be precise). git checkout 23b6772 should do.
  • Possible duplicate of Revert Git repo to a previous commit
  • @YaroslavAdmin No it should not. Checking out a commit directly is the reason detached HEAD state happened (since remote-tracking branches can't be checked out themselves and automatically defer to the commit they point to when you try to do so like OP did) Also, sorry for the necromantic comment :-) I sort of hope the initial problem is solved already...
  • If you are not on the latest commit - meaning that HEAD is pointing to a prior commit in history its called detached HEAD unless that prior commit in history is the tip of a different branch. In my experience, you could say that you are detached if HEAD is not pointing to a commit that is also pointed to by any branch. This does not apply for tags.
  • You can be in detached HEAD and on the same time have a branch with the same commit as the HEAD of this branch. I dont understand your comment
  • I have issues with your use of inline-code markup for headings :)
  • Could not find any better way to emphasize it. feel free to edit. you are more than welcome
  • Be EXCEEDINGLY CAUTIOUS with git push --force. In many situations it will make you the least popular person on the team for a little while....
  • to add to the above note, I just came across this quote at and had to add it: "A single git push --force command can easily ruin the day for a lot of people: [186 Jenkins] repositories have their branch heads rewinded to point to older commits, and in effect the newer commits were misplaced after the bad git-push." --very unpopular developer....
  • @KayV take a look at git push --force-with-lease (Thoughtbot article:
  • Useful flag, @oktober, and a good article. Thanks for adding it here and pinging me about it.
  • Important to note that a key detail in this answer is incorrect; I've proposed an edit. In the meantime: git push --force affects your remote. It does not "Then fix the HEAD to current commit."
  • This is not working because if I do (assuming that 8acc968 is HEAD~2) git checkout 8acc968then git branch -v has MyBranch in the list below ...but then git checkout MyBranch deletes my comments.
  • Hi @amuliar - git checkout 8acc968 will check out a commit, not a branch. If MyBranch has the commits you want, try git checkout MyBranch. If it does not contain the changes in commit 8acc968, you would need to merge those changes after checking out the branch.
  • Thanks for the answer! I did git checkout to see a previous commit and wanted to get back to the latest commit. But without the latest commit hash, I was pretty much lost. This solution is perfect for my situation!