为什么它不是一个提交,也不能从中创建一个分支?

98

我需要处理一组复杂的仓库配置。我有五个仓库:

  1. 机器1上的远程中央仓库。
  2. 我笔记本电脑(机器2)上的本地仓库。
  3. 机器3上的裸仓库。
  4. 机器3上的仓库。
  5. 我们进行代码审查的机器4上的仓库。

因此,我理解它是这样工作的:

  1. 在我的笔记本电脑(机器2)上,我从位于机器1上的中央仓库克隆/拉取。
  2. 我将本地仓库推送到机器3(使用裸仓库作为“中间件”)。

现在我在机器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 fetch origin
  6. git rebase origin/master
  7. git push origin HEAD:refs/for/master

我在第4步遇到问题,我收到以下错误:

fatal: 'machine3/test-branch' 不是提交,分支无法从它创建

附加

当我执行:

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>...]'

1
Machine 3的repo是空的,这意味着它没有任何工作树。git checkout -b test-branch machine-3/test-branch需要一个工作树,所以它会失败。此外,您需要检查machine3/test-branch是否存在。https://mijingo.com/blog/what-is-the-working-tree-in-git - ElpieKay
1
抱歉我错过了。git rev-parse machine3/test-branch 输出什么? - ElpieKay
1
在你运行第四步的仓库中。 - ElpieKay
1
让我们在聊天中继续这个讨论 - ElpieKay
git pull怎么样? - Charlie Parker
显示剩余4条评论
17个回答

199

对于那些寻找答案以解决fatal: 'origin/remote-branch-name' is not a commit and a branch 'local-branch-name' cannot be created from it的人,您可能首先想尝试这个:

git fetch --all

如果你在没有先运行fetch的情况下运行git checkout -b local-branch-name origin/remote-branch-name,你可能会遇到该错误。

之所以显示“不是提交”而不是更清晰的“分支不存在”,是因为Git采用了你指定的origin/remote-branch-name 作为参数,并尝试将其解析为提交哈希值。你也可以在此处使用标签名称和提交哈希值作为参数。如果它们失败,它会生成相同的错误。如果Git无法将提供的分支解析为特定的提交,通常是因为它没有最新的远程分支列表。 git fetch --all 将修复该情况。

--all标志包含在内,以防你有多个远程仓库(例如originbuildserverjoespc等),因为git fetch默认为您的第一个远程仓库——通常是origin。你也可以针对特定的远程仓库运行fetch;例如,git fetch buildserver将仅从buildserver远程获取所有分支。

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


39
对我来说,git fetch origingit fetch origin master没有帮助,我只能使用git fetch origin refs/heads/master:refs/remotes/origin/master - Silly Freak
3
@SillyFreak 那是唯一对我起作用的方法。 - Kristy Welsh
2
@SillyFreak - 我之前无法为上游创建跟踪分支,但在执行以下命令后它开始工作了: git fetch upstream refs/heads/master:refs/remotes/upstream/My-Remote-Branch-Name - SystemsInCode
git pull怎么样? - Charlie Parker
@CharlieParker git pull 只是一个快捷命令,用于执行 git fetch && git merge origin/master 或者你当前的远程名称/远程分支。所以,是的,它也可以工作。 - J.D. Mallen

63

我们遇到了这个错误:

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'。

9
谢谢!我已经困扰了一个小时,直到找到了你的答案 :) - Husam Ibrahim
1
我克隆了单个分支,在跟踪新分支时失败了,这帮助了我很多。 - carboncrystal
1
至少有人明白Git仓库可能非常庞大,使用“git fetch --all”不是一个选项。 - Anton K
2
在我的情况下,这个解决方案是这个问题的答案中唯一对我有效的。 - developer0hye
git pull怎么样? - Charlie Parker

18

我通过以下设置成功解决了这个问题,只需使用以下命令更新配置文件

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


这对我也起作用了。我不得不删除我的唯一远程分支,以一次性摆脱所有跟踪分支,然后那行就消失了。 - François Beaune
谢谢,这解决了我在获取远程分支方面的问题。 - Atul Arvind
对我来说有效,谢谢你。 - undefined

4
我发现这个问题简单的故障排除方法:我在尝试从远程创建全局分支时收到了相同的错误。我通过从提交哈希创建分支来解决它:
git ls-remote origin remote-branch
returned
<hash>  refs/heads/remote-branch

git checkout -b local-branch <hash>

3

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


1

我们在一台运行gitbash的Windows机器上,使用与Google Drive同步的文件夹时遇到了这个确切的错误。

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

卸载并重新安装禁用此功能的gitbash后,问题得以解决。

enter image description here


0

针对这个问题:

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

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

0

检查您的.git文件夹中的git配置,并验证此内容

[core]
    repositoryformatversion = 0
    fileMode = false
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/ABC/project0001.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
    remote = origin
    merge = refs/heads/main
[pull]
    ff = no
[branch "develop"]
    remote = origin
    merge = refs/heads/develop

[remote "origin"] <-- 这个部分


0

指定的远程源上不存在该分支。请仔细检查:您可能正在尝试从葡萄树上摘橙子。


0
问题很复杂,答案很简单。别名和machine3之间存在不匹配。用于远程的别名并非针对machine3。machine3有另一个别名。

当我在远程分支名称中拼写错误时,出现了相同的错误。 - nantitv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接