为什么它不是提交并且无法从中创建分支?

Why is it not a commit and a branch cannot be created from it?

我需要处理复杂的存储库配置。我有 5 个:

  1. 机器 1 上的远程中央存储库。
  2. 我笔记本(机器 2)上的本地存储库。
  3. 机器 3 上的裸存储库。
  4. 机器 3 上的存储库。
  5. 机器 4 上的存储库,我们在其中进行代码审查。

所以,我的理解是这样的:

  1. 在我的笔记本电脑(机器 2)上,我从机器 1 上的中央存储库克隆/拉取。
  2. 我将本地仓库推送到机器 3(使用裸仓库作为 "intermediate")。

现在我在机器 3 上做了一些更改,我想将这些更改推送到机器 4。以下是我需要遵循的说明:

  1. 在机器 3 上完成测试分支中的所有工作,提交。
  2. 推送到机器 3 上的裸仓库:git push origin test-branch
  3. 在您的笔记本电脑上:从 machine-3 存储库获取新提交:git fetch machine3
  4. 从机器 3 检查你的分支:git checkout -b test-branch machine-3/test-branch
  5. 从 machine-4 获取提交:git 获取来源
  6. git 变基 origin/master
  7. git 推送原点 HEAD:refs/for/master

我在执行第 4 步时遇到问题。出现以下错误:

fatal: 'machine3/test-branch' is not a commit and a branch 'test-branch' cannot be created from it

已添加

当我执行

git rev-parse machine3/test-branch

在我的笔记本电脑(机器 2)上,我得到:

machine3/test-branch
fatal: ambiguous argument 'machine3/test-branch': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

问题很复杂/令人费解,答案很简单。别名和 machine3 之间不匹配。已使用的远程别名不适用于 machine3。 machine3 有另一个别名。

对于那些在搜索 fatal: 'origin/remote-branch-name' is not a commit and a branch 'local-branch-name' cannot be created from it 的答案时发现此问题的人,您可能还想先尝试一下:

git fetch --all

如果您 运行 git checkout -b local-branch-name origin/remote-branch-name 没有先 fetching,您可以 运行 进入该错误。

它说 "is not a commit" 而不是像 "branch doesn't exist" 这样更清楚的原因是因为 git 在您指定 origin/remote-branch-name 的地方获取参数并尝试将其解析为提交哈希。您也可以在此处使用标记名称和提交哈希作为参数。如果他们失败了,它会产生同样的错误。如果 git 无法解析您提供给特定提交的分支,通常是因为它没有最新的远程分支列表。 git fetch --all 修复了该情况。

如果您有多个遥控器(例如 originbuildserverjoespc 等),则包含 --all 标志,因为 git fetch默认为您的第一个遥控器——通常是 origin。您还可以 运行 fetch 针对特定的遥控器;例如,git fetch buildserver 只会从 buildserver 远程获取所有分支。

要列出所有遥控器,运行 命令 git remote -v。如果在结果中只看到一个远程名称(例如 origin),则可以从 git fetch 中省略 --all 标志。

如果您要从标签(例如 git checkout -b XXXX v0.1.1)签出分支,您可以先尝试 git fetch --tags

我设法用这个设置解决了这个问题,只需用这个命令更新配置

git config -e --global

并添加此配置。

[remote "origin"]
    url = https://git.example.com/example.git (you can omit this URL)
    fetch = +refs/heads/*:refs/remotes/origin/*

然后你可以git fetch --all

我在 visual studio 代码中使用了 git 工作流程,如下图所示来解决我的问题:

J.D. Mallen 的解决方案涉及 git fetch --all

从 VSCode 1.53(2021 年 1 月)开始,您将拥有 (issue 110811 and PR 111090):

Changing autofetch to a string config which has "current", "all" and "off".

This allows users to fetch from all remotes instead of just their origin.
For developers working on a Fork, this should make extensions work more intuitively.

因此,如果您将 VSCode 设置 git.autofetch 设置为“all”,您甚至不必输入 git fetch --all.

对于这个问题:

fatal: 'machine3/test-branch' is not a commit and a branch 'test-branch' cannot be created from it

对我来说,我应该先检查到 test-branch,然后它工作正常并从 test-branch 创建了一个新分支。

我们遇到了这个错误:

fatal: 'origin/newbranch' is not a commit and a branch 'newbranch' cannot be created from it

因为我们做了一个简约的克隆使用:

git  clone  --depth 1  --branch 'oldbranch'  --single-branch  'git@github.com:user/repo.git'

对我们来说,最简单的解决方法是:

git  remote  set-branches  --add  'origin'  'newbranch'
git  fetch  'origin'
git  checkout  --track  'origin/newbranch'

假设远程名为'origin',远程分支名为'newbranch'。

我们在 Windows 机器 运行 与 google 驱动器同步的文件夹上的 gitbash 中遇到了这个确切的错误。

这是由于启用了“启用实验性内置文件系统监视器”功能造成的。

在禁用此功能的情况下卸载并重新安装 gitbash 后,它已修复。

我发现这个问题解决了更简单的问题:我在尝试从远程创建本地分支时收到同样的错误。 我通过从提交哈希创建分支来解决它:

git ls-remote origin remote-branch
returned
<hash>  refs/heads/remote-branch

git checkout -b local-branch <hash>