当前位置:   article > 正文

解决git在detached HEAD状态下提交找不到的两个方法_you are in detached head state

you are in detached head state

刚刚帮同事解决了一个关于git head detached的问题,记录一下。
在切换分支的时候,git checkout 使用的参数是origin/分支名,而不是分支名,导致HEAD处于游离状态。

testUser@computer MINGW64 /d/Program Files (x86)/project/GitTest/local/origin (master)
$ git branch -a
* master
  remotes/origin/detach/branch
  remotes/origin/master
  remotes/origin/test

//直接检出一个远端分支,加了origin,导致HEAD指针状态游离
git checkout origin/detach/branch
Note: switching to 'origin/detach/branch'.

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 9e0601a 合并冲突,B和C都有
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

然后在不知道这个的情况下,直接写代码,然后写完以后执行提交到本地。

// 这个是修改代码的替换例子
testUser@computer MINGW64 /d/Program Files (x86)/project/GitTest/local/origin ((9e0601a...))
$ echo "modify">>modify.txt

testUser@computer MINGW64 /d/Program Files (x86)/project/GitTest/local/origin ((9e0601a...))
$ git status
HEAD detached at origin/detach/branch
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        modify.txt

nothing added to commit but untracked files present (use "git add" to track)

testUser@computer MINGW64 /d/Program Files (x86)/project/GitTest/local/origin ((9e0601a...))
$ git add modify.txt
warning: LF will be replaced by CRLF in modify.txt.
The file will have its original line endings in your working directory

testUser@computer MINGW64 /d/Program Files (x86)/project/GitTest/local/origin ((9e0601a...))
$ git status
HEAD detached at origin/detach/branch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/466951
推荐阅读
相关标签
  

闽ICP备14008679号