Thursday, August 14, 2014

Git detached HEAD

Normally I do all my git operations from command line. It might be more effort than using Eclipse/Geppetto plugin, but at least I know exactly what I am doing.

Recently I have made the terrible mistake of moving some files within the Geppetto (Eclipse) IDE.

Suddenly, the "HEAD detached" or "detached HEAD" message started appearing in my "git status":

git status
HEAD detached at 05eff15


and when I commit stuff, it sure succeeds but it's in the detached head, not in my master branch:

git commit -a -m " moved domains files"

[detached HEAD c67cdde]  moved domains files
 3 files changed, 82 insertions(+)


and when I try to push, I obviously get the message

git push

fatal: You are not currently on a branch.
To push the history leading to the current (detached HEAD)
state now, use

    git push origin HEAD:


equally, pull --rebase will fail:

git pull --rebase


From ssh://stash.acme.com:7999/pup/puppet-nesoav2
   14bba35..05eff15  master     -> origin/master
You are not currently on a branch. Please specify which
branch you want to rebase against. See git-pull(1) for details.

    git pull  


So basically I am screwed, unless I can rejoin my master branch and put in it all the commits I have done in the meantime in the detached HEAD. Bugger. I really wished git gave some BIGGER warning, since you don't really want to work normally in a detached HEAD.

I do this: first I join again my master branch:

git checkout master

Warning: you are leaving 3 commits behind, not connected to
any of your branches:

  c67cdde  moved domains files4b31fcfdd3893db49
  1ab0beb  moved domains filesrnetto@nespresso.com>
  ccdbcd0 usr files1:57:15 2014 +0200



ok this tells me that c67cdde is the hash of my last commit in detached HEAD.... let's go back to it:

git checkout c67cdde

Note: checking out 'c67cdde'.

You are in 'detached HEAD' state. 


then I create a new branch in which to commit that stuff:

git checkout -b tmp

Switched to a new branch 'tmp'




then I go back to master

git checkout master

and I merge the tmp branch

git merge tmp


Updating 05eff15..c67cdde
Fast-forward


I will confirm now that my latest commits are there in master:

git log -3

and I delete the now useless tmp branch git branch -d tmp

No comments: