Git 如何压缩所有提交
Git how to squash all the commit
我已经尝试使用 git rebase -i HEAD~3
压缩所有提交。但是 git 抛出以下错误,因为提交 HEAD~3
不存在。
fatal: invalid upstream 'HEAD~3'
$ git log --oneline
7cabc02 (HEAD -> master) fix bug
26a9c03 fix bug
59fe21b record video process
那么,我怎样才能将所有提交压缩到一个。
你只有两个提交早于你的 HEAD,所以 HEAD~3
确实不存在。如果你想(交互地)对所有这些进行变基,你可以(应该!)使用 HEAD~2
:
git rebase -i HEAD~2
您还可以显式使用提交哈希:
git rebase -i 59fe21b
压缩这三个提交(包括第一个)的方法是运行以下命令:
git rebase -i --root master
请注意 --root
选项是在 git version 1.7.12 中引入的。
运行 此命令将打开交互式对话框以执行变基,该对话框将包含您的第一次提交。在该对话框中,将第一个提交保留为 pick
,并将其他两个设置为 squash
。保存并退出编辑器以执行操作。
pick 59fe21b record video process
squash 26a9c03 fix bug
squash 7cabc02 fix bug
# Rebase 7cabc02 onto 59fe21b (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
我已经尝试使用 git rebase -i HEAD~3
压缩所有提交。但是 git 抛出以下错误,因为提交 HEAD~3
不存在。
fatal: invalid upstream 'HEAD~3'
$ git log --oneline
7cabc02 (HEAD -> master) fix bug
26a9c03 fix bug
59fe21b record video process
那么,我怎样才能将所有提交压缩到一个。
你只有两个提交早于你的 HEAD,所以 HEAD~3
确实不存在。如果你想(交互地)对所有这些进行变基,你可以(应该!)使用 HEAD~2
:
git rebase -i HEAD~2
您还可以显式使用提交哈希:
git rebase -i 59fe21b
压缩这三个提交(包括第一个)的方法是运行以下命令:
git rebase -i --root master
请注意 --root
选项是在 git version 1.7.12 中引入的。
运行 此命令将打开交互式对话框以执行变基,该对话框将包含您的第一次提交。在该对话框中,将第一个提交保留为 pick
,并将其他两个设置为 squash
。保存并退出编辑器以执行操作。
pick 59fe21b record video process
squash 26a9c03 fix bug
squash 7cabc02 fix bug
# Rebase 7cabc02 onto 59fe21b (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out