如何确保修复提交不合并到主分支
How to make sure fixup commits are not merged into master branch
我经常使用 git commit --fixup
(或 --squash
),尤其是在代码审查期间。显然,这些提交最终应该在 git rebase --autosquash
之后消失,但让我担心的是我可能会忘记变基并将这些提交合并到 master.
我如何确保我不能将它们合并到某些分支中,或者至少不能将这些提交推送到某些分支中?
您至少可以使用下面的 pre-push
挂钩阻止任何包含 fixup!
的推送。
#! /usr/bin/perl
use strict;
use warnings;
use constant Z40 => '0' x 40;
my($remote,$url) = @ARGV;
my $abort_push = 0;
while (<STDIN>) {
# <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
my($lref,$lsha,$rref,$rsha) = split;
if ($lsha eq Z40) {} # ignore deletes
else {
my $commit_range =
$rsha eq Z40
? $lsha # new branch: check all commits
: "$rsha..$lsha"; # existing: check new commits since $rsha
my @cmd = (qw/ git rev-list --pretty=oneline --grep ^fixup! /, $commit_range);
open my $fh, "-|", @cmd or die "[=10=]: failed to start git rev-list: $!";
my @fixup_commits;
while (<$fh>) { push @fixup_commits, " - $_" }
close $fh;
if (@fixup_commits) {
my $s = @fixup_commits == 1 ? "" : "s";
warn "Remove fixup$s from $lref:\n", @fixup_commits;
$abort_push = 1;
}
}
}
die "Push aborted.\n" if $abort_push;
例如历史记录为
$ git lola
* 4a732d4 (HEAD -> feature/foo) fixup! fsdkfj
| * 478075c (master) w00t
| * 1d572d3 fixup! sdlkf
| * f9a55ee fixup! yo
|/
* ea708b0 (origin/master) three
* d4276a2 two
* 6426569 hello
尝试推动给出
$ git push origin master feature/foo
Remove fixups from refs/heads/master:
- 1d572d32f963d6218ed3b92f69d58a8ec790d7ea fixup! sdlkf
- f9a55ee14f28f9496e2aea1bc400ca65ae150f4b fixup! yo
Remove fixup from refs/heads/feature/foo:
- 4a732d4601012246986037437ac0c0bab39dd0a9 fixup! fsdkfj
Push aborted.
error: failed to push some refs to [...]
请注意 git lola
是非标准的,但 highly useful alias。将以下内容添加到您的全局 .gitconfig
.
[alias]
lol = log --graph --decorate --pretty=oneline --abbrev-commit
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all
我经常使用 git commit --fixup
(或 --squash
),尤其是在代码审查期间。显然,这些提交最终应该在 git rebase --autosquash
之后消失,但让我担心的是我可能会忘记变基并将这些提交合并到 master.
我如何确保我不能将它们合并到某些分支中,或者至少不能将这些提交推送到某些分支中?
您至少可以使用下面的 pre-push
挂钩阻止任何包含 fixup!
的推送。
#! /usr/bin/perl
use strict;
use warnings;
use constant Z40 => '0' x 40;
my($remote,$url) = @ARGV;
my $abort_push = 0;
while (<STDIN>) {
# <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF
my($lref,$lsha,$rref,$rsha) = split;
if ($lsha eq Z40) {} # ignore deletes
else {
my $commit_range =
$rsha eq Z40
? $lsha # new branch: check all commits
: "$rsha..$lsha"; # existing: check new commits since $rsha
my @cmd = (qw/ git rev-list --pretty=oneline --grep ^fixup! /, $commit_range);
open my $fh, "-|", @cmd or die "[=10=]: failed to start git rev-list: $!";
my @fixup_commits;
while (<$fh>) { push @fixup_commits, " - $_" }
close $fh;
if (@fixup_commits) {
my $s = @fixup_commits == 1 ? "" : "s";
warn "Remove fixup$s from $lref:\n", @fixup_commits;
$abort_push = 1;
}
}
}
die "Push aborted.\n" if $abort_push;
例如历史记录为
$ git lola
* 4a732d4 (HEAD -> feature/foo) fixup! fsdkfj
| * 478075c (master) w00t
| * 1d572d3 fixup! sdlkf
| * f9a55ee fixup! yo
|/
* ea708b0 (origin/master) three
* d4276a2 two
* 6426569 hello
尝试推动给出
$ git push origin master feature/foo
Remove fixups from refs/heads/master:
- 1d572d32f963d6218ed3b92f69d58a8ec790d7ea fixup! sdlkf
- f9a55ee14f28f9496e2aea1bc400ca65ae150f4b fixup! yo
Remove fixup from refs/heads/feature/foo:
- 4a732d4601012246986037437ac0c0bab39dd0a9 fixup! fsdkfj
Push aborted.
error: failed to push some refs to [...]
请注意 git lola
是非标准的,但 highly useful alias。将以下内容添加到您的全局 .gitconfig
.
[alias]
lol = log --graph --decorate --pretty=oneline --abbrev-commit
lola = log --graph --decorate --pretty=oneline --abbrev-commit --all