预览 Git 推送

42

如何查看实际将被推送到远程仓库的提交记录?

据我所知,每当我从远程仓库拉取主分支时,提交记录都有可能会生成,即使它们是空的。

这会导致本地的主分支即使没有需要推送的内容也变得“前进”了。

现在,如果我尝试(从主分支):

git cherry origin master

我知道即将被推送的内容是什么,但这也展示了我已经推送过的一些提交。有没有办法只显示即将被推送的新内容?

4个回答

25

记住,origin/master是一个引用,指向上次拉取时远程命名为origin的主分支的头部,因此您可以使用以下命令:

$ git log origin/master..master

您可以使用下面的git-preview-push来评论git push --dry-run --porcelain输出的内容:
#! /usr/bin/env perl

use warnings;
use strict;

die "Usage: $0 remote refspec\n" unless @ARGV == 2;
my($origin,$refspec) = @ARGV;
my @cmd = qw/ git push --dry-run --porcelain /;
no warnings 'exec';
open my $fh, "-|" => @cmd, $origin, $refspec or die "$0: exec: $!";
# <flag> \t <from>:<to> \t <summary> (<reason>)
my $update = qr/^ (.*)         \t    # flag (optional)
                  (\S+):(\S+)  \t    # from:to
                  (.+)               # summary
                  (?:[ ] \((.+)\))?  # reason
                $/x;

while (<$fh>) {
  next unless my($flag,$from,$to,$summary,$reason) = /$update/;
  if ($flag eq "!") {
    print "$0: $refspec rejected:\n", $_;
  }
  elsif ($flag eq "=") {
    print "$0: $refspec up-to-date\n";
  }
  if ($summary =~ /^[0-9a-f]+\.\.[0-9a-f]+$/) {
    system("git log --pretty=oneline $summary") == 0
      or warn "$0: git log exited " . ($? >> 8);
  }
  elsif ($summary eq "[new branch]") {
    print "$0: $refspec creates a new branch.\n";
  }
}

示例用法:

$ git preview-push /tmp/bare master
到 /tmp/bare
270f8e6bec7af9b2509710eb1ae986a8e97068ec baz
4c3d1e89f5d6b0d493c9d0c7a06420d6b2eb5af7 bar

1
+1,但我必须就 #!/usr/bin/env perl 上升到我的讲台,因为当我在 /usr/local/bin/perl 中安装模块时,bugzilla 使用 #!/usr/bin/perl,而我不得不暂时将 /usr/bin/perl 做成指向 /usr/local/bin/perl 的符号链接,结果被烧毁了。 - William Pursell
7
在大多数情况下,执行命令 git log origin/master..master 就足够了。 - Igor Zevaka
2
git log origin/master..master --oneline --decorate 也不错。 - hIpPy

5
我编写了一款工具来完成这个任务,名为git wtf:https://github.com/michaelklishin/git-wtf。有颜色和其他一些特性!作为额外的福利,它还会显示功能分支和集成分支之间的关系。

4
我已经将以下别名添加到我的~/.gitconfig文件中,以显示在执行pull操作时会合并哪些内容,会推送哪些内容,以及与远程仓库进行比较的别名:
[alias]
        # diff remote branch (e.g., git diff origin/master master)
        difr = "diff @{u}"

        # similar to hg incoming/outgoing, showing what would be pulled/pushed
        # use option "-p" to see actual patch
        incoming = "!git remote update -p; git log ..@{u}"

        # showing what would be pushed (see also alias difr)
        outgoing = log @{u}..

0

如果您将此内容放入Bash配置文件中,您就可以运行grin(Git远程传入)和grout(Git远程传出),以查看原始主机的传入和传出提交差异:

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}

function gd2 {
    echo branch \($1\) has these commits and \($2\) does not
    git log $2..$1 --no-merges --format='%h | Author:%an | Date:%ad | %s' --date=local
}

function grin {
    git fetch origin master
    gd2 FETCH_HEAD $(parse_git_branch)
}

function grout {
    git fetch origin master
    gd2 $(parse_git_branch) FETCH_HEAD
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接