11

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • To add on to all the answers, think about it - an arbitrary identifier can't be a hash value, by definition. – j4nw Jan 12 '18 at 13:00

4 Answers4

7

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.

larsks
  • 277,717
  • 41
  • 399
  • 399
6

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 <my@mail.com> 1513256382 +0100
committer Firstname Lastname <my@mail.com> 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.

Philippe
  • 28,207
  • 6
  • 54
  • 78
4

I do believe it is possible, although it would require some time and work and is a hack. It is true that hash is generated by values like:

  • content
  • commit date
  • ids of the previous commit(s)
  • ...

But, if you can sacrifice consistency of one of this values, it can be done. Here is an example: https://github.com/vog/beautify_git_hash
It's a script, which makes a commit, and then is looking for a specific date to make git hash the one you are looking for and then makes an amend with this date.

Rumid
  • 1,627
  • 2
  • 21
  • 39
2

If you make changes to the commits, the IDs are going to change.(They're dependent on the commit content itself, plus what changes are in them).

So, no.

cst1992
  • 3,823
  • 1
  • 29
  • 40
  • Here by 'commit content' I mean metadata such as date, author name, etc. So even if you do something like `git commit --amend --reset-author` it'll change the author, and so the id. – cst1992 Jan 12 '18 at 13:40