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


当前回答

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

从 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 承诺 - 修改,以手动修改。

其他回答

感谢这个有用的博客帖子,我发现你可以使用这个命令来破坏过去的3个任务:

git rebase -i HEAD~3


显示的文档是相当清晰的. 在任何指定线上,您可以将命令从选择到您选择的命令更改。


我做的第一件事是纠正错误,并在评论中做出新的承诺,把这一点推到我的新功能中。

接下来,我将包含修复(fe7f1e0)的承诺移动到属性承诺下方,并将选择转换为修复。

这在所有承诺都是本地的时工作得很好,但如果你试图改变已经推到远程的承诺,你真的可能会给那些已经检查过同一个分支的其他骗子带来问题!

在问题上,可以是双重的“最后”是什么意思。

* commit H0
|
* merge
|\
| * commit B0
| |
| * commit B1
| | 
* | commit H1
| |
* | commit H2
|/
|

可以使用折扣,但以不同的方式,然后在其他提到的答案:

首頁 / 首頁 / 2

这将向您展示选择选项(如其他答案中提到的):

pick B1
pick B0
pick H0

pick B1
pick B0
s H0

在保存和退出后,将执行命令,然后在H1之后,这意味着它将要求你重新解决冲突(在那里,主将是H1首先,然后积累命令,当它们被应用)。

退缩结束后,您可以选择失败的 H0 和 B0 消息:

* commit squashed H0 and B0
|
* commit B1
| 
* commit H1
|
* commit H2
|

P.S. 如果你只是做一些重新设置到BO:(例如,使用重新设置 - 混合,这里详细解释 https://stackoverflow.com/a/18690845/2405850):

git reset --mixed hash_of_commit_B0
git add .
git commit -m 'some commit message'

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