解决git在detached HEAD状态下提交找不到的两个方法

本文介绍了如何处理git detached HEAD状态下的提交丢失问题。当使用'origin/分支名'而非'分支名'切换分支导致HEAD游离,直接提交后,切换到其他分支会使提交看似丢失。可以通过查看切换提示或使用git reflog找到commit-id,再通过cherry-pick应用到目标分支,恢复提交。
摘要由CSDN通过智能技术生成

刚刚帮同事解决了一个关于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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值