在git中合并多个已经被推送的旧提交? [英] Merge multiple old commits in git which have already been pushed?
问题描述
我有一堆我已经推过的旧提交。这些变化很小拼写错误,我想将它们合并在一起,以便我没有100次类似的提交。
例如,这样的:
7af8cee5715e266bf249891cf66f832cf8bb6606错字
53104d19d1e2eba92baf36b8384100c461e417c1错字
9e5afd2afc5d6051f568ce3a441eebf087c9ea46错字
fb8be7c9c54ae2d9ee3ed15971de49104729e4d6确定
48828aaf959ee76d77a74dc35b8455542d0dbb8b固定链接
6387e73bd692024acbb67c1a843348dd6bd01bb8固定连杆
baff3fc602faab37fbd0bf7df9c61ff367121985固定htaccess的问题
2b3e66c19af49030a1da16f9fb7955c4d0f9aa3e固定htaccess的问题
5224690d2a44ec0c4872bedb3b54fb55af75530a固定iplogging问题
注意:这些不是直接在最后一次提交之后,这些都是从几个月前开始的。
是对提交执行交互式重定位并强制将重写的提交推送到服务器端存储库。我建议你保持历史不变,如果提交之后包含合并提交。- 首先,找到第一个提交的SHA想要改变,例如
522469
。 - 检查其他分支是否已合并:
git log --oneline --merges 522469 〜1
。如果这样会生成输出,请不要运行交互式底图。 开始交互式底图:git rebase --interactive 522469〜1
- git会向您显示您可以重新排序的
522469
开始的提交列表。将pick
命令(第一列)更改为squash
或fixup $ c $提交你想要的文件。
- 重新排序和
压缩/ fixup
到你心中的内容,保存文件并关闭编辑器。
- 完成交互式资源重置后,请检查您的构建。作为最后一步,强制推送重写历史到远程资源库: code> git push --force origin HEAD
请注意以下几点:
- 如果存在合并提交,交互式重新分配将移除它们。因此,如果#2生成输出,请不要
git rebase --interactive
。
- 交互式转发仅影响当前分支的提交(即可通过
HEAD
来访问)。任何基于编辑器窗口中任何提交的分支(git branch --contains 522469
)都需要重新编写它们的重写对象。因此,可能会有比上面显示的更多的工作。检查git rebase --onto
。 - 将来,当您发现需要更改/修复最近的提交时,使用
git commit --amend
来重写HEAD
提交,而不是创建一个新的提交。另一种选择是git rebase --interactive
提交的次数较少(例如,不是来自几个月前,而是说2个小时前,HEAD〜10
),这可能会减少潜在的冲突。
I have a bunch of old commits which I have already pushed. These are small changes 'e.g. typos', and I want to merge them together so that I don't have 100s of similar commits.
e.g. this:
7af8cee5715e266bf249891cf66f832cf8bb6606 typo 53104d19d1e2eba92baf36b8384100c461e417c1 typo 9e5afd2afc5d6051f568ce3a441eebf087c9ea46 typo fb8be7c9c54ae2d9ee3ed15971de49104729e4d6 ok 48828aaf959ee76d77a74dc35b8455542d0dbb8b fixed links 6387e73bd692024acbb67c1a843348dd6bd01bb8 fixed link baff3fc602faab37fbd0bf7df9c61ff367121985 fixed htaccess issue 2b3e66c19af49030a1da16f9fb7955c4d0f9aa3e fixed htaccess issue 5224690d2a44ec0c4872bedb3b54fb55af75530a fixed iplogging issue
Note: These are not directly behind the last commit, these are from months ago.
Your only option is to perform an interactive rebase on the commits and force-push the rewritten commits to the server-side repository. I recommend you keep the history as-is if the commits since then contain merge commits.
- First, find the SHA of the first commit you want to change, e.g.
522469
. - Check if other branches have been merged since:
git log --oneline --merges 522469~1
. If this generates output, do not run the interactive rebase. - Start the interactive rebase:
git rebase --interactive 522469~1
- git will present you the list of commits starting at
522469
that you may reorder. Change thepick
command (first column) to eithersquash
orfixup
for commits you want to meld. - Reorder and
squash/fixup
to your heart's content, save the file and close the editor. - After the interactive rebase is done, check your build.
- As the last step, force-push rewritten history to the remote repository:
git push --force origin HEAD
Some things to keep in mind:
- If there are merge commits, the interactive rebase will remove them. So please don't
git rebase --interactive
if #2 generated output. - The interactive rebase only affects the current branches' commits (i.e. reachable by
HEAD
). Any branches based off of any commits in the editor window (git branch --contains 522469
) will need to be rebased off their rewritten counterparts. So there might be more work involved than displayed above. Checkgit rebase --onto
. - In the future, when you find that you need to change/fix the most recent commit, use
git commit --amend
to rewrite theHEAD
commit instead of creating a new commit. Another option is togit rebase --interactive
with less commits (e.g. not from months ago but say 2 hours ago,HEAD~10
) which might present less potential for conflicts.
这篇关于在git中合并多个已经被推送的旧提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!