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

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

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


当前回答

对于其他人可能正在寻找一种获得分支名称的方法,但却得到了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;
}

其他回答

try

git reflog 

这给了你一个HEAD和分支指针的历史 哪里搬过去了。

例如:

88ea06b HEAD@{0}: checkout:从DEVELOPMENT移动到remotes/origin/SomeNiceFeature e47bf80 HEAD@{1}:拉动原点发展:快进

这个列表的顶部是一个原因,人们可能会遇到一个分离的头 状态……检查一个远程跟踪分支。

分离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.

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

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

进入git分离头状态的另一种方法是尝试提交到远程分支。喜欢的东西:

git fetch
git checkout origin/foo
vi bar
git commit -a -m 'changed bar'

请注意,如果您这样做,任何进一步尝试签出origin/foo将使您回到分离的头部状态!

解决方案是创建自己的本地foo分支来跟踪origin/foo,然后可选地push。

这可能与您最初的问题无关,但该页在“git分离头”的谷歌次点击中排名靠前,这个场景的文档记录严重不足。

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

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

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

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