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


当前回答

git rebase -i HEAD^^

在哪里 ^'s 的数量是 X

(此分類上一篇: 最後兩個命令)

其他回答

使用 git rebase -i <after-this-commit> 并在第二次和随后的命令中取代“squash”或“fixup”,如手册中所描述。

在此例子中, <after-this-commit> 是 SHA1 hash 或当前分支的 HEAD 的相对位置,从该分支的 Commits 被分析为 rebase 命令. 例如,如果用户希望从当前 HEAD 查看 5 个 Commits 在过去的命令是 git rebase -i HEAD~5.

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

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 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

希望能帮忙,谢谢。

我如何将我的最后一个X承诺集成到一个使用Git的承诺?

git rebase -i HEAD~X

下列内容将显示:

pick 1bffc15c My earlier commit
pick 474bf0c2 My recent commit

# ...

对于你想要的承诺,请用 fixup 取代 pick,所以它会变成:

pick 1bffc15c My earlier commit
fixup 474bf0c2 My recent commit

# ...

如果它在 vim 中打开(终端内默认界面),然后按 Esc 在键盘上,输入 :wq 并输入以保存文件。

查看: 查看 git log

您可以使用 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 &&吉特承诺是,你会得到承诺的消息,以每一个承诺的消息,你正在破坏。