Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying Git commit hash

Tags:

git

Can I specify Git commit hash, instead of letting Git generate it?

Is it even possible? I didn't find any way in Git documentation or online.


For some background: I'm recreating a new Git public repository from a private one programmatically. I need to make lot of changes to each commit to remove confidential information. But I'd like to preserve commit IDs.

like image 340
Martin Prikryl Avatar asked Jan 12 '18 12:01

Martin Prikryl


2 Answers

Here is an example of the content of a commit (anonymized ;) ) that you could get using git cat-file -p da500aa4f54cbf8f3eb47a1dc2c136715c9197b9 (replace with the sha1 of one of your commits):

tree 48038c4d189536a0862a2c20ed832dc34bd1c8b2
parent f0bb5942f47193d153a205dc089cbbf38299dd1a
author Firstname Lastname <[email protected]> 1513256382 +0100
committer Firstname Lastname <[email protected]> 1515152927 +0100

This is a commit message

If one of these data changes, all the sha1 changes:

  • The tree is the sha1 calculated from the content of the files and directories contents.

  • Parent is the parent commit hash.

  • Notice that there is also dates inside, so if you do exactly the same commit but at different moment, the sha1 will change also

PS: You could continue with the command git cat-file -p to continue explore the tree and better understand the way git store data.

like image 84
Philippe Avatar answered Oct 04 '22 14:10

Philippe


A git commit hash is a cryptographic checksum that is calculated from the state of your repository, including the hash of all the files in the repository, the hash of the previous commit, the current date and time, etc.

It is not possible to specify this manually.

More more information, see this question.

like image 33
larsks Avatar answered Oct 04 '22 13:10

larsks