Search code examples
gitversion-controlgit-remote

git branch unkown but checkout works


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?


Solution

  • 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.