git interactive rebase 压缩到下一次提交

标签 git squash git-squash git-interactive-rebase

在 Git 中,我可以使用交互式 rebase 来重写历史,这很棒,因为在我的功能分支中,我在探索不同的重构和完成它的方法时使用部分工作代码进行了大量提交。

我想在将分支 rebase 或 merge 到 master 之前将很多提交压缩在一起。

一些按照从第一个(顶部)到底部(最后一个)的顺序组成提交

1. Initial commit on feature branch "Automatic coffee maker UI"
2. Add hot chocolate as product
3. Add tea as product. Products are now generic
4. Create in memory data store for adapter tests
5. Cry because I can't get entity framework to create a composite key. Integration tests broken.
6. Implemented composite key!!
7. All tests green and feature done!

假设我想保留提交 3、4 和 7。

使用 rebase 我想“压缩”提交

  • 1 和 2 进入 3。
  • 4 次入住
  • 5和6进入7

理想情况下我会做的交互式 rebase

1. squash
2. squash
3. pick (contains the work of 1 & 2)
4. pick 
5. squash
6. squash
7. pick (contains the work of 5 & 6)

但这是倒退的,因为 squash 将提交与其之前的提交 merge 。我不知道如何让它向前挤压。

我是不是很难相处,我应该接受这行不通(我宁愿让它工作),还是有办法做到这一点?

我正在调用这个命令

git checkout My-feature-branch
git rebase master -i

然后我正在编辑出现的提交列表,并尝试通过保存文件和编辑编辑器来完成它,这通常对我有用。

最佳答案

如果可行,您还需要对提交进行重新排序,以便待保留的提交先于待压缩的提交。

如果这不可行,因为你会遇到不想解决的冲突,那就去做吧

1. pick
2. squash
3. squash
4. pick 
5. pick
6. squash
7. squash

压缩完成后,您可以编辑提交消息以包含您希望最终提交具有的消息。非常简单。 :-)

你甚至可以做到

1. pick
2. fixup
3. squash
4. pick 
5. pick
6. fixup
7. squash

然后我认为应该只启动一次提交消息编辑器,因为使用 fixup 之前的提交消息只是在不启动编辑器的情况下获取。

在提交消息编辑器触发时进行压缩时,您还会收到两个提交消息,一个来自 to-be-squashed-into 和 to-be-squashed 提交,这样您就可以简单地删除您没有的提交消息'想保留。

关于git interactive rebase 压缩到下一次提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44210747/

相关文章:

git - Rebase "fixup"提交到之前的 merge 提交

GitLab SSH key 停止工作

android - git - 使用 Gradle 构建将远程 git 代码 check out 到 Android Studio 导入的项目中

git - 在 SQUASHED 时检查 Git 分支是否已 merge 到 master 中?

git merge : is there a way to force --squash?

git - 压缩推送的 Git 提交的最佳实践是什么

git - 在 GitHub 中将 PR merge +压缩到 master 之后如何同步你的 git dev 分支?

git - 压缩包含 merge 提交的前几十个 git 提交

ruby - 运行 "bundle install"将 Gemfile.lock 中的 "git://"更改为 "https://"

c# - 在 .NET 中使用 Go CD