"hg cat"또는 "svn cat"의 git에 해당
git 저장소에 보관 된 파일의 최신 버전 사본을 추출하여 일부 처리를 위해 스크립트에 전달하고 싶습니다. svn 또는 hg에서는 "cat"명령을 사용합니다.
지정된 개정판에있는 그대로 지정된 파일을 인쇄합니다. 개정이 제공되지 않으면 작업 디렉토리의 상위가 사용되고 개정이 체크 아웃되지 않은 경우 팁이 사용됩니다.
(hg 문서의 hg cat에 대한 설명에서 발췌)
git로이 작업을 수행하는 동등한 명령은 무엇입니까?
git show rev:path/to/file
여기서 rev 는 개정판입니다.
git 및 svn 명령의 비교는 http://git.or.cz/course/svn.html 을 참조하십시오 .
다음과 같이 실행할 수있는 "git cat-file"이 있습니다.
$ git cat-file blob v1.0:path/to/file
여기서 'v1.0'을 원하는 브랜치, 태그 또는 커밋 SHA로 바꾼 다음 'path / to / file'을 저장소의 상대 경로로 바꿀 수 있습니다. 원하는 경우 '-s'를 전달하여 콘텐츠의 크기를 확인할 수도 있습니다.
이전에 언급 한 'show'가 거의 동일한 작업을 수행하지만 익숙한 'cat'명령에 더 가깝습니다.
git show
찾고있는 명령입니다. 문서에서 :
git show next~10:Documentation/README
Shows the contents of the file Documentation/README as they were
current in the 10th last commit of the branch next.
또한 브랜치 이름 (1st p의 HEAD와 같은)으로 작업하십시오.
git show $branch:$filename
사용 git show
에서와 같이, git show commit_sha_id:path/to/some/file.cs
.
github에 있는 git cat shell 스크립트를 작성했습니다.
직접적인 대체물 이없는 것 같습니다 . 이 블로그 항목 은 최신 커밋을 결정한 다음 해당 커밋의 파일에 대한 해시를 결정한 다음 덤프 하여 동일한 작업을 수행하는 방법을 자세히 설명합니다 .
git log ...
git ls-tree ...
git show -p ...
(블로그 항목에 오타가 있으며 위의 명령을 명령과 함께 사용 svn
)
어떤 git show
제안도 진정 만족스럽지 않습니다. 왜냐하면 (내가 할 수있는대로 시도해보세요), 출력 상단에서 메타 데이터를 얻을 수없는 방법을 찾을 수 없기 때문입니다. cat (1)의 정신은 내용을 보여주는 것뿐입니다. 이것은 (아래) 파일 이름과 선택적 번호를 취합니다. 숫자는 되돌리고 싶은 커밋 방법입니다. (해당 파일을 변경 한 커밋. 대상 파일을 변경하지 않은 커밋은 계산되지 않습니다.)
gitcat.pl filename.txt
gitcat.pl -3 filename.txt
filename.txt의 최신 커밋 시점의 filename.txt 내용과 그 이전의 3 개 커밋의 내용을 보여줍니다.
#!/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 );
}
bash를 사용하는 사람들에게는 다음이 유용한 기능입니다.
gcat () { if [ $# -lt 1 ]; then echo "Usage: $FUNCNAME [rev] file"; elif [ $# -lt 2 ]; then git show HEAD:./$*; else git show $1:./$2; fi }
.bashrc
파일에 넣으십시오 ( gcat
.
사용 예 :
> gcat
Usage: gcat [rev] file
또는
> gcat subdirectory/file.ext
또는
> gcat rev subdirectory/file.ext
참고 URL : https://stackoverflow.com/questions/2071288/equivalent-in-git-of-hg-cat-or-svn-cat
'developer tip' 카테고리의 다른 글
플로팅 라벨 스피너? (0) | 2020.09.22 |
---|---|
프로비저닝 프로필 갱신 (0) | 2020.09.22 |
라이브러리 경로의 기본 설정을 지정하는 방법은 무엇입니까? (0) | 2020.09.22 |
iOS 장치 (모바일 Safari)의 입력 필드에서 프로그래밍 방식으로 텍스트 선택 (0) | 2020.09.21 |
HTTP 상태 코드 0-오류 도메인 = NSURLErrorDomain? (0) | 2020.09.21 |