Git:在 master 上压缩提交的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8213926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:22:41  来源:igfitidea点击:

Git: simplest way of squashing commits on master

gitgithub

提问by Gabe

Possible Duplicate:
How can I squash my last X commits together using git?

可能的重复:
如何使用 git 将我最后的 X 提交压缩在一起?

I have a project hosted on GitHub and I have a local clone. I have a load of small commits that I have already pushed up to GitHub. This has all been done using the default *master branch.

我有一个托管在 GitHub 上的项目,我有一个本地克隆。我有很多小的提交,我已经推送到 GitHub。这一切都是使用默认的 *master 分支完成的。

I got confused between merge --squash and rebase etc... What is the most straightforward way of combining several historial commits in to one commit so that it pushes up to GitHub?

我在合并 --squash 和 rebase 等之间感到困惑......将几个历史提交组合到一个提交中以便它推送到 GitHub 的最直接的方法是什么?

回答by Mark Longair

Before starting, you should make sure that git statusis clean (i.e. there's no output from that command) to avoid losing work. The simplest way to do what you want is probably:

在开始之前,您应该确保它git status是干净的(即该命令没有输出)以避免丢失工作。做你想做的最简单的方法可能是:

git reset --soft <LAST-COMMIT-THAT'S-OK>
git commit -m 'Many squashed commits'
git push --force origin master

You should bear in mind that this is rewriting history (that's why you need the --forceoption to git push) so you should avoid this if anyone else might have cloned or pulled from your repository. For some more alternatives, see this question and its answers:

您应该记住,这是重写历史记录(这就是为什么您需要--force选择git push),因此如果其他人可能已经从您的存储库中克隆或提取,您应该避免这种情况。有关更多选择,请参阅此问题及其答案: