Separating head pointer

background knowledge

detached HEAD state, separation of the head pointer, i.e. the pointer points directly submit HEAD recorded
normally, should point to a branch HEAD
If execution git checkout tag名or git checkout 远端分支名or git checkout 提交记录哈希值, the HEAD points to point to a commit record, this is detached HEAD state

experiment

First with git log and graphical software git log commit record check test tree warehouse before the start of the experiment

$ git log
commit 314b5494eb06a9963f4e6f97075f2354c8a15f13 (HEAD -> master)
Author: MilesGO <[email protected]>
Date:   Sun Jun 9 17:20:40 2019 +0800

    add main2.cpp

commit 12c2c80788dcd74a1cd739157c0a1011514d36b2
Author: MilesGO <[email protected]>
Date:   Sun Jun 9 17:11:48 2019 +0800

    add src/main.cpp

commit 4a1d75425a4cd5c3e206a4c69415c59802175039
Author: MilesGO <[email protected]>
Date:   Sun Jun 9 17:00:08 2019 +0800

    modify README

commit 29c07152991c7b3ee41f9cb5ac1eff1f26610665
Author: MilesGO <[email protected]>
Date:   Sun Jun 9 16:52:41 2019 +0800

    add README

After the git-fork with a view, you can see whole grain submit records tree is linear
we detected directly hash value to the penultimate submitted

$ git checkout 12c2c8
Note: checking out '12c2c8'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 12c2c80 add src/main.cpp

Translate the above, you are in a state separated from the head pointer, you can do some experimental changes and submit them, but these submissions will be discarded unless you use a branch point to them
under the current file this branch has not main2.cpp we add this file

$ echo 'hello world again' >> main2.cpp
$ git add main2.cpp
$ git commit -m 'add main2.cpp again'
[detached HEAD 11ed7f1] add main2.cpp again
 1 file changed, 1 insertion(+)
 create mode 100644 main2.cpp

$ git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  11ed7f1 add main2.cpp again

If you want to keep it by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 11ed7f1

Switched to branch 'master'

Translate the above, you take an unused branch points to submit records to fall out, its hash value 11ed7f1, if you want to keep this submission, please use git branch <new-branch-name> 11ed7f1to create a new branch instruction and points to the submit records

$ git branch -av
* master 314b549 add main2.cpp

git branch pick 11ed7f1

$ git branch -av
* master 314b549 add main2.cpp
  pick   11ed7f1 add main2.cpp again
  

Guess you like

Origin www.cnblogs.com/milesgo517/p/10994171.html