I'm using following command to get the diff between two branches using two dots method.
git diff master..hotfix_master
in gitlab pipeline , using same command, it fails
> $ git diff hotfix_master..master fatal: ambiguous argument
> 'hotfix_master..master': unknown revision or path not in the working
> tree. Use '--' to separate paths from revisions, like this: 'git
> <command> [<revision>...] -- [<file>...]'
but from the local laptop, it works.
I'm literarily not able to understand, what the issue.
Do i need to use escape character somewhere?
Please suggest
EDIT 1 :
Even after putting origin
, it is not working.
$ git diff origin/hotfix_master..origin/master
fatal: ambiguous argument 'origin/hotfix_master..origin/master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
EDIT : 2
$ pwd
/builds/irfanjs1/blank-proj
$ ls -al
total 28
drwxrwxrwx 3 root root 4096 Dec 13 15:09 .
drwxrwxrwx 4 root root 4096 Dec 13 15:09 ..
drwxrwxrwx 6 root root 4096 Dec 13 15:09 .git
-rw-rw-rw- 1 root root 445 Dec 13 15:09 .gitlab-ci.yml
-rw-rw-rw- 1 root root 7583 Dec 13 15:09 README.md
-rw-rw-rw- 1 root root 13 Dec 13 15:08 a.txt
$ dir
README.md a.txt
$ git diff origin/hotfix_master..origin/master
fatal: ambiguous argument 'origin/hotfix_master..origin/master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
EDIT 3
$ pwd
/builds/irfanjs1/blank-proj
$ ls -al
total 28
drwxrwxrwx 3 root root 4096 Dec 13 15:55 .
drwxrwxrwx 4 root root 4096 Dec 13 15:54 ..
drwxrwxrwx 6 root root 4096 Dec 13 15:55 .git
-rw-rw-rw- 1 root root 494 Dec 13 15:55 .gitlab-ci.yml
-rw-rw-rw- 1 root root 7583 Dec 13 15:55 README.md
-rw-rw-rw- 1 root root 13 Dec 13 15:54 a.txt
-rw-rw-rw- 1 root root 0 Dec 13 15:55 b.txt
$ dir
README.md a.txt b.txt
$ git branch
* (HEAD detached at 486173f)
$ git branch -r
origin/master
$ git diff origin/hotfix_master..origin/master
fatal: ambiguous argument 'origin/hotfix_master..origin/master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
EDIT 4
$ dir
README.md a.txt b.txt
$ git branch
* (HEAD detached at 87eb7a5)
$ git branch -r
origin/master
$ git fetch
From https://gitlab.com/irfanjs1/blank-proj
* [new branch] hotfix_master -> origin/hotfix_master
* [new branch] main -> origin/main
$ git diff origin/hotfix_master...origin/master
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ddf5df3..5b149d0 100644
--- a/.gitlab-ci.yml
b/.gitlab-ci.yml
@@ -25,6 25,9 @@ merge-to-master:
- "pwd"
- "ls -al"
- "dir"
- - "git diff origin/hotfix_master..origin/master"
- "git branch"
- "git branch -r"
- "git fetch"
- "git diff origin/hotfix_master...origin/master"
CodePudding user response:
There are several cases when the error occurs.
Here are they:
The
git diff
command is run from a directory that is ignored by git:The
git diff
command is run from a directory that is newly created and not added to the git yet (unstaged):
For the first and second cases, the solution would be just going out of your ignored directory and running the git commands in the parent folder.
For the third case, just make sure the branches exist before running diff on them.
CodePudding user response:
TL;DR
Set your Git "depth" to zero; see the documentation.
Long
The key to your problem is here:
in gitlab pipeline
though without adequate documentation, there's no way you would know this in advance. (I added the gitlab-ci tag to your question as well, since the pipeline is part of the CI system.) Here's what you need to know:
CI systems in general operate on clones of the original repository. (They cannot use the original web-hosting-site repository directly for various reasons.)
CI systems try to save time and space when possible.
In Git, one way to save time and space in a clone is to make a shallow clone. This may or may not also use detached HEAD mode (your particular system seems to do that).
A shallow clone in Git is by default also a single-branch clone.
What this all boils down to is two problems for you:
There's only one commit in your repository, in your CI pipeline. This makes it impossible to diff your one commit against any other commit.
There is only one reliable name in your repository, namely
HEAD
. This makes it impossible to ask for any other commit.
So you cannot run this git diff
because:
- you cannot ask for the commits by (branch) name, as one or more of those names don't exist;
- even if you could, you don't have the other commit(s).
The easy solution to both problems is to override the shallow-and-single-branch clone that CI systems—GitLab is not alone here—make when they make clones of Git repositories. The precise details of how you do this, in each CI system, varies. In GitLab, it varies by GitLab version. It's fairly common, though, to take a "depth" of zero to mean make a full clone.
Note that you (now, correctly) will need to use origin/
names, since the full clone copies all the commits but renames all of the original repository's branch names to remote-tracking names. A git clone
operation leaves you in a clone with no branches, unless the final clone step is to create and check out one branch. (As I mentioned above, sometimes the final step is to check out a detached HEAD, in which case you do in fact have no branches. You can also make git clone
skip the final step entirely, which leaves you with no branches, though there may be no way to do this from a CI pipeline: the details will depend on the CI system.)