How to Fix "ambiguous argument" Error When Comparing Branches With "git diff"?

Why Does This Happen?

When comparing two branches using the git diff command, the "fatal: ambiguous argument '...': unknown revision or path not in the working tree." error indicates that the branch name(s) you provided as arguments to git diff (e.g. git diff <branch-1> <branch-2>) cannot be recognized within the repository's history. This error typically occurs when:

  • The specified branch does not exist in the current working directory and/or in the remote repository, or;
  • The specified branch was shallow cloned.

How to Fix the Issue?

To resolve this issue, you can try the following steps:

Check the Branch Names

If you intended to refer to a specific branch, double-check the spelling and ensure that the branch actually exists. You can use the git branch command to list the available branches. If your desired branch is not listed, it means that it is not available locally. In this case, you can do either of the following:

  • Switch to or check out the remote branch locally (e.g. using the git switch <branch> command) and try the git diff command again, or;
  • Prefix the remote repository name to the branch name. For example, if the branch you want to compare is named my-branch in the remote repository named origin, you can use origin/my-branch as the branch name in the git diff command.

For example, if running git diff branch-1 branch-2 is giving you the error "fatal: ambiguous argument 'branch-2': unknown revision or path not in the working tree.", then it means that branch-2 does not exist locally. You can check the local branches using the git branch command to verify this:

$ git branch
branch-3
* branch-1
  master

Please note that the asterisk before branch-1 indicates that it is the currently checked out or active branch.

If the branch does not exist in the list then you can check out the remote branch and then run the git diff command, for example, like so:

$ git switch branch-2
Switched to branch 'branch-2'
Your branch is up to date with 'origin/branch-2'.

$ git diff branch-1 branch-2
# ...

Now, if you re-reun git branch, it should show "branch-2" in the list.

Alternatively, you can prefix the remote repository name to the branch name, for example, like so:

$ git diff branch-1 origin/branch-2
# ...

This will compare the local branch "branch-1" to remote branch "branch-2".

Check the Current Working Directory

You can make sure that you are running the git diff command in the correct working directory. If the branch or file you want to compare is located in a different directory, then navigate to that directory in your terminal before running the git diff command.

Convert to Fully Cloned Repository if Shallow Cloned

If you shallow cloned the git repository, for example, using the following command:

git clone --depth 1 <repo-url>

Then it may give you the "fatal: ambiguous argument 'branch-2': unknown revision or path not in the working tree." error when running git diff command. To fix this, you can "unshallow" the shallow cloned repository and convert it to fully cloned using the following command:

git fetch --unshallow

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.