Last updated on May 17th, 2017 at 09:42 pm
Contents
I am writing few methods to fix git detached head with origin/master taken from stack overflow given in various answers. See for yourself which one works for you.
In my experience the problem occurs when you are at <branch_1> and you checkout origin/branch_1 or vice versa.
Method 1 :
Detached head means you are no longer on a branch, you have checked out a single commit in the history (in this case the commit previous to HEAD, i.e. HEAD^)
You only need to checkout the branch you were on, e.g.
git checkout <branch_name>
Method 2
To recover from your situation, you should create a branch that points to the commit currently pointed to by your detached HEAD:
git branch temp
_branch git checkout temp_branch
(these two commands can be abbreviated as git checkout -b temp)
This will reattach your HEAD to the new temp branch.
Next, you should compare the current commit (and its history) with the normal branch on which you expected to be working:
git log --graph --decorate --pretty=oneline --abbrev-commit master origin/master temp_branch
git diff master temp_branch git diff origin/master temp_branch
(You will probably want to experiment with the log options: add -p, leave off –pretty=… to see the whole log message, etc.)
If your new temp branch looks good, you may want to update (e.g.) master to point to it:
git branch -f master temp_branch git checkout master
(these two commands can be abbreviated as git checkout -B master temp)
You can then delete the temporary branch:
git branch -d temp_branch
Finally, you will probably want to push the reestablished history:
git push origin master
You may need to add –force to the end of this command to push if the remote branch can not be “fast-forwarded” to the new commit (i.e. you dropped, or rewrote some existing commit, or otherwise rewrote some bit of history).
Method 3
git reset --hard HEAD^
Method 4
Commit changes you want to keep. If you want to take over any of the changes you made in detached HEAD state, commit them. Like:
git commit -a -m “your commit message”
Discard changes you do not want to keep. The hard reset will discard any uncommitted changes that you made in detached HEAD state:
git reset --hard
(Without this, step 3 would fail, complaining about modified uncommitted files in the detached HEAD.)
Check out your branch. Exit detached HEAD state by checking out the branch you worked on before, for example:
git checkout master
Take over your commits. You can now take over the commits you made in detached HEAD state by cherry-picking, as shown in my answer to another question.
git reflog git cherry-pick <hash1> <hash2> <hash3> …
Method 5
To get back on a master branch, while keeping the changes, try the following commands:
git rebase HEAD master git checkout master
Method 6
If you are completely sure HEAD is the good state:
git branch -f master HEAD git checkout master
You probably can’t push to origin, since your master has diverged from origin. If you are sure no one else is using the repo, you can force-push:
git push -f
Method 7
If you have changed files you don’t want to lose, you can push them. I have committed them in the detached mode and after that I can move to a temporary branch to integrate later in master.
git commit -m "commit message"
git branch temp_branch_name git checkout master git merge temp_branch_name
Method 8
If you made some changes and then realized that you are on a detached head, there is a simple solution for that: stash -> checkout master -> stash pop:
git stash
git checkout master # Fix the detached head state git stash pop # … or for extra safety use ‘stash apply’ then later
# after fixing everything do ‘stash drop’
You will have your uncommited changes and normal “attached” HEAD, like nothing happened.
Method 9
Detached head means: (1) You are no longer on a branch, (2) You have checked out a single commit in the history
As you have no changes: you can switch to master by applying the following command
git checkout master
if you have changes that you want to keep:
In case of detached HEAD, commits work like normal, except no named branch gets updated. To get master branch updated with your committed changes, make a temporary branch where you are (this way the temporary branch will have all the committed changes you have made in the detached HEAD). After that, switch to the master branch and merge the temporary branch with the master.
git branch temp
git checkout master git merge temp