Есть ли способ получить идентификатор фиксации 1, 2 и 3?
Да, но не обязательно всегда.
$ git init
$ touch .gitignore
$ git add .gitignore
$ git ci -m .gitignore
$ echo 1 > myfile.txt
$ git add myfile.txt
$ git ci -m 'this is a commit on master, myfile.txt was modified by this commit'
$ git co -b mybranch
$ echo 2 >> myfile.txt
$ git ci -am "this is a commit on branch mybranch, myfile.txt was modified by this commit"
$ git co master
$ echo 3 >> myfile.txt
$ git ci -am "this is a commit on master, myfile.txt was modified by this commit"
$ git log --graph --all --oneline
* 2d0b94b (HEAD -> master) this is a commit on master, myfile.txt was modified by this commit
| * d223e74 (mybranch) this is a commit on branch mybranch, myfile.txt was modified by this commit
|/
* 0bf67f0 this is a commit on master, myfile.txt was modified by this commit
* b6c7c02 .gitignore
$ git merge mybranch
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.
$
На данный моментвы можете использовать git ls-files -u
, чтобы легко получить BLOB-объекты для 1, 2 и 3. И от BLOB-объекта вы можете найти соответствующий коммит:
git ls-files -u
100644 d00491fd7e5bb6fa28c517a0bb32b8b506539d4d 1 myfile.txt
100644 2b2f2e1b9261c50c3816610eb3eb140fabf1745a 2 myfile.txt
100644 1191247b6d9a206f6ba3d8ac79e26d041dd86941 3 myfile.txt
$ ./what-commit-contains-file-blob.sh myfile.txt d00491fd7e5bb6fa28c517a0bb32b8b506539d4d
0bf67f0 this is a commit on master, myfile.txt was modified by this commit
$ ./what-commit-contains-file-blob.sh myfile.txt 2b2f2e1b9261c50c3816610eb3eb140fabf1745a
2d0b94b (HEAD -> master) this is a commit on master, myfile.txt was modified by this commit
$ ./what-commit-contains-file-blob.sh myfile.txt 1191247b6d9a206f6ba3d8ac79e26d041dd86941
d223e74 (mybranch) this is a commit on branch mybranch, myfile.txt was modified by this commit
$ cat what-commit-contains-file-blob.sh
#!/bin/sh
# /179224/kakoi-kommit-imeet-etot-blob-obekt
# https://stackoverflow.com/a/32611564/23118
file=${1:?missing file name argument}
blob=${2:?missing blob argument}
git log --all --pretty=format:%H -- $file \
| xargs -n1 -I% sh -c "git ls-tree % -- $file | grep -q $blob && echo %" \
| xargs -n 1 git log -n 1 --oneline
$
Так что для этого простого примерарепо это решение работало отлично, но BLOB-объект может быть частью нескольких коммитов, поэтому я не уверен, что это всегда даст уникальный результат.