在推送 Git 之前组合多个提交 [英] Combining multiple commits before pushing in Git

查看:33
本文介绍了在推送 Git 之前组合多个提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的本地存储库中有一堆主题相似的提交.我想在推送到远程之前将它们组合成一个提交.我该怎么做?我认为 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.

推荐答案

你想要做的在 git 中被称为squashing".执行此操作时有很多选项(太多?)但如果您只想将所有未推送的提交合并为一个提交,请执行以下操作:

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

这将打开您的文本编辑器(-i 用于交互式"),其中包含一个如下所示的文件:

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

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

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.

瞧!谷歌搜索git squashing"会给你所有其他可用选项的解释.

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

这篇关于在推送 Git 之前组合多个提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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