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


当前回答

如何回答与此类工作流相关的问题?

许多地方的承诺,混合了多种合并,从大师,最终推到远程,PR和合并到大师由审查员(是的,开发人员将更容易合并 - 经过大师,但团队认为这将减缓过程)。

我没有在这个页面上看到这样的工作流(这可能是我的眼睛)如果我正确地理解复制,多种融合将需要多种冲突解决方案。

所以,这似乎为我们工作。

git pull master git checkout -b new-branch git checkout -b new-branch-temp edit and commit a lot locally, merge master regularly git checkout new-branch git merge --squash new-branch-temp // puts all changes in stage git commit 'one message to rule them all' git push Reviewer does PR and merges to master。

其他回答

如果您想将每个承诺分成一个单一承诺(例如第一次公开发布项目时),请尝试:

git checkout --orphan <new-branch>
git commit

基于克里斯·约翰森的回答,

从 bash 添加一个全球性的“squash”标志:(或在 Windows 上使用 Git Bash)

git config --global alias.squash '!f(){ git reset --soft HEAD~${1} && git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"; };f'

...或使用Windows的命令促销:

git config --global alias.squash "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"

您的 ~/.gitconfig 现在应该包含此标题:

[alias]
    squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"

使用:

git squash N

它自动将最后的 N 承诺集成在一起,包括。

注意: 结果的承诺消息是所有失败的承诺的组合,顺序. 如果你不满意,你总是可以 git 承诺 - 修改,以手动修改。

许多答案是基于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> 返回原始状态。

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

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

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

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

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

这就是它!

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