在git中撤消几个提交到一个提交

时间:2014-12-02 02:19:30

标签: git github

我最近在github上为一个开源项目做了贡献并且它很受欢迎,但是我没有遵守提交消息标准所以我被要求这样做,而不是每次提交做一次更改,我应该只使用我在其中提出的所有更改进行一次提交。

那么,我怎样才能撤消以前的所有提交,只做一个包含所有更改的提交?

1 个答案:

答案 0 :(得分:1)

您可以使用git interactive rebase将提交“压缩”为一个。

# To squash 2 commits
git rebase -i HEAD~2

# To squash back to a specific commit identified by it's sha1
git rebase -i a3ea79f


如果你有这样的提交历史: -

git log --oneline --decorate

* d6f7bbd (HEAD, master) added test2
* 35dde7a added test1
* a3ea79f initial commit

使用上述任何一个rebase命令重新打开将打开编辑器。它看起来像这样,修改“选择”行将调整提交: -

pick 35dde7a added test1
pick d6f7bbd added test2

# Rebase a3ea79f..d6f7bbd onto a3ea79f
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# 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