guettli
guettli

Reputation: 27855

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

I used git diff origin often in the past.

In a different environment it does not work. I have no clue why.

user@host> git diff origin
fatal: ambiguous argument 'origin': unknown revision or path 
       not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Status:

user@host> git status
On branch master
nothing to commit, working directory clean

Remotes:

user@host> git remote -v
origin  https://example.com/repos/djangotools (fetch)
origin  https://example.com/repos/djangotools (push)

Version:

user@host> git --version
git version 2.7.4

With "git version 1.8.1.4" git diff origin works.

BTW I see the same err msg if I use "git diff origin/master"

BTW2, I think the "/master" is redundant. The sane default is to compare the local branch with the same branch on the remote site.

Upvotes: 106

Views: 320174

Answers (14)

Dionis Beqiraj
Dionis Beqiraj

Reputation: 797

I was having this issue with the Bitbucket pipeline. Basically I needed to store the branch from remote repository locally before performing any further git operation on it.

Th wrong solution

git fetch origin develop
git log develop..$BITBUCKET_BRANCH // I was getting the error here

The right solution

git fetch origin develop:develop
git log develop..$BITBUCKET_BRANCH

Upvotes: 1

NickyPatel
NickyPatel

Reputation: 555

i was getting this error when I was trying to update my branch by mistake.

I found that my branch was merged.

Upvotes: 0

humazed
humazed

Reputation: 76922

If you are having this issue in GitHub action you need to add:

fetch-depth: 0

So the step is

steps:
  - name: Checkout repository
    uses: actions/checkout@v3
    with:
      fetch-depth: 0

For additional info please check https://github.com/actions/checkout/issues/296

Upvotes: 10

T.Todua
T.Todua

Reputation: 56429

It might happen if you fetched/cloned with depth=1. So, you might also need to fetch the target branch from remote:

git remote set-branches origin 'your_target_branch'
git fetch --depth=1
// show local branches to confirm
git branch -a
// git diff ....

(note, replace your_target_branch with whatever you need, you might even need master)

Upvotes: 3

Yannick
Yannick

Reputation: 9

start with git fetch

and git diff master origin/master

that is all.

Upvotes: 0

vksssd
vksssd

Reputation: 67

Try to unintall with:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

And then, install with:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Upvotes: -2

Ankita Agarwal
Ankita Agarwal

Reputation: 159

Run the following setup then you will be ready to use brew

Run unintsall script :- /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

Set Git Compression :- git config --global core.compression 0

Set Git buffer size :- git config --global http.postBuffer 1048576000

Run installation script :- /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Upvotes: 2

Manan Shah
Manan Shah

Reputation: 31

  • Remove opt/homebrew folder cd /opt, sudo rm -rf homebrew

  • Install Homebrew : /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

If you are installing homebrew first time and facing this error than also on second attempt first clear remove the /opt/homebrew folder first.

Upvotes: 3

Thuo Ng&#39;ang&#39;a
Thuo Ng&#39;ang&#39;a

Reputation: 211

For those experiencing this error on CI/CD, adding the line below worked for me on my GitHub Actions CI/CD workflow right after running pip install pyflakes diff-cover:

git fetch origin master:refs/remotes/origin/master

This is a snippet of the solution from the diff-cover github repo:

Solution: diff-cover matches source files in the coverage XML report with source files in the git diff. For this reason, it's important that the relative paths to the files match. If you are using coverage.py to generate the coverage XML report, then make sure you run diff-cover from the same working directory.

I got the solution on the links below. It is a documented diff-cover error.

https://diff-cover.readthedocs.io/en/latest//README.html https://github.com/Bachmann1234/diff_cover/blob/master/README.rst

Hope this helps :-).

Upvotes: 21

sfinkens
sfinkens

Reputation: 1350

If origin points to a bare repository on disk, this error can happen if that directory has been moved (even if you update the working copy's remotes). For example

$ mv /path/to/origin /somewhere/else
$ git remote set-url origin /somewhere/else
$ git diff origin/master
fatal: ambiguous argument 'origin': unknown revision or path not in the working tree.

Pulling once from the new origin solves the problem:

$ git stash
$ git pull origin master
$ git stash pop

Upvotes: 4

j-p
j-p

Reputation: 3828

This worked for me on win replace REL_PATH_TO_FILE with the relative path to the file to remove Removing sensitive data from a repository The docs say full path - but that errored for me -so I tried rel path and it worked.

<from the repo dir>git filter-branch --force --index-filter "git rm --cached --ignore-unmatch REL_PATH_TO_FILE" --prune-empty --tag-name-filter cat -- --all

Upvotes: 1

cosmicdust
cosmicdust

Reputation: 656

I ran into the same situation where commands such as git diff origin or git diff origin master produced the error reported in the question, namely Fatal: ambiguous argument...

To resolve the situation, I ran the command

git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master

to set refs/remotes/origin/HEAD to point to the origin/master branch.

Before running this command, the output of git branch -a was:

* master
  remotes/origin/master

After running the command, the error no longer happened and the output of git branch -a was:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

(Other answers have already identified that the source of the error is HEAD not being set for origin. But I thought it helpful to provide a command which may be used to fix the error in question, although it may be obvious to some users.)


Additional information:

For anybody inclined to experiment and go back and forth between setting and unsetting refs/remotes/origin/HEAD, here are some examples.

To unset:
git remote set-head origin --delete

To set:
(additional ways, besides the way shown at the start of this answer)
git remote set-head origin master to set origin/head explicitly
OR
git remote set-head origin --auto to query the remote and automatically set origin/HEAD to the remote's current branch.

References:

  • This SO Answer
  • This SO Comment and its associated answer
  • git remote --help see set-head description
  • git symbolic-ref --help

Upvotes: 30

Roma
Roma

Reputation: 333

Sometimes things might be simpler. I came here with the exact issue and tried all the suggestions. But later found that the problem was just the local file path was different and I was on a different folder. :-)

eg -

~/myproject/mygitrepo/app/$ git diff app/TestFile.txt

should have been

~/myproject/mygitrepo/app/$ git diff TestFile.txt

Upvotes: 4

Chris
Chris

Reputation: 8656

The git diff command typically expects one or more commit hashes to generate your diff. You seem to be supplying the name of a remote.

If you had a branch named origin, the commit hash at tip of the branch would have been used if you supplied origin to the diff command, but currently (with no corresponding branch) the command will produce the error you're seeing. It may be the case that you were previously working with a branch named origin.

An alternative, if you're trying to view the difference between your local branch, and a branch on a remote would be something along the lines of:

git diff origin/<branchname>

git diff <branchname> origin/<branchname>

Or other documented variants.

Edit: Having read further, I realise I'm slightly wrong, git diff origin is shorthand for diffing against the head of the specified remote, so git diff origin = git diff origin/HEAD (compare local git branch with remote branch?, Why is "origin/HEAD" shown when running "git branch -r"?)

It sounds like your origin does not have a HEAD, in my case this is because my remote is a bare repository that has never had a HEAD set.

Running git branch -r will show you if origin/HEAD is set, and if so, which branch it points at (e.g. origin/HEAD -> origin/<branchname>).

Upvotes: 69

Related Questions