your programing

같은 브랜치에있는 두 개의 다른 커밋간에 같은 파일을 어떻게 비교합니까?

lovepro 2020. 9. 27. 13:33
반응형

같은 브랜치에있는 두 개의 다른 커밋간에 같은 파일을 어떻게 비교합니까?


Git에서 동일한 브랜치 (예 : 마스터)에있는 두 개의 다른 커밋 (연속적이지 않음)간에 동일한 파일을 어떻게 비교할 수 있습니까?

Visual SourceSafe (VSS) 또는 Team Foundation Server (TFS) 와 같은 비교 기능을 찾고 있습니다. Git에서 가능합니까?


로부터 git-diff맨 :

git diff [--options] <commit> <commit> [--] [<path>...]

예를 들어, "main.c"파일의 현재와 두 개의 커밋의 차이점을 확인하려면 다음과 같은 세 가지 명령이 있습니다.

$ git diff HEAD^^ HEAD main.c
$ git diff HEAD^^..HEAD -- main.c
$ git diff HEAD~2 HEAD -- main.c

다음과 같이 두 개의 다른 버전에서 두 개의 다른 파일을 비교할 수도 있습니다.

git diff <revision_1>:<file_1> <revision_2>:<file_2>


"difftool"을 구성한 경우 다음을 사용할 수 있습니다.

git difftool revision_1:file_1 revision_2:file_2

예 : 마지막 커밋의 파일을 동일한 브랜치의 이전 커밋과 비교 : 프로젝트 루트 폴더에 있다고 가정

$git difftool HEAD:src/main/java/com.xyz.test/MyApp.java HEAD^:src/main/java/com.xyz.test/MyApp.java

~ / .gitconfig 또는 project / .git / config 파일에 다음 항목이 있어야합니다. p4merge 설치 [이것은 내가 선호하는 diff 및 merge 도구]

[merge]
    tool = p4merge
    keepBackup = false
[diff]
    tool = p4merge
    keepBackup = false
[difftool "p4merge"]
    path = C:/Program Files (x86)/Perforce/p4merge.exe
[mergetool]
    keepBackup = false
[difftool]
    keepBackup = false
[mergetool "p4merge"]
    path = C:/Program Files (x86)/Perforce/p4merge.exe
    cmd = p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"

확인 $ git log후 SHA의 2 개의 다른 커밋의 ID와 다음 실행 복사, git diff예를 들어, 그 ID를 가진 명령을 :

$ git diff (sha-id-one) (sha-id-two)

커밋별로 두 커밋 사이의 파일에 대한 모든 변경 사항을 보려면 다음을 수행 할 수도 있습니다.

git log -u $start_commit..$end_commit -- path/to/file


다음은 git log 명령에서 찾은대로 주어진 파일에 대해 git diff 명령을 출력하는 perl 스크립트입니다.

예 :

git log pom.xml | perl gldiff.pl 3 pom.xml

수율 :

git diff 5cc287:pom.xml e8e420:pom.xml
git diff 3aa914:pom.xml 7476e1:pom.xml
git diff 422bfd:pom.xml f92ad8:pom.xml

그런 다음 쉘 창 세션에 붙여 넣거나 / bin / sh에 파이프 할 수 있습니다.

메모:

  1. 숫자 (이 경우 3)는 인쇄 할 줄 수를 지정합니다.
  2. 파일 (이 경우 pom.xml)은 두 위치에서 모두 일치해야합니다 (두 위치에서 동일한 파일을 제공하기 위해 쉘 함수로 래핑 할 수 있음) 또는 쉘 스크립트로 bin dir에 넣을 수 있습니다.

암호:

# gldiff.pl
use strict;

my $max  = shift;
my $file = shift;

die "not a number" unless $max =~ m/\d+/;
die "not a file"   unless -f $file;

my $count;
my @lines;

while (<>) {
    chomp;
    next unless s/^commit\s+(.*)//;
    my $commit = $1;
    push @lines, sprintf "%s:%s", substr($commit,0,6),$file;
    if (@lines == 2) {
        printf "git diff %s %s\n", @lines;
        @lines = ();
    }
    last if ++$count >= $max *2;
}

여러 파일 또는 디렉토리가 있고 비 연속 커밋을 비교하려면 다음과 같이 할 수 있습니다.

임시 분기 만들기

git checkout -b revision

첫 번째 커밋 대상으로 되감기

git reset --hard <commit_target>

관심있는 커밋을 체리 따기

git cherry-pick <commit_interested> ...

차이 적용

git diff <commit-target>^

끝났을 때

git branch -D revision

@mipadi에 지정된 방법을 사용하여 둘 이상의 파일로 diff를 작성하려면 다음을 수행하십시오.

예를 들어 모든 파일 을 찾으 HEAD려면과 사이의 차이 :master.coffee

git diff master..HEAD -- `find your_search_folder/ -name '*.coffee'`

이렇게하면 your_search_folder/모든 .coffee파일 을 재귀 적으로 검색 하고 파일과 해당 master버전을 비교합니다 .


git의 굉장함을 사용하는 또 다른 방법 ...

git difftool HEAD HEAD@{N} /PATH/FILE.ext

If you want a simple visual comparison on Windows such as you can get in VSS or TFS, try this:

  • right-click on the file in File Explorer
  • select 'Git History'

Note: After upgrading to Windows 10 I have lost the git context menu options. However, you can achieve the same thing using 'gitk' or 'gitk filename' in a command window.

Once you call 'Git History', the Git GUI tool will start, with a history of the file in the top left pane. Select one of the versions you would like to compare. Then right-click on the second version and choose either

Diff this -> selected

or

Diff selected -> this

Colour-coded differences will appear in the lower left-hand pane.

참고URL : https://stackoverflow.com/questions/3338126/how-do-i-diff-the-same-file-between-two-different-commits-on-the-same-branch

반응형