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


当前回答

我们真的很高兴这个答案,这意味着你不需要通过承诺计算。

如果你有大量的承诺在主分支,并希望将它们全部结合在一个:

确保你在主分支和做 git 状态,以确保它是所有的清洁 复制承诺 SHA 为承诺 之前你开始你的工作 git 重新设置 ‒软承诺IdSHA

[您将注意到,您此时此刻的所有更改都将显示在您的 IDE 源控制部分 - 很好地审查它们以检查它们都在那里]

git commit -am “你想要的消息将取代所有其他 commit 消息” git push (会抱怨你在承诺后面) 所以做 git push --force

这就是它!

其他回答

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

我所做的就是

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

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

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

如果您正在使用 GitUp,选择您想要与其父母合并的承诺,并按下 S. 您必须为每个承诺做一次,但它比与正确的命令线引发更简单。

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

方法1 如果你有很多承诺

git rebase -i master 然后按一下键盘“i”来编辑

你会看到这样的:

pick etc1
pick etc2
pick etc2

用“f”取代“ pick”这个词,然后按 esc y :wq。

pick etc1 //this commit will the one commit
f etc2
f etc2

按下这个命令

git push origin +head

方法2 如果你有少数承诺,你可以这样做去删除承诺,你必须这样做去删除你的第二承诺等等。

git reset --soft HEAD^1 // or git reset --soft head~1
git commit --amend //then press `:wq` 
git push -f

方法 3 如果你已经有一个承诺,你不想提交另一个承诺更多

git add files...
git commit --amend  //then press `:wq`
git push origin +head