git 分支未知但结帐有效

git branch unkown but checkout works

在我的远程存储库上创建了一个新分支。在我的工作目录(在主分支上)的 GitBash 中,我键入 git remote updategit pull。据我了解 git remote update 将更新所有分支集以跟踪远程分支,如下所述: What is the difference between 'git remote update', 'git fetch' and 'git pull'?

因此,当我键入 git diff master newBranch --name-only 时,我希望看到两个分支中不同的文件列表。但是我收到了以下错误消息:

fatal: ambiguous argument 'newBranch': unknown revision or path not in the working tree.

但是如果我输入 git checkout newBranch 它工作正常,如果我通过输入 git checkout master 切换回 master 突然 git diff master newBranch --name-only 工作完美?

有人可以向我解释一下这种行为吗?

当您第一次输入 git diff master newBranch --name-only 时,您没有任何本地 newBranch 分支。所以,它给出了错误:

fatal: ambiguous argument 'newBranch': unknown revision or path not in the working tree.

稍后当您通过 git checkout newBranch 命令检出到 newBranch 时,将创建一个新的本地分支。所以,下次 git diff master newBranch --name-only 也能正常工作。

注意: git checkout newBranch实际上是找到本地的newBranch分支,如果找到则切换到该分支。但是,如果找不到,则在远程分支列表中查找,如果找到,则创建一个新的本地 newBranch 分支来跟踪远程 newBranch 分支。

正如您所提到的,您没有分支的本地副本 "newBranch" 因此您需要指定要对来自远程的分支标记进行比较,如下所示:

git diff origin/master origin/newBranch --name-only

或者假设你在本地有 master:

git diff master origin/newBranch --name-only

检查您在本地有哪些分支机构:

git branch -l

git branch

检查远程分支

git branch -r

检查所有分支:

git branch -a

所以这在您结帐后对您有用,因为 git 自动创建了一个名为 newBranch 的本地分支。因此,在结帐之前 git branch 不会显示名为 "newBranch" 的分支,但在结帐之后它会显示。