简体   繁体   English

指定Git提交哈希

[英]Specifying Git commit hash

Can I specify Git commit hash, instead of letting Git generate it? 我可以指定Git提交哈希,而不是让Git生成它吗?

Is it even possible? 它甚至可能吗? I didn't find any way in Git documentation or online. 我没有在Git文档或在线中找到任何方法。


For some background: I'm recreating a new Git public repository from a private one programmatically. 对于某些背景:我正在以编程方式从私有库重新创建一个新的Git公共存储库。 I need to make lot of changes to each commit to remove confidential information. 我需要对每个提交进行大量更改以删除机密信息。 But I'd like to preserve commit IDs. 但我想保留提交ID。

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. git commit hash是一个加密校验和,它是根据存储库的状态计算出来的,包括存储库中所有文件的哈希值,前一次提交的哈希值,当前日期和时间等。

It is not possible to specify this manually. 无法手动指定此项。

More more information, see this question . 更多信息,请参阅此问题

Content of a commit (anonymized ;) ) that you could get using git cat-file -p da500aa4f54cbf8f3eb47a1dc2c136715c9197b9 (replace with the sha1 of one of your commits): 您可以使用git cat-file -p da500aa4f54cbf8f3eb47a1dc2c136715c9197b9获取的提交内容(匿名;))(替换为您的某个提交的sha1):

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. 如果其中一个数据发生更改,则所有sha1都会更改。

  • The tree is the sha1 calculated from the content of the files and directories content. tree是根据文件和目录内容计算的sha1。

  • Parent is the parent commit hash. Parent是父提交哈希。

  • Notice that there is also dates inside, so if you do exactly the same commit but at different moment, the sha1 will change also 请注意,内部也有日期,因此如果您在不同时刻执行完全相同的提交,则sha1也会更改

PS: You could continue with the command git cat-file -p to continue explore the tree and better understand the way git store data. PS:您可以继续使用命令git cat-file -p继续探索tree并更好地理解git存储数据的方式。

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) 先前提交的ID
  • ... ...

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 这是一个例子: 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. 这是一个脚本,它进行提交,然后正在寻找一个特定的日期来使git hash成为你正在寻找的那个,然后用这个日期进行修改。

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). 如果您对提交进行更改,则ID将会更改。(它们取决于提交内容本身,以及其中的更改)。

So, no. 所以不行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM