我经常使用git commit --fixup
(或--squash
),特别是在代码审查期间。显然这些提交最终应该在git rebase --autosquash
之后消失,但它让我担心我可能会忘记将这些提交重新组合并合并为主人。
如何确保我不能将这些分支合并到某些分支中,或者至少某些分支不能与这些提交一起推送?
你可以至少阻止任何包含fixup!
的推动,下面是pre-push
。
#! /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 "$0: 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