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


当前回答

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

我所做的就是

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

其他回答

只需将此 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 reset --soft HEAD~5

git commit -m “委托信”

git push 起源分支 --force-with-lease

方法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 HEAD~3 &&
git commit

如果你想开始编辑新的承诺消息与现有承诺消息的折叠(即类似于什么一个 pick/squash/squash/.../squash git rebase -i 指示列表会开始你),那么你需要提取这些消息,并将它们转移到 git commit:

git reset --soft HEAD~3 && 
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"

两种方法都将过去三项承诺分成一个单一的新承诺,相同的方式。 软重定义只会重新点头到最后一项承诺,你不想分解。 无论指数还是工作树都不会被软重定义所触摸,让指数在你新承诺所需的状态(即它已经有所有从你即将“扔掉”的承诺的变化)。

要避免在重定向同一分支中的承诺时不得不解决任何合并冲突,您可以使用下列命令:

git rebase -i <last commit id before your changes start> -s recursive -X 我們

要将所有承诺分成一个,当您被要求编辑将合并的承诺时(在旗帜上),更新所有但第一个行动,从选择到分解,如其他答案中所建议。

在这里,我们使用合并策略(旗帜)回归和战略选项(X)我们的,以确保历史上后来的承诺赢得任何合并冲突。

注意:不要把这与我们做别的事情的 git rebase 混淆。

引用: git rebase recursive merge 策略