When git working tree is in conflict state during merge/rebase/cherry-pick, it's possible to get ours/theirs/base version of individual files by several ways, e.g. git checkout --ours/--theirs path/file
or git show :n:path/file
where n is 1, 2 or 3, see e.g. Git: get ours/theirs file content during merge
However, I would like to get ours and theirs commit hashes (SHA). For ours it's simple: git rev-parse HEAD
. But for theirs I have to try git rev-parse
for preudorefs MERGE_HEAD, REBASE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD, ...
(...
is for maybe more pseudorefs? E.g. what pseudoref has stash pop
conflict?). Is there any simple way to get theirs commit hash for all scenarios simply and reliably?
When git working tree is in conflict state during merge/rebase/cherry-pick, it's possible to get ours/theirs/base version of individual files by several ways, e.g. git checkout --ours/--theirs path/file
or git show :n:path/file
where n is 1, 2 or 3, see e.g. Git: get ours/theirs file content during merge
However, I would like to get ours and theirs commit hashes (SHA). For ours it's simple: git rev-parse HEAD
. But for theirs I have to try git rev-parse
for preudorefs MERGE_HEAD, REBASE_HEAD, CHERRY_PICK_HEAD, REVERT_HEAD, ...
(...
is for maybe more pseudorefs? E.g. what pseudoref has stash pop
conflict?). Is there any simple way to get theirs commit hash for all scenarios simply and reliably?
1 Answer
Reset to default 2Is there any simple way to get theirs commit hash for all scenarios simply and reliably?
There's no requirement that there be a commit hash for it. Git will already construct a base version from multiple merge bases resolving merges that have them, it's already common in some workflows to have a :1:$path
revision that's referenced only in the index, that was constructed specially.
Also: anyone doing something Git doesn't already have a convenience command for can load the index arbitrarily (pretty much the same way the existing conveniences do) with git update-index --index-info
or --cacheinfo
or --info-only
.
You could ls `git rev-parse --git-dir`/*_HEAD
and ignore FETCH_HEAD to get all the _HEAD
s of commands that follow the existing naming convention, and walk through their content looking for a match.
path=example.c
pathrev=`git rev-parse :3:$path`
for head in `git rev-parse --git-dir`/*_HEAD
do if test x`git rev-parse ${head##*/}:$path 2>&-` = x$pathrev
then git rev-parse ${head##*/}
fi; done
will do it for existing commands.
-s --format=%H
togit-show
to get just the oid. – William Pursell Commented Nov 20, 2024 at 12:32git show :n:path/file
to show file content. I can get file oid bygit rev-parse :n:path/file
, but I want commit oid, see the question. – xmedeko Commented Nov 20, 2024 at 13:18