简体   繁体   English

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

[英]Git: Change already pushed commit message using git rebase

we are working on a git repo with several branches. 我们正在与多个分支合作的git repo。 The system only allows to deploy changes onto the live system, if all commits have feature-ids as the beginning of the commit comment that must be approved by the company for this project. 如果所有提交都具有功能部件标识作为提交注释的开头,并且该注释必须由公司为此项目批准,则系统仅允许将更改部署到实时系统上。

We have a typo in one commit that is more than 100 commits back in the past (several people working on this repo), that leads to the feature-id being wrong (and therefore not approved for this project). 我们在一次提交中有一个错别字,过去有100多次提交(很多人正在做这个仓库),这导致功能ID错误(因此未获批准用于此项目)。 So now we are not able to push our changes into the live system because of this one commit. 因此,由于这一提交,现在我们无法将更改推送到实时系统中。

Therefore I need to change the commit message of this one commit to solve my problem. 因此,我需要更改这一提交的提交消息以解决我的问题。

I researched and came across this solution using git rebase . 我研究并使用git rebase遇到了这个解决方案 I'm aware of the fact that I would change git history here and I'm aware of the implications. 我知道我将在此处更改git历史记录的事实,并且知道其中的含义。 That would be fine so far. 到目前为止还可以。 Now my problem is that (to my understanding), git rebase takes all the ~140 commits, deletes them from the history and tries to apply them again. 现在我的问题是(据我了解),git rebase接受了所有〜140个提交,将它们从历史记录中删除,然后尝试再次应用它们。 This works well if you don't have any merges in your commits. 如果您的提交中没有任何合并,则此方法效果很好。 But boy do we have a lot of merges. 但是男孩,我们有很多合并。 There were many merge conflicts, that have all been resolved (obviously). 有很多合并冲突,所有冲突都已解决(很明显)。

But when the rebase is done, the information on how to resolve the conflicts seems to be lost and git asks me to merge everything again. 但是,当完成了基础调整后,有关如何解决冲突的信息似乎丢失了,git要求我再次合并所有内容。 That is not a viable option because re-doing merges for ~140 commits would take about four weeks or more (also information on how to merge might not be available any more). 这不是一个可行的选择,因为要重新合并约140次提交将花费大约四个星期或更长时间(有关合并的信息可能不再可用)。

So we need to make git resolve these conflicts automatically, using the information given in the repo. 因此,我们需要使用仓库中提供的信息,使git自动解决这些冲突。

I can't figure out how to do that. 我不知道该怎么做。 I read about the git rerere command ( see here ), but to my understanding I should have enabled that option before resolving all the conflicts and while doing so, git would have recorded the necessary information for later. 我阅读了有关git rerere命令的信息( 请参阅此处 ),但据我所知,我应该在解决所有冲突之前启用该选项,而这样做的话,git会记录必要的信息供以后使用。

What are my options to get this one commit renamed? 我有什么选择可以重命名这一提交? Seems like a simple tasks, but I'm out of ideas. 看起来像一个简单的任务,但是我没有主意。

Thanks a lot for any help! 非常感谢您的帮助!

You can use git amend to change the commit message. 您可以使用git amend更改提交消息。 Then replace your current commit with new commit in all branch. 然后在所有分支中用new commit替换您current 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.

Try using --preserve-merges on your rebase 尝试在基础上使用--preserve- merges

git rebase --preserve-merges HEAD~100

It should try to recreate the merge commits and might help you out. 它应该尝试重新创建合并提交,并可能对您有所帮助。 If using with -i just make sure you don't change the order of the commits or it might mess things up. 如果与-i使用,请确保您不更改提交的顺序,否则可能会弄乱事情。

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

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