Git:使用 git rebase 更改已推送的提交消息

2024-02-29

我们正在开发一个有多个分支的 git 存储库。如果所有提交都以功能 ID 作为提交评论的开头(该评论必须得到公司针对该项目的批准),则系统仅允许将更改部署到实时系统上。

我们在一次提交中有一个拼写错误,该提交超过了过去的 100 次提交(有几个人在处理此存储库),这导致功能 ID 错误(因此未批准用于此项目)。因此,由于这一提交,现在我们无法将更改推送到实时系统中。

因此我需要更改这一提交的提交消息来解决我的问题。

我研究并发现了这个解决方案 https://stackoverflow.com/questions/5032374/accidentally-pushed-commit-change-git-commit-message using git rebase。我知道我会在这里更改 git 历史记录,并且我知道其中的含义。到目前为止就这样就好了。现在我的问题是(据我理解),git rebase 获取所有约 140 次提交,从历史记录中删除它们并尝试再次应用它们。如果您的提交中没有任何合并,这很有效。 但是我们确实有很多合并。有许多合并冲突,但都已解决(显然)。

但是当 rebase 完成后,关于如何解决冲突的信息似乎丢失了,git 要求我再次合并所有内容。这不是一个可行的选择,因为重新合并约 140 次提交将需要大约四个星期或更长时间(而且有关如何合并的信息可能不再可用)。

所以我们需要让 git 使用 repo 中给出的信息自动解决这些冲突。

我不知道该怎么做。我读到了关于git rerere命令 (see here https://git-scm.com/blog/2010/03/08/rerere.html),但据我了解,我应该在解决所有冲突之前启用该选项,并且在这样做时,git 会记录必要的信息供以后使用。

我可以选择哪些选项来重命名这一提交?看起来像是一个简单的任务,但我没有想法。

非常感谢您的帮助!


您可以使用git amend更改提交消息。然后替换你的current commit with new commit在所有分支机构。

$ git branch backup                                 # backup your branch just for safety

$ git checkout <commit-hash>                        # checkout the commit trying to modify
$ git commit --amend -m 'Change message here'       # change the author name and mail

$ git log                                           # copy the new-commit-hash
$ git replace <old-commit-hash> <new-commit-hash>   # replace the old commit by new one
$ git filter-branch -- --all ^<old-commit-hash>     # note '^' before hash, rewrite all futures commits based on the replacement                   

$ git replace -d <old-commit-hash>                  # remove the replacement for cleanliness 

$ git checkout <branch>                             # go to branch HEAD and see if the commit is changed perfectly

$ git push -f origin HEAD                           # force push as history is changed.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Git:使用 git rebase 更改已推送的提交消息 的相关文章

随机推荐