我今天结束了一个分离的头,同样的问题描述:git推送说所有最新的,即使我有局部的变化

据我所知,我没有做任何不同寻常的事情,只是从本地回购中提交和推送。

那么,我是如何得到一个分离的头部的呢?


当前回答

分离HEAD意味着当前签出的不是本地分支。

导致分离HEAD状态的一些场景:

If you checkout a remote branch, say origin/master. This is a read-only branch. Thus, when creating a commit from origin/master it will be free-floating, i.e. not connected to any branch. If you checkout a specific tag or commit. When doing a new commit from here, it will again be free-floating, i.e. not connected to any branch. Note that when a branch is checked out, new commits always gets automatically placed at the tip. When you want to go back and checkout a specific commit or tag to start working from there, you could create a new branch originating from that commit and switch to it by git checkout -b new_branch_name. This will prevent the Detached HEAD state as you now have a branch checked out and not a commit.

其他回答

任何不是分支名称的提交签出都会得到一个分离的HEAD。SHA1表示树枝的顶端,仍然给出一个分离的HEAD。只有签出本地分支名称才能避免这种模式。

参见使用分离的HEAD进行承诺

当分离HEAD时,除了没有更新命名分支外,提交工作正常。(您可以将其视为一个匿名分支。)

例如,如果您签出一个“远程分支”而没有首先跟踪它,您可能会得到一个分离的HEAD。

见git:开关分支没有分离头

意思是:git签出origin/main(或者以前的origin/master)会导致:

Note: switching to 'origin/main'.

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 a1b2c3d My commit message

这就是为什么你不应该再使用git checkout,而应该使用新的git switch命令。

使用git switch,同样尝试“签出”(切换到)远程分支将立即失败:

git switch origin/main
fatal: a branch is expected, got remote branch 'origin/main'

添加更多git开关:

使用Git 2.23(2019年8月),你不必再使用令人困惑的Git checkout命令了。

git switch也可以签出一个分支,并获得一个分离的HEAD,除了:

它有一个显式的—detach选项

在不创建新分支的情况下提交HEAD~3进行临时检查或实验: git开关——分离HEAD~3 HEAD现在在9fc9555312合并分支cc/shared-index-permbits

它不能错误地分离远程跟踪分支

See:

C:\Users\vonc\arepo>git checkout origin/master
Note: switching to 'origin/master'.

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.

Vs.使用新的git switch命令:

C:\Users\vonc\arepo>git switch origin/master
fatal: a branch is expected, got remote branch 'origin/master'

如果你想创建一个新的本地分支来跟踪一个远程分支:

git switch <branch> 

如果没有找到<分支>,但是在恰好一个远程(称其为<远程>)中存在一个具有匹配名称的跟踪分支,则将其等效于 Git开关-c <分支>——track <远程>/<分支>

别再犯错了! 没有更多不想要的分离头!

如果你使用git switch <tag>而不是git switch——detach <tag>, git 2.36会帮助你记住缺失的——detach选项。

当你签出到一个commit git checkout <commit-hash>或一个远程分支时,你的HEAD将被分离,并尝试在上面创建一个新的提交。

任何分支或标记都无法访问的提交将在30天后被垃圾收集并从存储库中删除。

解决这个问题的另一种方法是为新创建的提交和签出创建一个新分支。Git checkout -b <branch-name> <commit-hash>

本文将说明如何进入分离HEAD状态。

在我的案例中,情况是这样的:

创建一个新的分支(feb_debugging)。 运行git fetch 我看到新的分支(feb_debugging)被拉 现在,我使用git checkout origin/feb_debugging

在这里它让我到HEAD现在在....

我只需要再结账一次

Git签出feb_debugging 现在git说我在feb_debugging分支。

以下是VonC的评论,这是我如何解决这个“分离的头部”问题的简短版本。

在我的遥控器上创建了一个分支;来源/ dev /功能 在我的本地运行git fetch,所以现在我的本地将知道这个新的远程分支 现在运行git switch feature/dev,我们就完成了!

对于其他人可能正在寻找一种获得分支名称的方法,但却得到了HEAD,这是我想出的:

const { execSync } = require('child_process');

const getBranchName = () => {
  let branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
  if (branch === 'HEAD') branch = execSync(`git branch -a --contains HEAD | sed -n 2p | awk '{ printf $1 }'`).toString().trim();
  return branch;
}