Programing

git diff 이름이 변경된 파일

lottogame 2020. 8. 21. 08:27
반응형

git diff 이름이 변경된 파일


파일이 a.txt있습니다.

cat a.txt
> hello

의 내용 a.txt은 "안녕하세요"입니다.

나는 커밋합니다.

git add a.txt
git commit -m "first commit"

그런 다음 dir 로 이동 a.txt합니다 test.

mkdir test
mv a.txt test

그런 다음 두 번째 커밋을합니다.

git add -A
git commit -m "second commit"

마지막으로 a.txt대신 "안녕"이라고 편집 합니다.

cat a.txt
> goodbye

마지막 커밋을합니다.

git add a.txt
git commit -m "final commit"

이제 여기 내 질문이 있습니다.

a.txt마지막 커밋과 첫 커밋 사이 의 내용을 어떻게 비교 합니까?

시도해 보았지만 git diff HEAD^^..HEAD -M a.txt작동하지 않았습니다. git log --follow a.txt이름을 제대로 감지하지만 git diff. 하나있어?


차이의 문제 HEAD^^와는 HEAD당신이 가지고있다 a.txt사본 및 변화가 어떤 이름 바꾸기가 없다, 그래서 그냥 (차이는 무엇 인) 두 커밋을 고려, 모두 커밋에 있습니다.

복사본을 감지하려면 다음을 사용할 수 있습니다 -C.

git diff -C HEAD^^ HEAD

결과:

index ce01362..dd7e1c6 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-hello
+goodbye
diff --git a/a.txt b/test/a.txt
similarity index 100%
copy from a.txt
copy to test/a.txt

덧붙여서, diff를 하나의 경로로 제한하는 경우 ( git diff HEAD^^ HEAD a.txt단일 경로를 제외한 모든 항목을 제외하고 이름을 변경하거나 복사하는 경우 정의에 따라 두 가지가 포함되므로 이름이 변경되거나 복사본이 표시되지 않습니다. 경로.


특정 파일의 이름을 바꾸려면 -M -- <old-path> <new-path>( -C도 작동 함)을 사용하십시오.

따라서 마지막 커밋에서 파일의 이름 변경 하고 변경 한 경우 다음을 사용 하여 변경 사항을 볼 수 있습니다.

git diff HEAD^ HEAD -M -- a.txt test/a.txt

이것은 다음을 생성합니다.

diff --git a/a.txt b/test/a.txt
similarity index 55%
rename from a.txt
rename to test/a.txt
index 3f855b5..949dd15 100644
--- a/a.txt
+++ b/test/a.txt
@@ -1,3 +1,3 @@
 // a.txt

-hello
+goodbye

( // a.txtgit이 이름 바꾸기를 감지 할 수 있도록 추가 된 행)


git이 이름 변경을 감지하지 못하는 경우 를 사용하여 낮은 유사성 임계 값 ( -M[=n]예 : 1 %)을 지정할 수 있습니다 .

git diff HEAD^ HEAD -M01 -- a.txt test/a.txt

에서 힘내은 diff 문서 :

-M [<n>]-이름 찾기 [= <n>]

Detect renames. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file's size). For example, -M90% means Git should consider a delete/add pair to be a rename if more than 90% of the file hasn't changed. Without a % sign, the number is to be read as a fraction, with a decimal point before it. I.e., -M5 becomes 0.5, and is thus the same as -M50%. Similarly, -M05 is the same as -M5%. To limit detection to exact renames, use -M100%. The default similarity index is 50%.


You can also do:

git diff rev1:file1 rev2:file2

which, for your example, would be

git diff HEAD^^:./a.txt HEAD:./test/a.txt

Note the explicit ./ -- this format otherwise assumes the paths to be relative to the root of the repo. (If you're in the root of the repo, you can of course omit that.)

This doesn't depend on the rename detection at all, as the user is explicitly stating exactly what to compare. (Therefore, it also comes in handy in some other circumstances, such as comparing files between different svn branches in a git-svn environment.)


If your rename commit is staged but not committed yet, you can use:

git diff --cached -M -- file.txt renamed_file.txt

참고URL : https://stackoverflow.com/questions/7759193/git-diff-renamed-file

반응형