在推送 Git 之前合并多个提交

发布于 2024-11-27 19:38:08 字数 96 浏览 5 评论 0原文

我的本地存储库上有一堆主题相似的提交。我想在推送到远程之前将它们组合成一个提交。我该怎么做?我认为 rebase 可以做到这一点,但我无法理解这些文档。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

ゝ杯具 2024-12-04 19:38:08

你想要做的事情在 git 中被称为“挤压”。执行此操作时有很多选项(太多?),但如果您只想将所有未推送的提交合并到单个提交中,请执行以下操作:

git rebase -i origin/master

这将打开您的文本编辑器(-i< /code> 代表“交互”),文件如下所示:

pick 16b5fcc Code in, tests not passing
pick c964dea Getting closer
pick 06cf8ee Something changed
pick 396b4a3 Tests pass
pick 9be7fdb Better comments
pick 7dba9cb All done

将所有 pick 更改为 squash (或 s),除了第一个:

pick 16b5fcc Code in, tests not passing
squash c964dea Getting closer
squash 06cf8ee Something changed
squash 396b4a3 Tests pass
squash 9be7fdb Better comments
squash 7dba9cb All done

保存文件并退出编辑器。然后,另一个文本编辑器将打开,让您将所有提交中的提交消息合并为一个大提交消息。

瞧!谷歌搜索“git scraping”将为您提供所有其他可用选项的解释。

What you want to do is referred to as "squashing" in git. There are lots of options when you're doing this (too many?) but if you just want to merge all of your unpushed commits into a single commit, do this:

git rebase -i origin/master

This will bring up your text editor (-i is for "interactive") with a file that looks like this:

pick 16b5fcc Code in, tests not passing
pick c964dea Getting closer
pick 06cf8ee Something changed
pick 396b4a3 Tests pass
pick 9be7fdb Better comments
pick 7dba9cb All done

Change all the pick to squash (or s) except the first one:

pick 16b5fcc Code in, tests not passing
squash c964dea Getting closer
squash 06cf8ee Something changed
squash 396b4a3 Tests pass
squash 9be7fdb Better comments
squash 7dba9cb All done

Save your file and exit your editor. Then another text editor will open to let you combine the commit messages from all of the commits into one big commit message.

Voila! Googling "git squashing" will give you explanations of all the other options available.

倦话 2024-12-04 19:38:08

如果您有很多提交,并且只想压缩最后 X 个提交,请找到要开始压缩的提交的提交 ID,

git rebase -i <that_commit_id>

然后执行 leopd 的回答中所述的操作,更改所有除了第一个之外,picksquash es。

示例

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

您可以执行此操作(写入提交数):

git rebase --interactive HEAD~[7]

或者这样(写下您不想想要压缩的最后一次提交的哈希值):

git rebase --interactive 6394dc

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

git rebase -i <that_commit_id>

Then proceed as described in leopd's answer, changing all the picks to squashes except the first one.

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
暖风昔人 2024-12-04 19:38:08

这里有很多可行的答案,但我发现这是最简单的。此命令将打开一个编辑器,您可以在其中将 pick 替换为 squash ,以便将它们删除/合并到一个

git rebase -i HEAD~4

编辑器中,其中 4 是您想要压缩为一次的提交数量。 此处也对此进行了解释。

如果您希望压缩提交(其中还包括第一个提交),请使用此命令

git rebase -i --root

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

git rebase -i HEAD~4

where, 4 is the number of commits you want to squash into one. This is explained here as well.

If you are looking to squash the commits which also includes the very first commit, then use this command

git rebase -i --root
冰火雁神 2024-12-04 19:38:08

您可以使用 git rebase -i 来执行此操作,传入要用作“根”的修订版:

git rebase -i origin/master

将打开一个编辑器窗口,显示您在上次提交后所做的所有提交起源/主。您可以拒绝提交、将提交压缩为单个提交或编辑以前的提交。

有一些资源可能可以更好地解释这一点,并显示一些其他示例:

http://book.git -scm.com/4_interactive_rebasing.html

http://gitready.com/advanced/2009/02/10/squashing-commits- with-rebase.html

是我能找到的前两个好页面。

You can do this with git rebase -i, passing in the revision that you want to use as the 'root':

git rebase -i origin/master

will open an editor window showing all of the commits you have made after the last commit in origin/master. You can reject commits, squash commits into a single commit, or edit previous commits.

There are a few resources that can probably explain this in a better way, and show some other examples:

http://book.git-scm.com/4_interactive_rebasing.html

and

http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html

are the first two good pages I could find.

海之角 2024-12-04 19:38:08

我想出了

#!/bin/sh

message=`git log --format=%B origin..HEAD | sort | uniq | grep -v '^

合并、排序、统一和删除提交消息中的空行。我用它来对 github wiki 进行本地更改(使用 gollum)

` git reset --soft origin git commit -m "$message"

合并、排序、统一和删除提交消息中的空行。我用它来对 github wiki 进行本地更改(使用 gollum)

I came up with

#!/bin/sh

message=`git log --format=%B origin..HEAD | sort | uniq | grep -v '^

Combines, sorts, unifies and remove empty lines from the commit message. I use this for local changes to a github wiki (using gollum)

` 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)

冷了相思 2024-12-04 19:38:08

您可以使用交互式变基压缩(加入)提交。有一个非常漂亮的 YouTube 视频,展示了如何在命令行或使用 SmartGit 执行此操作:

  • < a href="https://www.youtube.com/watch?v=qi_QAFrmHJM" rel="noreferrer">https://www.youtube.com/watch?v=qi_QAFrmHJM

如果您已经是然后,SmartGit 用户可以选择所有传出提交(通过按住 Ctrl 键)并打开上下文菜单(右键单击)来压缩您的提交。

非常舒服:

“在此处输入图像描述”"

Atlassian 还提供了一个非常好的教程,其中展示了如何作品:

  • <一href="https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase-i" rel="noreferrer">https://www.atlassian.com/git/tutorials/rewriting-history /git-rebase-i

You can squash (join) commits with an Interactive Rebase. There is a pretty nice YouTube video which shows how to do this on the command line or with 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.

It's very comfortable:

enter image description here

There is also a very nice tutorial from Atlassian which shows how it works:

寂寞花火° 2024-12-04 19:38:08

压缩多个push的方式是(也许您将许多提交推送到自己的分支,现在您希望执行拉取请求,并且不想让它们混乱有许多您已经推送的提交)。我这样做的方式(据我所知,没有其他更简单的选择)。

  1. 为了 squash 创建新分支(来自您希望拉取请求的原始分支的分支)。
  2. 推送新创建的分支。
  3. 将分支与提交(已推送)合并到新分支。
  4. 重新调整新分支和南瓜的基础。
  5. 推新分支。
  6. 为现在具有单个提交的新分支创建新的拉取请求。

示例:

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

您现在可以将请求拉入 from_branch_you_wish_to_pull_request_to

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

清君侧 2024-12-04 19:38:08

您可能想使用 交互式变基,该链接中有详细描述。

如果搜索“git rebase Interactive”,您可以找到其他好的资源。

You probably want to use Interactive Rebasing, which is described in detail in that link.

You can find other good resources if you search for "git rebase interactive".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文