git 挑选一个提交并保留原始 SHA 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14676715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:25:23  来源:igfitidea点击:

cherry-pick a commit and keep original SHA code

gitcommitshacherry-pick

提问by blameless75

I would like to cherry-pick a commit on a fetched remote while keeping it's original SHA commit code (my current branch is based on this remote which I resetted to a previous state).

我想在获取的遥控器上挑选一个提交,同时保留它的原始 SHA 提交代码(我当前的分支基于我重置为以前状态的这个遥控器)。

回答by Marco Leogrande

A git SHA hash is computed from different pieces of information:

git SHA 哈希是根据不同的信息计算得出的:

  1. The treeit refers to; basically, the current content of the repository in the branch in which the commit appears.
  2. The SHA of the parent commit(s).
  3. The commit message.
  4. The author information: name, email and timestamp.
  5. The committer information: name, email and timestamp.
  1. 它所指的;基本上,提交出现的分支中存储库的当前内容。
  2. 父提交的 SHA 。
  3. 提交信息
  4. 作者信息:姓名,电子邮件和时间戳。
  5. 提交者信息:姓名,电子邮件和时间戳。

Even if you edit a cherry-picked commit so that the tree, the commit message, the author and committer information are exactly the same, the SHA of the parent commit (or commits, if dealing with merge commits) will be always different. So, you will not be able to generate the same SHA hash after a cherry-pick (unless you find a SHA collision ;) ).

即使你编辑了一个精心挑选的提交,使得树、提交消息、作者和提交者信息完全相同,父提交(或提交,如果处理合并提交)的 SHA 将始终不同。因此,您将无法在挑选后生成相同的 SHA 哈希(除非您发现 SHA 冲突 ;) )。

回答by eis

The SHA commit hash is made of from the state of the repository, using the whole history up to the point of the commit (branches not included). This means that you cannot keep the original hash on cherry-picking unless the whole history is the same, and in that case cherry-picking would make no sense.

SHA 提交哈希由存储库的状态组成,使用到提交点的整个历史记录(不包括分支)。这意味着除非整个历史都相同,否则您不能在樱桃采摘时保留原始哈希,在这种情况下,樱桃采摘将毫无意义。

回答by siegi

According to your comments to other answers I think you simply want to reset to some remote commit. You can use git reset --hard <SHA>to do this. WARNING: This will discard all of your (uncommitted) changesin the working directory and all commits you did in this branch will no longer be accessible.

根据您对其他答案的评论,我认为您只是想重置为某些远程提交。您可以使用它git reset --hard <SHA>来执行此操作。警告:这将丢弃您在工作目录中的所有(未提交的)更改,并且您在此分支中所做的所有提交将不再可访问。

If this is not what you want (or you are not sure) please describe more clearly what you did and what you want to do or what you are trying to accomplish.

如果这不是你想要的(或者你不确定),请更清楚地描述你做了什么,你想做什么,或者你想要完成什么。

回答by Dustin Oprea

Go into interactive rebase ("git rebase -i") and paste a new entry at the end with the exact revision you want to prepend to your HEAD.

进入交互式 rebase ("git rebase -i") 并在末尾粘贴一个新条目,其中包含您想要添加到 HEAD 的确切修订版本。

Example:

例子:

Open an interactive rebase session:

打开一个交互式 rebase 会话:

$ git rebase -i HEAD~4

The screen now shows [something like] this:

屏幕现在显示[类似]:

pick efdd0ece Linked how to make a pull requests in README
pick 790a3be8 adjust pytest pins to fix testing infra
pick 5bb90d8f drop 3.4 support
pick 839dc8ba v2.22.0
pick b97fb61a Print the type of the password instead of the password itself

Your current HEAD is the last entry. Add a new entry to the bottom (just "pick" and your revision; no description necessary) with the exact revision that you want to prepend:

您当前的 HEAD 是最后一个条目。在底部添加一个新条目(只需“选择”和您的修订版本;无需描述),其中包含您想要添加的确切修订版本:

pick efdd0ece Linked how to make a pull requests in README
pick 790a3be8 adjust pytest pins to fix testing infra
pick 5bb90d8f drop 3.4 support
pick 839dc8ba v2.22.0
pick b97fb61a Print the type of the password instead of the password itself
pick 2a173c2a6491aae0772640ba7946a08315d18eb8

Save and close. That exact revision will now be your HEAD:

保存并关闭。那个确切的修订现在将是您的 HEAD:

$ git log --oneline | head -n 6
2a173c2a Some commit
b97fb61a Print the type of the password instead of the password itself
839dc8ba v2.22.0
5bb90d8f drop 3.4 support
790a3be8 adjust pytest pins to fix testing infra
efdd0ece Linked how to make a pull requests in README

As mentioned in other answers, you still have to follow the rules. This works in only the very narrow case where you have the exact same branch, parents, and committer (such as with a code-review-centric process where you have a bunch of commits queued up somewhere, where the developers can push them up and bring them down without necessarily submitting them into the repository first); really only when the timestamps are the only thing that might have changed. In this case, you can force the identical revision in order to force the timestamps to be unchanged.

正如其他答案中提到的,您仍然必须遵守规则。这仅适用于您拥有完全相同的分支、父级和提交者的非常狭窄的情况(例如在以代码为中心的过程中,您有一堆提交在某处排队,开发人员可以将它们推送并将它们删除,而不必先将它们提交到存储库中);真的只有当时间戳是唯一可能改变的东西时。在这种情况下,您可以强制进行相同的修订以强制时间戳不变。

In most other cases, the parent will usually be different, and, this alone, means that your dream of forcing a certain revision will be dead. Git will always fix-up the revision to be correct if any of the non-timestamp factors are different.

在大多数其他情况下,父母通常会有所不同,仅此一项,就意味着您强制进行某个修订的梦想将破灭。如果任何非时间戳因素不同,Git 将始终修复修订版以使其正确。