git - 展平分支上的提交

标签 git

是否可以在与主分支 merge 之前将分支上的提交压缩为单个提交?我认为这将是一个相当常见的场景,但也许我没有使用正确的搜索词。

我会更详细地解释这个场景。我经常想在分支中进行更改时进行许多本地提交,以确保我拥有全面的更改历史记录。但是一旦完成分支中的更改,当我 merge 到 main 时,我想将分支上的提交减少到一个,然后将其 merge 到 main。我确实了解 Git 中的提交成本低廉,但在某些情况下,我可能更愿意这样做。

*   merge to main
|\
* | commit 2 on main
* | commit 1 on main
| * commit 2 on branch
| * commit 1 on branch
|/
*   branch from main

看起来像

*   merge to main
|\
* | commit 2 on main
* | commit 1 on main
| * commit on branch (branch commits flattened to one)
|/
*   branch from main

我是 git 的新手。如果我在使用术语时犯了错误,我深表歉意。

最佳答案

我建议学习使用交互式 rebase ,但如果你觉得它太复杂,你可以简单地使用

git reset --soft <diverging-commit>

在不更改索引的情况下撤消所有提交到 fork 点,并且

git commit -s

对所有更改进行一次提交。

关于git - 展平分支上的提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11724666/

相关文章:

linux - Git - 在 Linux 上使用 Linux 风格的行尾,在 Windows 上使用 Windows 风格的行尾

git - 有什么方法可以查看在 github 上访问/下载 repo 的最后/次数?

git - 在我已经进行更改后如何在新分支中进行 git commit?

git - 为什么 git 不断向我抛出 merge 警告消息?

java - 如何使用 Heroku 管理多个 Spring 配置文件

git - 在 Github Actions for PR 中获取 [not] 来自 fork 的目标分支引用

git rebase master 特性给出(重命名/删除)冲突,git rebase -i master 特性没有

xcode - 为什么这些文件不会被 git 忽略?

android - 如何在 Gradle 中编译 fork 库?

eclipse - 如何使用 Eclipse 压缩提交?