在推送Git之前合并多个提交[重复]

本文提供了多种在推送Git之前合并类似主题的多个提交的方法,包括使用git squash、交互式重置(Interactive Rebase)、SmartGit等工具。给出了详细的操作步骤和示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Combining multiple commits before pushing in Git [duplicate]

This question already has an answer here: 这个问题已经在这里有了答案:

I have a bunch of commits on my local repository which are thematically similar. 我在本地存储库上有一堆主题上相似的提交。 I'd like to combine them into a single commit before pushing up to a remote. 我想在将其推送到远程服务器之前将它们组合为一个提交。 How do I do it? 我该怎么做? I think rebase does this, but I can't make sense of the docs. 我认为rebase做到这一点,但是我无法理解这些文档。


#1楼

参考:https://stackoom.com/question/T62q/在推送Git之前合并多个提交-重复


#2楼

If you have lots of commits and you only want to squash the last X commits, find the commit ID of the commit from which you want to start squashing and do 如果提交次数很多 ,而您只想压榨最近的X次提交,请找到要开始压榨的提交的提交ID,然后执行

git rebase -i <that_commit_id>

Then proceed as described in leopd's answer, changing all the pick s to squash es except the first one. 然后按照leopd的答案所述进行操作,将除第一个pick以外的所有pick s改为squash

Example : 范例

871adf OK, feature Z is fully implemented      --- newer commit --┐
0c3317 Whoops, not yet...                                         |
87871a I'm ready!                                                 |
643d0e Code cleanup                                               |-- Join these into one
afb581 Fix this and that                                          |
4e9baa Cool implementation                                        |
d94e78 Prepare the workbench for feature Z     -------------------┘
6394dc Feature Y                               --- older commit

You can either do this (write the number of commits): 您可以执行以下操作(写入提交次数):

git rebase --interactive HEAD~[7]

Or this (write the hash of the last commit you don't want to squash): 或者这(写最后的哈希提交你不想壁球):

git rebase --interactive 6394dc

#3楼

I came up with 我想出了

#!/bin/sh

message=`git log --format=%B origin..HEAD | sort | uniq | grep -v '^$'`
git reset --soft origin
git commit -m "$message"

Combines, sorts, unifies and remove empty lines from the commit message. 合并,排序,统一和删除提交消息中的空行。 I use this for local changes to a github wiki (using gollum) 我用它来对github wiki进行本地更改(使用gollum)


#4楼

You can squash (join) commits with an Interactive Rebase . 您可以使用Interactive Rebase压缩 (加入)提交。 There is a pretty nice YouTube video which shows how to do this on the command line or with SmartGit : YouTube上有一个非常漂亮的视频,展示了如何在命令行或SmartGit上执行此操作:

If you are already a SmartGit user then you can select all your outgoing commits (by holding down the Ctrl key) and open the context menu (right click) to squash your commits. 如果您已经是SmartGit用户,则可以选择所有传出的提交(通过按住Ctrl键),然后打开上下文菜单(右键单击)以压缩提交。

It's very comfortable: 很舒服:

在此处输入图片说明

There is also a very nice tutorial from Atlassian which shows how it works: Atlassian还有一个很好的教程,展示了它的工作原理:


#5楼

And my way of squashing multiple push is (perhaps you pushed to your own branch many commits and now you wish to do a pull request and you don't want to clutter them with many commits which you have already pushed). squashing多次push是(也许您将许多提交推送到自己的分支,而现在您希望发出一个pull请求,而又不想让您已经推送的许多提交弄乱它们)。 The way I do that (no other simpler option as far as I can tell is). 我这样做的方式(据我所知,没有其他更简单的选择了)。

  1. Create new branch for the sake of squash (branch from the original branch you wish to pull request to). 为了squash而创建新分支(从您希望将请求拉至的原始分支分支)。
  2. Push the newly created branch. 推送新创建的分支。
  3. Merge branch with commits (already pushed) to new branch. 合并具有提交(已推送)的分支到新分支。
  4. Rebase new branch and squash. 重新设置新的分支和壁球。
  5. Push new branch. 推送新分支。
  6. Create new pull request for new branch which now has single commit. 为现在具有单个提交的新分支创建新的拉取请求。

Example: 例:

git checkout from_branch_you_wish_to_pull_request_to
git checkout -b new_branch_will_have_single_squashed_commit
git push -u new_branch_will_have_single_squashed_commit
git merge older_branch_with_all_those_multiple_commits
git rebase -i (here you squash)
git push origin new_branch_will_have_single_squashed_commit

You can now pull request into from_branch_you_wish_to_pull_request_to 您现在可以将请求拉入from_branch_you_wish_to_pull_request_to


#6楼

There are quite a few working answers here, but I found this the easiest. 这里有很多可行的答案,但是我发现这是最简单的。 This command will open up an editor, where you can just replace pick with squash in order to remove/merge them into one 此命令将打开一个编辑器,您可以在其中将squash替换为pick ,以将其删除/合并为一个

git rebase -i HEAD~4

where, 4 is the number of commits you want to squash into one. 其中, 4是您要压缩为一的提交数。 This is explained here as well. 也在这里解释。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值