-
慕雪6442864
使用git rebase -i <after-this-commit>并将第二次提交和后续提交中的“挑选”替换为“squash”或“Fixup”,如手册.在这个例子中,<after-this-commit>是SHA 1散列或来自当前分支头部的相对位置,从该分支的提交将为rebase命令进行分析。例如,如果用户希望在过去从当前头查看5提交,则命令为git rebase -i HEAD~5.
-
慕沐林林
你可以很容易地做到这一点。git rebase或git merge --squash。在本例中,我们将压缩最后3次提交。如果您想从头开始编写新的提交消息,这就足够了:git reset --soft HEAD~3 &&
git commit如果您想开始编辑新的提交消息,并将现有的提交消息连接起来(例如,与Pick/squash/…类似)壁球git rebase -i),然后您需要提取这些消息并将它们传递给git commit:git reset --soft HEAD~3 &&
git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"这两个方法都以相同的方式将最后三个提交压缩为单个新提交。软重置只是重新指向最后一次提交,你不想压扁。软重置不会触及索引或工作树,使索引处于新提交时所需的状态(也就是说,它已经对即将“丢弃”的提交进行了所有更改)。
-
翻阅古今
你可以用git merge --squash对于这一点,它比git rebase -i。假设您使用的是主人,并且希望将最后12次提交压缩为一次。警告:首先要确保你的工作-检查一下。git status是干净的(因为git reset --hard将丢弃已上演和未上演的变化)然后:# 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更详细的选项。最新情况:这种方法相对于简单的方法唯一真正的优点是git reset --soft HEAD~12 && git commit由chris johnsen在他的回答是将预填充的提交消息预先填充到正在压缩的每个提交消息中。