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


当前回答

让我们说 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 merge --squash 为此,这比 git rebase -i 略有优雅。

警告: 首先,请确保您完成工作 - 检查 git 状态是干净的(因为 git 重新设置 - 硬将抛弃错误和错误的变化)

然后:

# 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

git merge 的文档详细描述了 --squash 选项。


更新:这个方法的唯一真正优点,比克里斯·约翰森(Chris Johnsen)在他的回答中提出的更简单的吉特重新设置 --soft HEAD~12 &&吉特承诺是,你会得到承诺的消息,以每一个承诺的消息,你正在破坏。

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

步骤 0: git log

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

此分類上一篇

步骤一:转动

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

$ git rebase -i 2d23ea524936e612fae1ac63c95b705db44d937d

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

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

此分類上一篇

步骤 3: 调整消息(s)

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

此分類上一篇

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

如果你想写下新的承诺消息从滑板,这就足够了:

git reset --soft HEAD~3 &&
git commit

如果你想开始编辑新的承诺消息与现有承诺消息的折叠(即类似于什么一个 pick/squash/squash/.../squash git rebase -i 指示列表会开始你),那么你需要提取这些消息,并将它们转移到 git commit:

git reset --soft HEAD~3 && 
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

两种方法都将过去三项承诺分成一个单一的新承诺,相同的方式。 软重定义只会重新点头到最后一项承诺,你不想分解。 无论指数还是工作树都不会被软重定义所触摸,让指数在你新承诺所需的状态(即它已经有所有从你即将“扔掉”的承诺的变化)。

簡單的單線,總是工作,因為你現在在你想要的分支,主是它起源的分支,最新的承諾包含承諾訊息和作者你想使用:

git reset --soft $(git merge-base HEAD master) && git commit --reuse-message=HEAD@{1}

要将最后10个承诺分为1个单一承诺:

git reset --soft HEAD~10 && git commit -m "squashed commit"

如果你也想更新远程分支与破碎的承诺:

git push -f