我如何将最后的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 承诺 - 修改,以手动修改。

其他回答

只需将此 bash 函数添加到.zshrc 文件的 bash。

# Squash last X commits with a Commit message.
# Usage: squash X 'COMMIT_MSG'
# where X= Number of last commits.
# where COMMIT_MSG= New commit msg.
function squash() {
    if [ -z "${1}" -o -z "${2}" ]; then
        echo "Usage: \`squash X COMMIT_MSG\`"
        echo "X= Number of last commits."
        echo "COMMIT_MSG= New commit msg."
        return 1
    fi

    git reset --soft HEAD~"$1"
    git add . && git ci -m "$2" # With 100 emoji
    git push --force
}

然后跑

squash X 'New Commit 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从控制台,如果你想转换。

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

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

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

簡單的單線,總是工作,因為你現在在你想要的分支,主是它起源的分支,最新的承諾包含承諾訊息和作者你想使用:

git reset --soft $(git merge-base HEAD master) && git commit --reuse-message=HEAD@{1}