git diff
는 working directory에서의 변화, staging에서의 변화를 살펴 볼 수 있다. git status
, git log
처럼 정보만을 주는 기능으로 무엇이 달라졌는 지만 보여준다.
먼저 git diff
를 사용해보기 위해서 새로운 rainbow.txt
을 만들어보자.
red
orange
yellow
green
blue
purple
해당 파일을 repository에 반영하도록 하자.
git add ./rainbow.txt
git commit -m "Add rainbow.txt"
이제 rainbow.txt
를 수정해보도록 하자.
red
orange
yellow
green
blue
indigo
violet
indigo
와 violet
이 추가되고 purple
이 사라졌다. git diff
를 통해서 확인해보도록 하자.
git diff
diff --git a/rainbow.txt b/rainbow.txt
index 0b75516..26ec8e7 100644
--- a/rainbow.txt
+++ b/rainbow.txt
@@ -3,4 +3,5 @@ orange
yellow
green
blue
-purple
\ No newline at end of file
+indigo
+violet
diff --git a/rainbow.txt b/rainbow.txt
는 a
버전의 rainbow.txt와 b
버전의 rainbow.txt를 비교한다는 것이다. index
부분은 metadata인데 필요없다.-
는 삭제된 부분 +
는 추가된 부분을 의미한다. 본문 부분을 chunk
라고 한다. 재밌는 것은 orange
부분인데, orange
를 포함하지 않고, 그 아래부터 수정되었다는 것이다. yellow
, green
, blue
는 수정되지 않은 부분이지만, 문맥을 위해서 존재한다고 보면 된다.
git status
말고도 기본적으로 git diff
를 통해서 staging되지 않은 working directory와 staging된 영역 간의 비교가 가능하다.
이외에도 git diff
는 상당히 비슷한 pattern으로 여러 상황에서의 비교가 가능하다.
git diff HEAD
는 commit된 HEAD와 현재 file들의 차이를 보여준다. 즉, working direcotry든 stage area에 있던 HEAD
와 차이가 있다면 diff
를 보여준다는 것이다.
colors.txt
라는 file을 하나만들고, 다음의 값들을 넣자.
red
orange
yello
green
blue
purple
다음으로 commit을 시켜주도록 하자.
git commit -am "add colors.txt"
이제 colors.txt
에 purple
을 지우고 indigo
를 넣어보도록 하자.
red
orange
yello
green
blue
indigo
commit을 하지 않고, git diff
로 차이를 확인해보도록 하자.
git diff
diff --git a/colors.txt b/colors.txt
index 960ca33..d1f49b6 100644
--- a/colors.txt
+++ b/colors.txt
@@ -3,4 +3,4 @@ orange
yello
green
blue
-purple
\ No newline at end of file
+indigo
\ No newline at end of file
다음과 같이 git diff
는 working directory와 stage와의 영역 차이를 보여준다.
git diff HEAD
역시도 마찬가지의 결과가 나온다.
git diff HEAD
diff --git a/colors.txt b/colors.txt
index 960ca33..d1f49b6 100644
--- a/colors.txt
+++ b/colors.txt
@@ -3,4 +3,4 @@ orange
yello
green
blue
-purple
\ No newline at end of file
+indigo
\ No newline at end of file
다음으로 git add ./colors.txt
를 실행시켜 working directory에 있는 변동 사항을 stage area로 올리도록 하자.
git add ./colors.txt
다음으로 git diff
를 실행하면 아무것도 나오지 않을 것이다. 이유는 간단하다. git diff
는 working directory와 stage area의 차이를 보여줄 뿐이다. 따라서 git add
를 통해서 변동 사항을 stage area로 보내면 git diff
로 나오지 않는다.
반면에 git diff HEAD
는 현재 branch의 HEAD와의 차이를 보여준다. 따라서, stage area에 있다하더라도, commit되지 않았기 때문에 차이가 나오게 된다.
git diff HEAD
diff --git a/colors.txt b/colors.txt
index 960ca33..d1f49b6 100644
--- a/colors.txt
+++ b/colors.txt
@@ -3,4 +3,4 @@ orange
yello
green
blue
-purple
\ No newline at end of file
+indigo
\ No newline at end of file
따라서, 정리하자면 git diff HEAD
를 실행하면 변경사항이 stage에 등록되었든, 안되었던 상관없이, 현재 branch의 HEAD와 비교한 결과를 내놓는다.
이 두 옵션은 stage에 등록된 사항과 last commit의 차이를 보여준다.
가령 다음과 같이 numbers.txt
를 새로 만들어보도록 하자.
touch ./numbers.txt
echo "one" >> ./numbers.txt
git add ./numbers.txt
.numbers.txt
를 stage영역에 추가한 것을 알 수 있다. git diff HEAD
로 확인하면 차이가 나올 것이다.
git diff HEAD
diff --git a/numbers.txt b/numbers.txt
new file mode 100644
index 0000000..43dd47e
--- /dev/null
+++ b/numbers.txt
@@ -0,0 +1 @@
+one
\ No newline at end of file
다음으로 numbers.txt
에 "two"를 추가해주되, stage영역에 넣지 않도록 하자. git diff --cached
를 실행시키면 아무것도 나오지 않을 것이다.
echo "two" >> ./numbers.txt
git diff HEAD
를 사용하면 다음과 같이 tow
가 추가된 것을 볼 수 있다.
git diff HEAD
diff --git a/numbers.txt b/numbers.txt
new file mode 100644
index 0000000..1d202ca
--- /dev/null
+++ b/numbers.txt
@@ -0,0 +1 @@
+onetwo
그런데, git add
를 하지 않았으므로 stage영역에는 없다. 따라서, git diff --cached
또는 git diff --staged
호출 시에는 수정된 결과가 나오지 않는다.
git diff --staged
diff --git a/numbers.txt b/numbers.txt
new file mode 100644
index 0000000..5626abf
--- /dev/null
+++ b/numbers.txt
@@ -0,0 +1 @@
+one
two
가 없다는 것을 알 수 있다. 이는 two
는 아직 stage 영역에 없으므로 git diff --staged
시에 최신 commit과 비교가 안되는 것이다.
그렇다면 numbers.txt
에 대한 수정 사항을 stage 영역으로 보내버린 다음에 git diff --staged
로 확인하도록 하자.
git add ./numbers.txt
git diff --staged
diff --git a/numbers.txt b/numbers.txt
index 5626abf..1d202ca 100644
--- a/numbers.txt
+++ b/numbers.txt
@@ -1 +1 @@
-one
+onetwo
이제 git diff --staged
에 우리의 반영이 나올 것이다.
반면에 git diff
는 아무것도 안나올 것이다. 이는 stage와 working directory를 비교하는 것이기 때문이다.
다음으로 commit
후에 git diff를 사용해보도록 하자.
git commit -m "add two2"
[master 9c05c1d] add two2
1 file changed, 1 insertion(+), 1 deletion(-)
git diff --staged
아무것도 안나올 것이다. 이는 stage
와 최신 commit이 동일하기 때문이다.
git diff
명령어는 특정 file을 지정하여 비교가 가능하다. 이는 git diff HEAD <filename>
, git diff --staged <filename>
등 각종 옵션 뒤에 file만 지정하면 된다.
colors.txt
파일과 rainbow.txt
모두 변경해보도록 하자.
echo -n "three" >> ./numbers.txt
echo -n "sky" >> ./colors.txt
두 file 모두 수정되었기 때문에 git diff
시에 다음과 같이 나온다.
git diff
diff --git a/colors.txt b/colors.txt
index d1f49b6..9c79574 100644
--- a/colors.txt
+++ b/colors.txt
@@ -3,4 +3,5 @@ orange
yello
green
blue
-indigo
\ No newline at end of file
+indigo
+sky
\ No newline at end of file
diff --git a/numbers.txt b/numbers.txt
index 1d202ca..203c0da 100644
--- a/numbers.txt
+++ b/numbers.txt
@@ -1 +1,3 @@
onetwo
+
+three
\ No newline at end of file
그러나, 내가 보고 싶은 것은 colors.txt
만 이라면 어떻게 해야할까?? 이 경우는 git diff
시에 filename을 지정하면 된다.
git diff ./colors.txt
diff --git a/colors.txt b/colors.txt
index d1f49b6..9c79574 100644
--- a/colors.txt
+++ b/colors.txt
@@ -3,4 +3,5 @@ orange
yello
green
blue
-indigo
\ No newline at end of file
+indigo
+sky
\ No newline at end of file
colors.txt
만 확인된 것을 볼 수 있다.
그리고 둘 다 git add
를 통해서 stage로 보내버리도록 하자.
git add .
이제 git diff
로는 안나오지만 git diff HEAD
를 통해서 확인할 수 있다. 이때 numbers.txt
만 확인해보도록 하자.
git diff HEAD numbers.txt
diff --git a/numbers.txt b/numbers.txt
index 1d202ca..203c0da 100644
--- a/numbers.txt
+++ b/numbers.txt
@@ -1 +1,3 @@
onetwo
+
+three
\ No newline at end of file
--staged
역시도 마찬가지이다. colors.txt
만 보고 싶다면 다음과 같이 쓸 수 있다.
git diff --staged ./colors.txt
diff --git a/colors.txt b/colors.txt
index d1f49b6..9c79574 100644
--- a/colors.txt
+++ b/colors.txt
@@ -3,4 +3,5 @@ orange
yello
green
blue
-indigo
\ No newline at end of file
+indigo
+sky
\ No newline at end of file
이렇게 특정 file을 지정하여 diff를 보여주는 범위를 줄일 수 있는 것이다.
git diff branch1 ..branch2
를 통해서 branch들끼리의 비교가 가능하다. branch1..branch2
또는 branch1 branch2
와 같이 ..
을 써주거나 space로 띄워주면 된다.
먼저 master
branch에서 numbers.txt
를 다음과 같이 수정하도록 하자.
git status
On branch master
nothing to commit, working tree clean
echo "one" > ./numbers.txt
다음으로 commit까지 시켜주도록 하자.
git add ./numbers.txt
git commit -m "modify numbers.txt"
다음으로 odd-numbers
branch를 만들어주도록 하자.
git switch -c "odd-numbers"
그리고 numbers.txt
를 다음과 같이 수정하도록 하자.
echo -n "three" >> ./numbers.txt
echo -n "five" >> ./numbers.txt
cat ./numbers.txt
one
threefive
다음으로 commit을 올리도록 하자.
git add ./numbers.txt
git commit -m "update numbers.txt"
다음으로 master
branch와 비교해보도록 하자.
git diff master odd-numbers
diff --git a/numbers.txt b/numbers.txt
index 5626abf..4d90fd8 100644
--- a/numbers.txt
+++ b/numbers.txt
@@ -1 +1,2 @@
one
+threefive
\ No newline at end of file
a/numbers.txt
가 master
branch의 text file이고 b/numbers.txt
가 odd-numbers
이다. 이는 git diff를 실행할 때 branch name을 순서대로 쓴 것이 반영된 것이다.
다음과 같이도 사용이 가능하다.
git diff master..odd-numbers
diff --git a/numbers.txt b/numbers.txt
index 5626abf..4d90fd8 100644
--- a/numbers.txt
+++ b/numbers.txt
@@ -1 +1,2 @@
one
+threefive
\ No newline at end of file
둘 다 차이는 없으므로, 마음에 드는 것으로 쓰는 것이 좋다.
두 commit 간에 어떤 변경 사항이 있는 지 알고 싶다면 git diff commit1..commit2
로 쓰면 된다. 여기서 commit은 commit hash를 사용하면 된다.
어떤 commit이 있는 지 알기위해서 git log
를 사용하자.
git log
...
commit a575ba5f438ac7706db7f3965192b59a59e63615 (HEAD -> odd-numbers)
Author: calo <calo@calo.com>
Date: Thu Aug 1 15:50:18 2024 +0900
update numbers.txt
commit ac4386814606aa70707fe3a7bb7bb55d6e770658 (master)
Author: calo <calo@calo.com>
Date: Thu Aug 1 15:41:48 2024 +0900
modify numbers.txt
commit da9220eedcc35149bc88a375226e66905d90f810
Author: calo <calo@calo.com>
Date: Thu Aug 1 15:36:08 2024 +0900
update files
첫번째 commit과 3번째 commit을 비교하기 위해서 다음과 같이 쓸 수 있다.
git diff a575ba5f438ac7706db7f3965192b59a59e63615 da9220eedcc35149bc88a375226e66905d90f810
diff --git a/numbers.txt b/numbers.txt
index 4d90fd8..203c0da 100644
--- a/numbers.txt
+++ b/numbers.txt
@@ -1,2 +1,3 @@
-one
-threefive
\ No newline at end of file
+onetwo
+
+three
\ No newline at end of file
자세한 정보들을 나오는 것을 볼 수 있다. 참고로 a575ba5f438ac7706db7f3965192b59a59e63615
는 HEAD
와 마찬가지이기 때문에 다음과 같이도 쓸 수 있다.
git diff HEAD da9220eedcc35149bc88a375226e66905d90f810
diff --git a/numbers.txt b/numbers.txt
index 4d90fd8..203c0da 100644
--- a/numbers.txt
+++ b/numbers.txt
@@ -1,2 +1,3 @@
-one
-threefive
\ No newline at end of file
+onetwo
+
+three
\ No newline at end of file
위와 같은 결과가 나온다.