I'm using the following command to get the key of the previous commit to another commit, in context of a file:
git log --max-count=1 --pretty=format:'%H' COMMITKEY~1 -- path/to/file
But in case of a deleted file it will return an error: stderr: 'fatal: ambiguous argument 'path/to/file': unknown revision or path not in the working tree.
So I tried something like this:
git log --max-count=1 --pretty=format:'%H' --all --full-history COMMITKEY~1 -- path/to/file
This actually returns a commit key, but always the newest, not the previous one. Even if I change the revision to COMMITKEY~2
or something else, it will return the same commit key.
What's wrong here?
Are you sure you used --
with the first command?
That your second command does not work is easily explained by your usage of --all
, as --all
is equivialent to listing all refs manually, so you will of course always get the latest commit returned.
Actually you can abbreviate --max-count=1
to -1
, so using git log -1 --pretty='%H' commit-ish~ -- path/to/file
is enough and will also work.
You only get the message 'fatal: ambiguous argument 'path/to/file': unknown revision or path not in the working tree
if you are not using the --
that tells git that there is a path following, as then Git does not know whether this should be a ref that cannot be resolved or a path that might exist in the history.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments