1

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?

airborne
  • 3,664
  • 4
  • 15
  • 27

2 Answers2

1

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.

Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
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.

code_fodder
  • 15,263
  • 17
  • 90
  • 167