我如何将最后的N承诺集成到一个承诺中?


当前回答

什么可能真的很方便: 找到你想在顶部滑动的承诺,说d43e15。

现在使用

git reset d43e15
git commit -am 'new commit name'

其他回答

我认为最简单的方式来做到这一点是通过创建一个基于大师的新分支,并进行功能分支的合并。

git checkout master
git checkout -b feature_branch_squashed
git merge --squash feature_branch

然后,你有所有的变化准备好进行。

Anomies 答案很好,但我对此感到不安全,所以我决定添加几张屏幕截图。

步骤 0: git log

查看你在哪里与 git log. 最重要的是,找到第一个 commit 的 commit hash 你不想 squash。

此分類上一篇

步骤一:转动

在我的案例中,执行 git rebase -i(你的哈希):

$ git rebase -i 2d23ea524936e612fae1ac63c95b705db44d937d

步骤2:选择 / 切割你想要的东西

在我的情况下,我想把所有的承诺,这是第一次在时间上。 订单是从第一到最后,所以正如在 git log。 在我的情况下,我想:

此分類上一篇

步骤 3: 调整消息(s)

如果您只选出一个承诺,并解除其余的承诺,您可以调整一个承诺消息:

此分類上一篇

一旦你保存这个(:wq),你已经完成了。

首先,我知道我的功能分支和当前主分支之间的承诺数量。

git checkout master
git rev-list master.. --count

然后,我创建另一个基于我的功能分支的分支,保持我的功能分支无触。

最后,我跑

git checkout my-feature
git checkout -b my-rebased-feature
git checkout master
git checkout my-rebased-feature
git rebase master
git rebase head^x -i
// fixup/pick/rewrite
git push origin my-rebased-feature -f // force, if my-rebased-feature was ever pushed, otherwise no need for -f flag
// make a PR with clean history, delete both my-feature and my-rebased-feature after merge

希望能帮忙,谢谢。

让我们说 n 真的很大. 你不想处理每一个承诺. 你只是想要一个名为 new_feature_branch 的新分支,其中有 1 承诺和所有旧_feature_branch 的变化。

git checkout main 

# will contain changes all changes from old_feature_branch 
git checkout -b new_feature_branch 

# get all changes from old_feature_branch and stage them
for f in $(git --no-pager diff --name-only old_feature_branch ) ; do git checkout fencing -- $f ; done

git commit -m "one commit message"

许多答案是基于Git Rebase命令,但在我的经验中,它对Git初学者来说有点复杂和先进。

假设你想排除最后3个命令,然后以下是步骤:

下载现行承诺ID: 使用 git log -1 --online 并记住当前状态的承诺ID(仅在您与 git reset 做错事的情况下) 返回 3 个承诺: 使用 git reset --soft HEAD~3 您将返回 3 个承诺(并忘记您以前做过这三个承诺) 做一个新的承诺: 现在简单地做 git commit -m <NEW_SINGLE_MESSAGE> 将自动结合 t

如果与 git reset 发生错误,您可以通过 git reset --soft <ORIGINAL_COMMIT> 返回原始状态。