问题
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