【Git】05 分离头指针



一、分离头指针

正常情况下,在通过git checkout命令切换分支时,在命令后面跟着的是分支名(例如master、temp等)或分支名对应commit的哈希值。

非正常情况下,git checkout切换分支时后面跟了一个非分支对应commit的哈希值,此时就会产生分离头指针问题。

例如,项目的版本历史中有如下3次commit,其中两个分别是temp分支和master分支;此时将分支切换到第三个commit(也就是非temp、master分支)上,就会出现detached HEAD提示,即分离头指针问题。

git log
commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (HEAD -> temp)
Author: xxx <xxx@163.com>
Date:   Thu Nov 9 10:22:51 2023 +0800

    add temp_test

commit 01df9fd5e046f104312468746168b027f4285c5c (master)
Author: xxx <xxx@163.com>
Date:   Thu Nov 9 10:22:04 2023 +0800

    add file1

commit db2d096bf27e4e8f4ca42e8b185e973b05e186df
Author: xxx <xxx@163.com>
Date:   Thu Nov 9 10:21:31 2023 +0800

    add readme

切换到非分支的commit上,git就会提示当前处在detached HEAD分离头指针状态:

git checkout db2d096bf27e4e
Note: switching to 'db2d096bf27e4e'.

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 switching back to a branch.

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

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at db2d096 add readme

此时HEAD指针并未在某个分支旁边,即头指针与分支分离了:

git log --all
commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (temp)
Author: xxx <xxx@163.com>
Date:   Thu Nov 9 10:22:51 2023 +0800

    add temp_test

commit 01df9fd5e046f104312468746168b027f4285c5c (master)
Author: xxx <xxx@163.com>
Date:   Thu Nov 9 10:22:04 2023 +0800

    add file1

commit db2d096bf27e4e8f4ca42e8b185e973b05e186df (HEAD)
Author: xxx <xxx@163.com>
Date:   Thu Nov 9 10:21:31 2023 +0800

    add readme

二、创建分支

当切换到某个commit时,git会提示已进入到分离头指针状态,并显示在该状态下可进行的操作:
a)可在该HEAD(commit)中进行测试、提交或取消更改,当切回到分支时不会影响到分支,但此前在此commit上做的所有操作都会丢失
b)若想保留在该commit上的变动,可通过git switch -c branch_name进行分支添加

分离头指针指向某个commit后,若再切换回master分支或其他分支时,没有为该commit新建分支,则所有在该commit上做的操作都将丢失

1)在commit上进行修改操作,并进行commit提交

vi readme

git status
HEAD detached at db2d096
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   readme

no changes added to commit (use "git add" and/or "git commit -a")

git add readme
warning: LF will be replaced by CRLF in readme.
The file will have its original line endings in your working directory

git commit -m "detach modify file"
[detached HEAD 69cde78] detach modify file
 1 file changed, 1 insertion(+)

git log
commit 69cde788edb4184538f2155ea5e062f5649e8781 (HEAD)
Author: xxx <xxx@163.com>
Date:   Thu Nov 9 15:56:42 2023 +0800

    detach modify file

commit db2d096bf27e4e8f4ca42e8b185e973b05e186df
Author: xxx <xxx@163.com>
Date:   Thu Nov 9 10:21:31 2023 +0800

    add readme

2)当再切回到master分支时,git提示有一个commit未被连接到任意branch分支上,可通过git branch来创建新分支。

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

  69cde78 detach modify file

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> 69cde78

Switched to branch 'master'

3)在未把分离头指针对应的commit创建新branch时,git log中是看不到它的信息的。

git log --all --graph          # gitk --all 可调出图形界面
* commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (temp)
| Author: xxx <xxx@163.com>
| Date:   Thu Nov 9 10:22:51 2023 +0800
|
|     add temp_test
|
* commit 01df9fd5e046f104312468746168b027f4285c5c (HEAD -> master)
| Author: xxx <xxx@163.com>
| Date:   Thu Nov 9 10:22:04 2023 +0800
|
|     add file1
|
* commit db2d096bf27e4e8f4ca42e8b185e973b05e186df
  Author: xxx <xxx@163.com>
  Date:   Thu Nov 9 10:21:31 2023 +0800

      add readme

4)为detach区域建立分支。

git branch detach_branch 69cde78   # 复制前面git提示的命令语句,添加分支名称即可

git log --all --graph        # 此时版本历史中就能看到detach分支的信息
* commit 69cde788edb4184538f2155ea5e062f5649e8781 (detach_branch)
| Author: xxx <xxx@163.com>
| Date:   Thu Nov 9 15:56:42 2023 +0800
|
|     detach modify file
|
| * commit e5d60c7d913d427b2e2161f717dff97249dd1f9b (temp)
| | Author: xxx <xxx@163.com>
| | Date:   Thu Nov 9 10:22:51 2023 +0800
| |
| |     add temp_test
| |
| * commit 01df9fd5e046f104312468746168b027f4285c5c (HEAD -> master)
|/  Author: xxx <xxx@163.com>
|   Date:   Thu Nov 9 10:22:04 2023 +0800
|
|       add file1
|
* commit db2d096bf27e4e8f4ca42e8b185e973b05e186df
  Author: xxx <xxx@163.com>
  Date:   Thu Nov 9 10:21:31 2023 +0800

      add readme

5)图形化界面看版本历史
gitk --all
在这里插入图片描述

三、比较commit内容

可通过git diff命令来比较两个commit之间的内容差异。

git log --all --oneline
69cde78 (detach_branch) detach modify file
e5d60c7 (temp) add temp_test
01df9fd (HEAD -> master) add file1
db2d096 add readme

git diff 69cde78 e5d60c7        # 后跟两个commit哈希值
diff --git a/file1 b/file1
new file mode 100644
index 0000000..e69de29
diff --git a/readme b/readme
index 9fa5398..e69de29 100644
--- a/readme
+++ b/readme
@@ -1 +0,0 @@
-test detach
diff --git a/temp_test b/temp_test
new file mode 100644
index 0000000..e69de29

git diff后面也可以跟HEAD这样的指针名称,以及用^~1来表示父类

git diff HEAD HEAD^         # HEAD^ 表示HEAD指针对应commit的父commit
diff --git a/file1 b/file1
deleted file mode 100644
index e69de29..0000000

git diff HEAD HEAD^^          # HEAD^^ 父亲的父亲
fatal: ambiguous argument 'HEAD^^': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

git diff HEAD HEAD~1         # HEAD~1 <==> HEAD^
diff --git a/file1 b/file1
deleted file mode 100644
index e69de29..0000000

git diff HEAD HEAD~2         # HEAD~2 <==> HEAD^^
fatal: ambiguous argument 'HEAD~2': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

四、总结

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


相关推荐

  1. git 合并当前分支指定仓库的指定分支

    2024-02-05 09:22:01       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-05 09:22:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-05 09:22:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-05 09:22:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-05 09:22:01       20 阅读

热门阅读

  1. Adobe Illustrator CEP插件开发入门指南

    2024-02-05 09:22:01       32 阅读
  2. 字符串拼接+和+=的执行过程

    2024-02-05 09:22:01       34 阅读
  3. 手机常亮屏不自动灭屏

    2024-02-05 09:22:01       92 阅读
  4. 【Golang】exec.command命令日志输出示例

    2024-02-05 09:22:01       31 阅读
  5. pandas学习一

    2024-02-05 09:22:01       30 阅读
  6. el-button新的type可选值

    2024-02-05 09:22:01       30 阅读
  7. 开源文化的未来:面临的挑战与机遇(AI)

    2024-02-05 09:22:01       30 阅读
  8. 2023年度总结

    2024-02-05 09:22:01       30 阅读
  9. c#队列和栈

    2024-02-05 09:22:01       30 阅读
  10. iOS面试题

    2024-02-05 09:22:01       35 阅读