Git:使用git rebase更改已推送的提交消息 [英] Git: Change already pushed commit message using git rebase

查看:271
本文介绍了Git:使用git rebase更改已推送的提交消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在与一些分支机构合作进行git回购。如果所有提交都有特征ID作为必须由公司批准的项目提交注释的开始,则系统仅允许将更改部署到实时系统。



我们在一次提交中有一个错误,超过100次提交(有几个人从事这个回购工作),导致功能ID错误(因此未被批准用于该项目)。因此,现在我们无法将我们的更改推送到实时系统中,因为这是一次提交。



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



我研究过并发现了这个解决方案,使用 git rebase 。我意识到我会在这里改变git的历史,并且我意识到这个影响。这到目前为止是很好的。现在我的问题是(根据我的理解),git rebase需要所有〜140次提交,将它们从历史记录中删除并尝试再次应用它们。如果你的提交中没有任何合并,这很好。
但男孩我们有很多合并。有很多合并冲突,都已经被解决(显然)。



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

<因此,我们需要使用回购中给出的信息自动解决这些冲突。



我无法弄清楚如何做到这一点。我阅读了关于 git rerere 命令(见这里),但根据我的理解,我应该在解决所有冲突之前启用该选项,同时这样做,git会记录必要的信息供以后使用。



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



非常感谢您的帮助!

解决方案

您可以使用 git modify 更改提交消息。然后在所有分支中将当前提交替换为新提交

  $ git分支备份#仅仅为了安全而备份你的分支

$ git checkout< commit-散列> #检查提交尝试修改
$ git commit --amend -m'在此处更改消息'#更改作者姓名和邮件

$ git log#复制new-commit-hash
$ git替换< old-commit-hash> <新提交哈希> #替换旧的提交
$ git filter-branch - --all ^< old-commit-hash> #在hash之前注意'^',根据替换重写所有的期货提交

$ git replace -d< old-commit-hash> #删除替换为清洁

$ git checkout< branch> #去分支HEAD,看看提交是否完全改变

$ git push -f origin HEAD#随着历史记录的改变,强制推送。


we are working on a git repo with several branches. 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). 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. I'm aware of the fact that I would change git history here and I'm aware of the implications. 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. 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. 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).

So we need to make git resolve these conflicts automatically, using the information given in the repo.

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.

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. Then replace your current commit with new commit in all branch.

$ 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.

这篇关于Git:使用git rebase更改已推送的提交消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆