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


当前回答

您可以按照 git 命令使用。

 git rebase -i HEAD~n

n(=4在这里)是最后的承诺的数字,然后你得到了以下选项,

pick 01d1124 Message....
pick 6340aaa Message....
pick ebfd367 Message....
pick 30e0ccb Message....

更新如下,选择一个承诺,并将其他人推到最新的,

p 01d1124 Message....
s 6340aaa Message....
s ebfd367 Message....
s 30e0ccb Message....

详细信息点击链接

其他回答

在我的工作流中,我开始一个分支,我的第一个承诺这个分支总结了目标(也就是说,这通常是我将作为功能的“最终”消息推到公共存储库)。所以当我完成时,我只想做的是Git squash master回到第一个消息,然后我准备推。

我用这个 alias:

squash = !EDITOR="\"_() { sed -n 's/^pick //p' \"\\$1\"; sed -i .tmp '2,\\$s/^pick/f/' \"\\$1\"; }; _\"" git rebase -i

这将破坏历史被破坏之前,它会这样做 - 这给你一个机会,通过抓住一个旧的承诺ID从控制台,如果你想转换。

让我们说 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"

我建议尽可能避免吉特重新设置 - 特别是对于吉特新闻. 除非你真的需要自动化一个过程,基于几个承诺,有一个不那么异国情调的方式。

将被排除的承诺放在一个工作分支(如果它们还没有) - 使用 gitk 为此 查看目标分支(例如“主”) git merge --squash (工作分支名称) git commit

承诺的消息将基于 squash 预受欢迎。

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

例如,如果您想在一个分支(远程存储库)中排除最后3个承诺,例如: https://bitbucket.org

我所做的就是

git reset --soft HEAD~3 git commit git push 起源 <branch_name> --force