Martin Prikryl
Martin Prikryl

Reputation: 202612

Specifying Git commit hash

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.

Upvotes: 13

Views: 14530

Answers (4)

Philippe
Philippe

Reputation: 31227

You can't decide of the hash because it is built using the content of the commit.

Here is an example of the content of a commit (anonymized πŸ˜‰ ) that you could get using git cat-file -p da500aa4f54cbf8f3eb47a1dc2c136715c9197b9 (replace with the hash/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 hash changes (because that's this content described above that is hashed to get the hash of a commit):

  • 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:

  1. You could continue with the command git cat-file -p to continue explore the tree and better understand the way git store data.
  2. to be exact, the content hashed for a commit is the one described above but prefix with the string "commit" (the blob type) and the number of characters (wc -c). Then the file is compressed with zlib and stored in the file (you could see it using command line cat .git/objects/da/500aa4f54cbf8f3eb47a1dc2c136715c9197b9 | perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)')

Upvotes: 8

Rumid
Rumid

Reputation: 1707

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.

Upvotes: 4

larsks
larsks

Reputation: 312580

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.

Upvotes: 10

cst1992
cst1992

Reputation: 3941

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.

Upvotes: 2

Related Questions