我使用下面的命令来获得两个分支之间的差异,使用两个点方法。
git diff master..hotfix_master
在gitlab管道中,使用相同的命令,它将失败
> $ 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>...]'
但从本地的笔记本电脑上看,它还能工作。
我真的不明白,到底是怎么回事。
我需要在某个地方使用转义字符吗?
请建议
编辑1 :
即使在使用origin
之后,它也不起作用。
$ 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>...]'
编辑: 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>...]'
编辑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>...]'
编辑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"
发布于 2021-12-13 14:59:15
有几种情况下会发生错误。
以下是它们:
git diff
命令从git忽略的目录中运行:git diff
命令是从一个新创建的目录中运行的,该目录尚未添加到git中(未执行):对于第一种和第二种情况,解决方案就是走出被忽略的目录,在父文件夹中运行git命令。
对于第三种情况,只需确保分支存在,然后再对它们运行diff。另外,在比较远程分支之前,不要忘记执行git fetch
。
发布于 2021-12-13 20:58:00
TL;DR
将您的Git“深度”设置为零;请参见the documentation。
长
解决问题的关键在于:
在gitlab管道里
虽然没有足够的文档,你不可能提前知道这一点。(我也在您的问题中添加了gitlab-ci标记,因为管道是CI系统的一部分。)你需要知道的是:
这一切归结为对你来说有两个问题:
HEAD
。这使得不可能要求任何其他提交。因此不能运行此git diff
,因为:
解决这两个问题的简单方法是覆盖CI系统--GitLab--在创建Git存储库时所做的浅层和单分支克隆。在每个CI系统中,您是如何做到这一点的具体细节是不同的。在GitLab,it varies by GitLab version。这是相当普遍的,虽然,采取一个“深度”的零,意味着做一个完整的克隆。
请注意,您(现在正确地)需要使用origin/
名称,因为完整的克隆复制了所有提交,但将原始存储库的所有分支名称重命名为远程跟踪名称。git clone
操作使您处于没有分支的克隆中,除非最后的克隆步骤是创建和签出一个分支。(正如我前面提到的,有时最后一步是检查一个独立的头,在这种情况下,实际上没有分支。)您还可以让git clone
完全跳过最后一步,这使您没有分支,尽管可能无法从CI管道中做到这一点:细节将取决于CI系统)。
https://stackoverflow.com/questions/70335406
复制相似问题