使用 Git 压缩我最后的 X 提交 [英] Squash my last X commits together using Git

查看:25
本文介绍了使用 Git 压缩我最后的 X 提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Git 将我最后的 X 次提交合并为一次提交?

解决方案

你可以使用 git merge --squash 来解决这个问题,比 git rebase -i 稍微优雅一些代码>.假设您在 master 上,并且想要将最后 12 个提交压缩为一个.

警告:首先确保你提交了你的工作——检查 git status 是否干净(因为 git reset --hard 将丢弃已暂存和未暂存的更改)

那么:

# 将当前分支重置为最后 12 个之前的提交:git reset --hard HEAD~12# HEAD@{1} 是分支在上一个命令之前的位置.# 此命令将索引的状态设置为就像它一样# 从那个提交合并之后:git merge --squash HEAD@{1}# 提交那些压扁的更改.提交消息将很有帮助# 预先填充所有压缩提交的提交消息:提交

git merge 文档 更详细地描述了 --squash 选项.

<小时>

更新:与更简单的 git reset --soft HEAD~12 && 相比,此方法唯一真正的优势git commit 由 Chris Johnsen 在 他的回答 是你得到的提交消息预先填充了你正在压缩的每条提交消息.

How can I squash my last X commits together into one commit using Git?

解决方案

You can use git merge --squash for this, which is slightly more elegant than git rebase -i. Suppose you're on master and you want to squash the last 12 commits into one.

WARNING: First make sure you commit your work—check that git status is clean (since git reset --hard will throw away staged and unstaged changes)

Then:

# Reset the current branch to the commit just before the last 12:
git reset --hard HEAD~12

# HEAD@{1} is where the branch was just before the previous command.
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}

# Commit those squashed changes.  The commit message will be helpfully
# prepopulated with the commit messages of all the squashed commits:
git commit

The documentation for git merge describes the --squash option in more detail.


Update: the only real advantage of this method over the simpler git reset --soft HEAD~12 && git commit suggested by Chris Johnsen in his answer is that you get the commit message prepopulated with every commit message that you're squashing.

这篇关于使用 Git 压缩我最后的 X 提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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