Keine der git show
Vorschläge wirklich befriedigen, weil (versuchen Sie, wie ich könnte), kann ich nicht einen Weg finden, nicht die bekommen Metadaten kommen von der Spitze der Ausgabe. Der Geist von Katze (1) ist nur um den Inhalt zu zeigen. Dies (unten) nimmt einen Dateinamen und eine optionale Nummer. Die Nummer gibt an, wie oft Sie zurückgehen möchten. (Commits, die diese Datei geändert haben. Commits, die die Zieldatei nicht ändern, werden nicht gezählt.)
gitcat.pl filename.txt
gitcat.pl -3 filename.txt
zeigt den Inhalt des filename.txt ab spätestens vor dem von filename.txt und den Inhalt von 3 Commits begehen.
#!/usr/bin/perl -w
use strict;
use warnings;
use FileHandle;
use Cwd;
# Have I mentioned lately how much I despise git?
(my $prog = $0) =~ s!.*/!!;
my $usage = "Usage: $prog [revisions-ago] filename\n";
die($usage) if(! @ARGV);
my($revision, $fname) = @ARGV;
if(! $fname && -f $revision) {
($fname, $revision) = ($revision, 0);
}
gitcat($fname, $revision);
sub gitcat {
my($fname, $revision) = @_;
my $rev = $revision;
my $file = FileHandle->new("git log --format=oneline '$fname' |");
# Get the $revisionth line from the log.
my $line;
for(0..$revision) {
$line = $file->getline();
}
die("Could not get line $revision from the log for $fname.\n")
if(! $line);
# Get the hash from that.
my $hash = substr($line, 0, 40);
if(! $hash =~ m/^([0-9a-fA-F]{40})/x) {
die("The commit hash does not look a hash.\n");
}
# Git needs the path from the root of the repo to the file because it can
# not work out the path itself.
my $path = pathhere();
if(! $path) {
die("Could not find the git repository.\n");
}
exec("git cat-file blob $hash:$path/'$fname'");
}
# Get the path from the git repo to the current dir.
sub pathhere {
my $cwd = getcwd();
my @cwd = split('/', $cwd);
my @path;
while(! -d "$cwd/.git") {
my $path = pop(@cwd);
unshift(@path, $path);
if(! @cwd) {
die("Did not find .git in or above your pwd.\n");
}
$cwd = join('/', @cwd);
}
return join('/', map { "'$_'"; } @path);
}
Meine letzte Lösung war zum ersten "git ls-files --full-name" verwenden, um den vollständigen Pfad relativ zum oberen Rand des Repository, und dann „git show HEAD: " –
mögliches Duplikat von [Wie kann eine einzelne Datei von einer bestimmten Revision in Git abgerufen werden?] (http://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file -from-specific-revision-in-git) – ks1322
mögliches Duplikat von [Gibt es einen schnellen Git-Befehl, um eine alte Version einer Datei anzuzeigen?] (http://stackoverflow.com/questions/338436/is-there-a -quick-git-command-zu-sehen-eine-alte-Version-of-a-fil e) –