git - 我们可以将最新的提交压缩为特定的先前提交吗?

标签 git version-control squash git-squash

例如,如果我们想将 HEAD 处的提交压缩为 HEAD~4 处的提交,我们可以选择通过

  1. 将提交从 HEAD 重置为 HEAD~3
  2. 使用与旧 HEAD 相同的更改构建临时提交(例如,temp)
  3. stash 其他已暂存/未暂存的更改
  4. 用我们的旧HEAD~4压缩temp
  5. pop 我们 stash 的更改
  6. 再次构建提交作为较旧的提交(HEAD~3HEAD)。

还有其他更快更方便的方法吗?

最佳答案

正如 @jthill 所建议的,git 存在一个 autosquash 选项。

例如,就我而言,假设初始情况为,

$ git log --oneline --decorate
ddd4444 (HEAD, my-feature-branch) A fourth commit
ccc3333 A third commit
bbb2222 A second commit
aaa1111 A first commit
9999999 (master) Old stuff on master

我现在将添加我想用 HEAD~4 压缩的最近更改 如:

$ git add .
$ git commit --fixup aaa1111
[my-feature-branch eee5555] fixup! A first commit

所以,我的历史现在看起来像:

$ git log --oneline --decorate
eee5555 (HEAD, my-feature-branch) fixup! A first commit
ddd4444 A fourth commit
ccc3333 A third commit
bbb2222 A second commit
aaa1111 A first commit
9999999 (master) Old stuff on master

现在通过 $ git rebase --interactive --autosquash master 使用 autosquash 进行 rebase ,可自动选择带有 --fixup 提交的提交正确的地方。

在进行 rebase 并与提交消息编辑 merge 后,您将成功进行 rebase !

就像魔术一样! :)

来源:thoughtbot

关于git - 我们可以将最新的提交压缩为特定的先前提交吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37731164/

相关文章:

git - 将我所有的提交压缩为 GitHub pull 请求

git interactive rebase 压缩到下一次提交

git - 使用 'git pull' 与 'git checkout -f' 进行网站部署

git - 适用于 Node.js 的 `.gitignore`

git - 源代码树和 Github 看不到我的所有文件

version-control - 是否有托管化石储存库的网站?

git - 使用 Julia 安装模块

Git 子树 : move subtree to a different directory and pull it

visual-studio - 如何将现有解决方案添加到 Team Foundation Server?