刚刚帮同事解决了一个关于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都有
然后在不知道这个的情况下,直接写代码,然后写完以后执行提交到本地。
// 这个是修改代码的替换例子
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)