git:squash/fixup之前的提交

elm*_*rco 24 git

假设你有:

A-B-C
Run Code Online (Sandbox Code Playgroud)

现在你的构建/测试失败了.修复程序应该合并到A.我当前的工作流程是这样的:

$ git commit -m "fixA"

A-B-C-fixA

$ git rebase -i A~1
Run Code Online (Sandbox Code Playgroud)

并在A中压制fixA,导致:

A'-B-C
Run Code Online (Sandbox Code Playgroud)

是否有命令可以执行以下操作:

A-B-C + (index with fix for A)

$ git commit -supperdupper A 
Run Code Online (Sandbox Code Playgroud)

结果:

A'-B-C
Run Code Online (Sandbox Code Playgroud)

Jo *_*iss 20

如果您只是想找到修复早期提交的简单解决方案,请阅读问题!它解释了一切.但是,既然Elmarco要求一个光滑的方式,我们在这里:

从Git 1.7.0开始,有一个--autosquash选项可以满足rebase您的需求.也有--fixup--squash选项commit,使事情变得更容易.通过一些别名,您甚至可以将整个事情整合到一个命令中.

我建议升级到最新的Git以获得最大的惊人效果:

git/Documentation/RelNotes $ grep -i -A1 autosquash\\\|fixup *
1.7.0.txt: * "git rebase -i" learned new action "fixup" that squashes the change
1.7.0.txt-   but does not affect existing log message.
--
1.7.0.txt: * "git rebase -i" also learned --autosquash option that is useful
1.7.0.txt:   together with the new "fixup" action.
1.7.0.txt-
--
1.7.3.txt: * "git rebase -i" peeks into rebase.autosquash configuration and acts as
1.7.3.txt:   if you gave --autosquash from the command line.
1.7.3.txt-
--
1.7.4.txt: * "git commit" learned --fixup and --squash options to help later invocation
1.7.4.txt-   of the interactive rebase.
--
1.7.4.txt: * "git rebase --autosquash" can use SHA-1 object names to name which
1.7.4.txt:   commit to fix up (e.g. "fixup! e83c5163").
1.7.4.txt-
Run Code Online (Sandbox Code Playgroud)

  • 很好的例子:http://technosorcery.net/blog/2010/02/07/fun-with-the-upcoming-1-7-release-of-git-rebase---interactive---autosquash/ (2认同)

小智 5

创建了一些别名,以便更轻松地使用git 1.7 中添加的git commit --fixup和命令。git commit --squash将这些添加到您的~/.gitconfig

[alias]
  fixup = !sh -c 'REV=$(git rev-parse $1) && git commit --fixup $@ && git rebase -i --autosquash $REV^' -
  squash = !sh -c 'REV=$(git rev-parse $1) && git commit --squash $@ && git rebase -i --autosquash $REV^' -
Run Code Online (Sandbox Code Playgroud)

用法:

$ git commit -am 'bad commit'
$ git commit -am 'good commit'

$ git add .          # Stage changes to correct the bad commit
$ git fixup HEAD^    # HEAD^ can be replaced by the SHA of the bad commit
Run Code Online (Sandbox Code Playgroud)

错误的提交可能是多次提交的结果。