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


当前回答

简单的解决方案:

git reset --soft HEAD~5

git commit -m “委托信”

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

其他回答

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

只需将此 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'

你已经完成了。

最方便的方式是使用 GitHub 桌面. 只需在历史中选择所有命令,右键单击并选择“Squash x 命令”:

此分類上一篇

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

有人提到在 IntelliJ IDEA UI 上做得多容易:

转到 git 窗口 手动选择所有要融入一个的承诺. 右键单击 > Squash 承诺 > 编辑失败的承诺消息 点击左侧的分支名称 > 右键单击 > Push > Force Push

此分類上一篇