Search code examples
gitdetach

What is the reason of existance of detaching in git


I have read git documents but I can't get what is it the intention of detaching branches in git.


Solution

  • When you want to make some change but you don't want that them be part of your branch you can detach your branch. From A Visual Git Reference:

    When HEAD is detached, commits work like normal, except no named branch gets updated. (You can think of this as an anonymous branch.) enter image description here

    Once you check out something else, say master, the commit is (presumably) no longer referenced by anything else, and gets lost. Note that after the command, there is nothing referencing 2eecb. enter image description here on the other hand, you want to save this state, you can create a new named branch using git checkout -b name.