ToB企服应用市场:ToB评测及商务社交产业平台

标题: 【Git】05 分离头指针 [打印本页]

作者: 小秦哥    时间: 2024-7-16 07:35
标题: 【Git】05 分离头指针


  

一、分离头指针

正常情况下,在通过git checkout下令切换分支时,在下令反面跟着的是分支名(例如master、temp等)或分支名对应commit的哈希值。
非正常情况下,git checkout切换分支时反面跟了一个非分支对应commit的哈希值,此时就会产生分离头指针题目。
例如,项目标版本汗青中有如下3次commit,其中两个分别是temp分支和master分支;此时将分支切换到第三个commit(也就是非temp、master分支)上,就会出现detached HEAD提示,即分离头指针题目。
  1. git log
  2. commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (HEAD -> temp)
  3. Author: xxx <xxx@163.com>
  4. Date:   Thu Nov 9 10:22:51 2023 +0800
  5.     add temp_test
  6. commit 01df9fd5e046f104312468746168b027f4285c5c (master)
  7. Author: xxx <xxx@163.com>
  8. Date:   Thu Nov 9 10:22:04 2023 +0800
  9.     add file1
  10. commit db2d096bf27e4e8f4ca42e8b185e973b05e186df
  11. Author: xxx <xxx@163.com>
  12. Date:   Thu Nov 9 10:21:31 2023 +0800
  13.     add readme
复制代码
切换到非分支的commit上,git就会提示当前处在detached HEAD分离头指针状态:
  1. git checkout db2d096bf27e4e
  2. Note: switching to 'db2d096bf27e4e'.
  3. You are in 'detached HEAD' state. You can look around, make experimental
  4. changes and commit them, and you can discard any commits you make in this
  5. state without impacting any branches by switching back to a branch.
  6. If you want to create a new branch to retain commits you create, you may
  7. do so (now or later) by using -c with the switch command. Example:
  8.   git switch -c <new-branch-name>
  9. Or undo this operation with:
  10.   git switch -
  11. Turn off this advice by setting config variable advice.detachedHead to false
  12. HEAD is now at db2d096 add readme
复制代码
此时HEAD指针并未在某个分支旁边,即头指针与分支分离了:
  1. git log --all
  2. commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (temp)
  3. Author: xxx <xxx@163.com>
  4. Date:   Thu Nov 9 10:22:51 2023 +0800
  5.     add temp_test
  6. commit 01df9fd5e046f104312468746168b027f4285c5c (master)
  7. Author: xxx <xxx@163.com>
  8. Date:   Thu Nov 9 10:22:04 2023 +0800
  9.     add file1
  10. commit db2d096bf27e4e8f4ca42e8b185e973b05e186df (HEAD)
  11. Author: xxx <xxx@163.com>
  12. Date:   Thu Nov 9 10:21:31 2023 +0800
  13.     add readme
复制代码
二、创建分支

当切换到某个commit时,git会提示已进入到分离头指针状态,并显示在该状态下可进行的操作:
a)可在该HEAD(commit)中进行测试、提交或取消更改,当切回到分支时不会影响到分支,但此前在此commit上做的全部操作都会丢失
b)若想保留在该commit上的变动,可通过git switch -c branch_name进行分支添加
   分离头指针指向某个commit后,若再切换回master分支或其他分支时,没有为该commit新建分支,则全部在该commit上做的操作都将丢失
  1)在commit上进行修改操作,并进行commit提交
  1. vi readme
  2. git status
  3. HEAD detached at db2d096
  4. Changes not staged for commit:
  5.   (use "git add <file>..." to update what will be committed)
  6.   (use "git restore <file>..." to discard changes in working directory)
  7.         modified:   readme
  8. no changes added to commit (use "git add" and/or "git commit -a")
  9. git add readme
  10. warning: LF will be replaced by CRLF in readme.
  11. The file will have its original line endings in your working directory
  12. git commit -m "detach modify file"
  13. [detached HEAD 69cde78] detach modify file
  14. 1 file changed, 1 insertion(+)
  15. git log
  16. commit 69cde788edb4184538f2155ea5e062f5649e8781 (HEAD)
  17. Author: xxx <xxx@163.com>
  18. Date:   Thu Nov 9 15:56:42 2023 +0800
  19.     detach modify file
  20. commit db2d096bf27e4e8f4ca42e8b185e973b05e186df
  21. Author: xxx <xxx@163.com>
  22. Date:   Thu Nov 9 10:21:31 2023 +0800
  23.     add readme
复制代码
2)当再切回到master分支时,git提示有一个commit未被连接到恣意branch分支上,可通过git branch来创建新分支。
  1. git checkout master
  2. Warning: you are leaving 1 commit behind, not connected to
  3. any of your branches:
  4.   69cde78 detach modify file
  5. If you want to keep it by creating a new branch, this may be a good time
  6. to do so with:
  7. git branch <new-branch-name> 69cde78
  8. Switched to branch 'master'
复制代码
3)在未把分离头指针对应的commit创建新branch时,git log中是看不到它的信息的。
  1. git log --all --graph          # gitk --all 可调出图形界面
  2. * commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (temp)
  3. | Author: xxx <xxx@163.com>
  4. | Date:   Thu Nov 9 10:22:51 2023 +0800
  5. |
  6. |     add temp_test
  7. |
  8. * commit 01df9fd5e046f104312468746168b027f4285c5c (HEAD -> master)
  9. | Author: xxx <xxx@163.com>
  10. | Date:   Thu Nov 9 10:22:04 2023 +0800
  11. |
  12. |     add file1
  13. |
  14. * commit db2d096bf27e4e8f4ca42e8b185e973b05e186df
  15.   Author: xxx <xxx@163.com>
  16.   Date:   Thu Nov 9 10:21:31 2023 +0800
  17.       add readme
复制代码
4)为detach区域创建分支。
  1. git branch detach_branch 69cde78   # 复制前面git提示的命令语句,添加分支名称即可
  2. git log --all --graph        # 此时版本历史中就能看到detach分支的信息
  3. * commit 69cde788edb4184538f2155ea5e062f5649e8781 (detach_branch)
  4. | Author: xxx <xxx@163.com>
  5. | Date:   Thu Nov 9 15:56:42 2023 +0800
  6. |
  7. |     detach modify file
  8. |
  9. | * commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (temp)
  10. | | Author: xxx <xxx@163.com>
  11. | | Date:   Thu Nov 9 10:22:51 2023 +0800
  12. | |
  13. | |     add temp_test
  14. | |
  15. | * commit 01df9fd5e046f104312468746168b027f4285c5c (HEAD -> master)
  16. |/  Author: xxx <xxx@163.com>
  17. |   Date:   Thu Nov 9 10:22:04 2023 +0800
  18. |
  19. |       add file1
  20. |
  21. * commit db2d096bf27e4e8f4ca42e8b185e973b05e186df
  22.   Author: xxx <xxx@163.com>
  23.   Date:   Thu Nov 9 10:21:31 2023 +0800
  24.       add readme
复制代码
5)图形化界面看版本汗青
gitk --all

三、比较commit内容

可通过git diff下令来比较两个commit之间的内容差异。
  1. git log --all --oneline
  2. 69cde78 (detach_branch) detach modify file
  3. e5d60c7 (temp) add temp_test
  4. 01df9fd (HEAD -> master) add file1
  5. db2d096 add readme
  6. git diff 69cde78 e5d60c7        # 后跟两个commit哈希值
  7. diff --git a/file1 b/file1
  8. new file mode 100644
  9. index 0000000..e69de29
  10. diff --git a/readme b/readme
  11. index 9fa5398..e69de29 100644
  12. --- a/readme
  13. +++ b/readme
  14. @@ -1 +0,0 @@
  15. -test detach
  16. diff --git a/temp_test b/temp_test
  17. new file mode 100644
  18. index 0000000..e69de29
复制代码
git diff反面也可以跟HEAD这样的指针名称,以及用^和~1来体现父类
  1. git diff HEAD HEAD^         # HEAD^ 表示HEAD指针对应commit的父commit
  2. diff --git a/file1 b/file1
  3. deleted file mode 100644
  4. index e69de29..0000000
  5. git diff HEAD HEAD^^          # HEAD^^ 父亲的父亲
  6. fatal: ambiguous argument 'HEAD^^': unknown revision or path not in the working tree.
  7. Use '--' to separate paths from revisions, like this:
  8. 'git <command> [<revision>...] -- [<file>...]'
  9. git diff HEAD HEAD~1         # HEAD~1 <==> HEAD^
  10. diff --git a/file1 b/file1
  11. deleted file mode 100644
  12. index e69de29..0000000
  13. git diff HEAD HEAD~2         # HEAD~2 <==> HEAD^^
  14. fatal: ambiguous argument 'HEAD~2': unknown revision or path not in the working tree.
  15. Use '--' to separate paths from revisions, like this:
  16. 'git <command> [<revision>...] -- [<file>...]'
复制代码
四、总结

一般情况下,我们切换分支只在已有的几个分支名称之间来回切换,但遇到切换到某个commit的情况时,即出现分离头指针题目时,要懂得该题目是如何产生的,并通过什么样的操作步骤可以去解决它,而不影响到现有分支。此外,分离头指针现象也可以便于我们进行相关的测试,在不影响现有生产的条件下。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4