简体   繁体   English

如何将已推送的提交从 X 压缩到 Y?

[英]How do I squash Commits from X to Y that have already been pushed?

I would like to squash commits X to Y (let's say the 2nd to last commit to the 5th last commit) that have already been pushed to my remote repository.我想压缩已经推送到我的远程存储库的 X 到 Y 的提交(假设是倒数第二次提交到最后一次提交的第 5 次提交)。 I see answers on Stackoverflow for squashing X last commits, but I am not interested in squashing the last X commits, but commits behind the last few commits.我在 Stackoverflow 上看到了关于压缩 X 次最后提交的答案,但我对压缩最后 X 次提交不感兴趣,而是在最后几次提交之后提交。

What you want is an "interactive rebase" ( git rebase -i ) which you can use to rebase a branch onto itself.您想要的是“交互式变基”( git rebase -i ),您可以使用它来将分支变基到自身上。 The command will provide you with a list of commits that you've specified in your range which you can then ask to be edited, squashed together, left alone - For instance in the middle of a range of commits.该命令将为您提供您在范围内指定的提交列表,然后您可以要求对其进行编辑,压缩在一起,单独放置 - 例如在提交范围的中间。

https://git-scm.com/docs/git-rebase https://git-scm.com/docs/git-rebase

-i --interactive Make a list of the commits which are about to be rebased. -i --interactive 列出将要重新定位的提交。 Let the user edit that list before rebasing.让用户在变基之前编辑该列表。 This mode can also be used to split commits (see SPLITTING COMMITS below).此模式还可用于拆分提交(请参阅下面的拆分提交)。

Example:例子:

15:49 $ git log --oneline -5
ffdad47 2020-10-12 (HEAD -> master) fourth commit [Martin KJELDSEN]
60b7363 2020-10-06 third commit [Martin KJELDSEN]
a72eea4 2019-03-28 second commit [Martin KJELDSEN]
b9b64b8 2019-03-28 first commit [Martin KJELDSEN]

Then run the rebase command to rebase all commits onto b9b64b8 .然后运行 ​​rebase 命令将所有提交b9b64b8

git rebase -i b9b64b8

Git presents me with the following list in an editor and i'll select to squash one of the middle commits ( fixup = squash and use existing commit message. squash = let the user merge all the commit messages together before proceeding with rebase). Git 在编辑器中向我显示以下列表,我将选择压缩其中一个中间提交( fixup = squash 并使用现有的提交消息squash = 让用户在继续 rebase 之前将所有提交消息合并在一起)。

pick a72eea4 second commit                                                              
fixup 60b7363 third commit
<There could be more commits here>
pick fourth commit
                                                                             
# Rebase b9b64b8..ffdad47 onto b9b64b8 (3 commands)                              
#                                                                                
# Commands:                                                                      
# p, pick <commit> = use commit                                                  
# r, reword <commit> = use commit, but edit the commit message                   
# e, edit <commit> = use commit, but stop for amending                           
# s, squash <commit> = use commit, but meld into previous commit                 
# f, fixup <commit> = like "squash", but discard this commit's log message       
# x, exec <command> = run command (the rest of the line) using shell             
# b, break = stop here (continue rebase later with 'git rebase --continue')      
# d, drop <commit> = remove commit                                               
# l, label <label> = label current HEAD with a name                              
# t, reset <label> = reset HEAD to a label                                       
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]                     
# .       create a merge commit using the original merge commit's                
# .       message (or the oneline, if no original merge commit was               
# .       specified). Use -c <commit> to reword the commit message.              
#                                                                                

When saving this configuration and continuing with the rebase you'll have rewritten history according to your rebase -i configuration保存此配置并继续 rebase 时,您将根据rebase -i配置重写历史记录

I have found it easy to use the reverse-commit workflow.我发现使用反向提交工作流程很容易。 To execute that I start with the most recent commit I want to back out and work my way down the commit history using these commands (or your favorite GUI tool):为了执行它,我从最近的提交开始,我想退出并使用这些命令(或您最喜欢的 GUI 工具)沿着提交历史记录:

git revert [commit_hash]

There are some useful flags such as --no-edit and --no-commit depending on how you like to manage your commit messages and where you are in your commit index.有一些有用的标志,例如--no-edit--no-commit取决于您喜欢如何管理提交消息以及您在提交索引中的位置。

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

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