git branch unkown but checkout works

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:58:39

问题


on my remote repository a new branch has been created. In GitBash in my Working Directory (on master branch) I type git remote update and git pull. To my understanding git remote update will update all branches set to track remote ones as explained here: What is the difference between 'git remote update', 'git fetch' and 'git pull'?

So when I type git diff master newBranch --name-only I expected to see a list of files which are different in both branches. But instead I got the following error message:

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

But if I type git checkout newBranch it works fine, and if I switch back to master by typing git checkout master suddenly git diff master newBranch --name-only works perfectly?

Could anyone explain to me this behavior?


回答1:


As you mentioned you don't have the local copy of the branch "newBranch" so you need to specify that you want to do a diff with the branch tag from the remote like this:

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

or assuming you have the master locally:

git diff master origin/newBranch --name-only

Check which branches you have locally:

git branch -l

or

git branch

check remote branches

git branch -r

check all branches:

git branch -a

So this worked for you after you did a checkout because git automatically created a local branch called newBranch. So before your checkout git branch would not show a branch called "newBranch" but after the checkout it would.




回答2:


When you typed git diff master newBranch --name-only for first time, you had not any local newBranch branch. So, it gives the error:

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

Later when you checked out to the newBranch by git checkout newBranch command, a new local branch is created. So, next time git diff master newBranch --name-only is working as well.

Note: git checkout newBranch actually finds a local newBranch branch if found then switch to the branch. But if not found then finds in remote branch lists if found then create a new local newBranch branch tracking the remote newBranch branch.



来源:https://stackoverflow.com/questions/44695733/git-branch-unkown-but-checkout-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!