Command "git diff {branch}...HEAD" fails (master fetch doesn't help)
See original GitHub issueJenkins SCM module does the following (minimal representation, it actually does a few more things):
git checkout git@github.com:<path>.git <dir>
cd <dir>
git checkout -f <commit-hash>
If I invoke git diff {branch}...HEAD
(as is done by diff-cover), I get the same error as is produced when running diff-cover:
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Preceding the diff with the recommended fetch does not work either:
git fetch origin $(TARGET_BRANCH):refs/remotes/origin/$(TARGET_BRANCH)
In this case, TARGET_BRANCH is “master”, which is the repo’s default branch.
Output of “git branch”:
% git branch
* (HEAD detached at 0fcb182)
master
The full Jenkins SCM operation looks like this:
> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url git@github.com:<repo-path> # timeout=10
Fetching upstream changes from git@github.com:<repo-path>
> git --version # timeout=10
using GIT_SSH to set credentials
> git -c core.askpass=true fetch --tags --progress git@github.com:<repo-path> +refs/pull/*:refs/remotes/origin/pr/* +refs/heads/master:refs/remotes/origin/master
> git rev-parse 0fcb1822ad5bcf71b9e0c7cddb85dcd5a54d4967^{commit} # timeout=10
Checking out Revision 0fcb1822ad5bcf71b9e0c7cddb85dcd5a54d4967 (detached)
> git config core.sparsecheckout # timeout=10
> git checkout -f 0fcb1822ad5bcf71b9e0c7cddb85dcd5a54d4967
> git rev-list 7ea84cf781acc06bb72767860dc1132d5663a90d # timeout=10
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
[rejected] master -> master (fetch first) - git - Stack Overflow
The answer is there, git is telling you to fetch first. Probably somebody else has pushed to master already, and your commit is...
Read more >git-fetch Documentation - Git
By default git fetch refuses to update the head which corresponds to the current branch. This flag disables the check.
Read more >git: fetch and merge, don't pull | Mark's Blog
The other problem is that by both fetching and merging in one command, your working directory is updated without giving you a chance...
Read more >Merging vs. Rebasing | Atlassian Git Tutorial
Compare git rebase with the related git merge command and identify all of the potential opportunities to ... Merging master into the feature...
Read more >Fix git “tip of your current branch is behind its remote ...
git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @twall , @Bachmann1234 , I think I hit a similar problem. In my case I’m not using Jenkins but rather Teamcity. Also Teamcity does something like “git checkout -f <commit-hash>” at some point and thus clones the git repo in a “HEAD detached” condition (https://www.git-tower.com/learn/git/faq/detached-head-when-checkout-commit/). I tried and indeed a command like “git diff origin/develop…HEAD” in such condition fails with error
fatal: ambiguous argument 'origin/develop..HEAD': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
I’m not sure how to workaround this… any solution or workaround so far ?
I was able to get this to work in GitHub Actions by doing 2 separate checkouts in my workflow (1 for master / main first, then the branch / ref that triggered the workflow, my PR branch in my case):
I have added this in case anyone else is trying to get diff_cover working with GitHub Actions