分离头指针情况下的注意事项

分离头指针情况下的注意事项

十月 21, 2019

分离头指针情况下的注意事项

$ git log
commit 4b6999eecb60df46af36c5409e3737e44b877dd7 (HEAD -> master)
Author: xxx <xxx@xxx.com>
Date: Sun May 19 13:50:07 2019 +0800

create folder and file

commit 6d4192be4c570363865468870d14f59efaa5521e
Author: xxx <xxx@xxx.com>
Date: Sun May 19 13:46:22 2019 +0800

create abc

commit 4d2bdddf4098e40ce57fdaf04bb862f7bea04918
Author: xxx <xxx@xxx.com>
Date: Mon Apr 29 21:36:15 2019 +0800

rename

commit d34df54beffb33cf6e4f4e97d0f0e24f2b676d51 (tag: test)
Author: xxx <xxx@xxx.com>
Date: Mon Apr 29 21:02:02 2019 +0800

学习工作区和暂存区

commit 33f7ae4c4d4da85453ed0077670e242b71d0a621
Author: xxx <xxx@xxx.com>
Date: Sun Apr 28 22:19:09 2019 +0800

Add readme
$ git checkout 6d4192 #分离头指针 本意上就是指我们现在正工作在没有分支的状态下 这个时候如果产生commit或者很多变更 假设突然有个任务 有个bug要修复 需要切换到master上去修复 这个时候切换 分离头指针地方做开发的commit没有branch挂钩 就有可能被git当作垃圾清理掉
Note: checking out '6d4192'.

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 6d4192b create abc
$ vi abc #修改abc文件
$ git status #查看当前状态 发现git会提示当前头指针没有指向任何分支 是基于6d4192b做的
HEAD detached at 6d4192b
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: abc

no changes added to commit (use "git add" and/or "git commit -a")
$ git add -u #将修改添加至暂存区
$ git commit -m 'Add hello world'
[detached HEAD 4e43c01] Add hello world
1 file changed, 1 insertion(+)
$ git log #会发现head指针没有指向任何分支 这样就叫分离头指针的状态
commit 4e43c01c8f3c76aedd986d598a00b90809a3a588 (HEAD)
Author: xxx <xxx@xxx.com>
Date: Sun May 19 16:33:15 2019 +0800

Add hello world

commit 6d4192be4c570363865468870d14f59efaa5521e
Author: xxx <xxx@xxx.com>
Date: Sun May 19 13:46:22 2019 +0800

create abc

commit 4d2bdddf4098e40ce57fdaf04bb862f7bea04918
Author: xxx <xxx@xxx.com>
Date: Mon Apr 29 21:36:15 2019 +0800

rename

commit d34df54beffb33cf6e4f4e97d0f0e24f2b676d51 (tag: test)
Author: xxx <xxx@xxx.com>
Date: Mon Apr 29 21:02:02 2019 +0800

学习工作区和暂存区

commit 33f7ae4c4d4da85453ed0077670e242b71d0a621
Author: xxx <xxx@xxx.com>
Date: Sun Apr 28 22:19:09 2019 +0800

Add readme
$ git branch -av
* (HEAD detached from 6d4192b) 4e43c01 Add hello world
master 4b6999e create folder and file
temp 543c5d8 add test
$ git checkout master #假设这个时候接到任务 必须要到master去修复问题
Warning: you are leaving 1 commit behind, not connected to
any of your branches: #git提醒你有一个commit在后边,而且没有连接你任何的分支

4e43c01 Add hello world

If you want to keep it by creating a new branch, this may be a good time
to do so with: #下边再次提醒 问你要不要为4e43c01创建一个新的分支

git branch <new-branch-name> 4e43c01

Switched to branch 'master'
$ gitk --all #如图1 会发现找不到commit记录 证明这条commit在git看来是不重要的 就很有可能被git清理掉
$ git branch fix_add 4e43c01 #假设这个时候我们发现这条commit很重要 那么按照上边的提示 创建一个分支出来
$ gitk --all #这个时候再看 就会发现已经有了这条记录 如图2

imges

img