Thursday, January 10, 2019

Detached HEAD in GIT and how to fix it .

No comments
What is a 'Detached HEAD' state ?

When you do a "git checkout" you determine which revision of your project you want to work on . GIT then places all the files in that revision in your working folder . We usually do a git checkout on a branch name . However, you can also checkout on a commit id . This is when the detached HEAD state comes into picture .

 $ git checkout master   // checkout on a specific branch
$ git checkout 1234defg // checkout on a specific commit id 

The HEAD pointer in GIT determines your current working revision . Normally when checking out a proper branch name , GIT automatically moves the HEAD pointer along when you create a new commit . You are automatically on the newest commit of the chosen branch. This wont happen when you checkout a commit id . The consequence is that when you make changes and commit them these changes do not belong to any branch . This means these changes can easily be lost when you checkout a different branch .

When you see that you are on a detached HEAD state , and you want to keep the changes done run the below commands :

[$ git add .  //all the files that you want to keep

$ git commit -m “<commit message>”
//at this step you will have a new commit id say 123abc

$git checkout master // i.e checkout the current branch that you want to be on

$ git merge 123abc

$git push //to your current branch i.e master

 
Please refer the below posts for more details on the detached HEAD state
1)    stackoverflow
2)    blog post

No comments :

Post a Comment