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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49297153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:15:35  来源:igfitidea点击:

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

gitgit-non-bare-repository

提问by Roman

I need to work with an intricate configuration of repositories. I have 5 of them:

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

  1. A remote central repository on machine 1.
  2. My local repository on my notebook (machine 2).
  3. A bare repository on machine 3.
  4. A repository on machine 3.
  5. A repository on machine 4 where we do code review.
  1. 机器 1 上的远程中央存储库。
  2. 我的笔记本(机器 2)上的本地存储库。
  3. 机器 3 上的裸仓库。
  4. 机器 3 上的存储库。
  5. 机器 4 上的存储库,我们在其中进行代码。

So, my understanding that it works this way:

所以,我的理解是它是这样工作的:

  1. On my laptop (machine 2) I clone / pull from the central repository located on machine 1.
  2. I push the local repo to the machine 3 (using the bare repository as a "intermediate").
  1. 在我的笔记本电脑(机器 2)上,我从位于机器 1 上的中央存储库克隆/拉取。
  2. 我将本地存储库推送到机器 3(使用裸存储库作为“中间”)。

Now I did some changes on the machine 3 and I want to push these changes to machine 4. Here are the instructions that I need to follow:

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

  1. On machine 3 do all work in your test-branch, commit.
  2. Push to your bare repo on machine 3: git push origin test-branch
  3. On your laptop: fetch new commits from the machine-3 repo: git fetch machine3
  4. Check out your branch from machine 3: git checkout -b test-branch machine-3/test-branch
  5. Fetch commits from machine-4: git fetch origin
  6. git rebase origin/master
  7. git push origin HEAD:refs/for/master
  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. 从机器 4 获取提交:git fetch origin
  6. git rebase 原点/主
  7. git push origin HEAD:refs/for/master

I have problems with step 4. I get the following error:

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

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

ADDED

添加

When I execute

当我执行

git rev-parse machine3/test-branch

On my laptop (machine 2) I get:

在我的笔记本电脑(机器 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>...]'

采纳答案by Roman

The question is complex / convolute, the answer is simple. There was a mismatch between the alias and machine3. The alias for the remote that has been used, was not for machine3. The machine3 had another alias.

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

回答by J.D. Mallen

For those who found this searching for an answer to fatal: 'origin/remote-branch-name' is not a commit and a branch 'local-branch-name' cannot be created from it, you may also want to try this first:

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

git fetch --all

If you run git checkout -b local-branch-name origin/remote-branch-namewithout fetching first, you can run into that error.

如果您git checkout -b local-branch-name origin/remote-branch-name没有fetch先运行ing,则可能会遇到该错误。

The reason it says "is not a commit" rather than something clearer like "branch doesn't exist" is because git takes the argument where you specified origin/remote-branch-nameand tries to resolve it to a commit hash. You can use tag names and commit hashes as an argument here, too. If they fail, it generates the same error. If git can't resolve the branch you provide to a specific commit, it's usually because it doesn't have the freshest list of remote branches. git fetch --allfixes that scenario.

它说“不是提交”而不是“分支不存在”之类的更清楚的原因是因为 git 在您指定的地方接受参数origin/remote-branch-name并尝试将其解析为提交哈希。您也可以在此处使用标记名称和提交哈希作为参数。如果它们失败,则会生成相同的错误。如果 git 无法解析您提供给特定提交的分支,通常是因为它没有最新的远程分支列表。git fetch --all修复了这种情况。

The --allflag is included in case you have multiple remotes (e.g. origin, buildserver, joespc, etc.), because git fetchby itself defaults to your first remote-- usually origin. You can also run fetchagainst a specific remote; e.g., git fetch buildserverwill only fetch all the branches from the buildserverremote.

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

To list all your remotes, run the command git remote -v. You can omit the --allflag from git fetchif you only see one remote name (e.g. origin) in the results.

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

回答by ttfreeman

If you're checking out a branch from a tag (like git checkout -b XXXX v0.1.1) , you can try git fetch --tagsfirst.

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