[Git] Git Diff
Git Diff
- Reading git diff
- Viewing Unstaged Changes
- Viewing Staged Changes
- Viewing Working Directory Changes
- Viewing Specific File Change
- Comparing Changes Across Branches
- Comparing Changes Across Commits
Reading git diff
- Typically shows changes between one file
- Every line that is changed is marked with either + or -
- Lines that begin with - come from file A(usually the old version)
- Lines that begin with + come from file B(usually the new version)
#diff --git a/test.py b/test.py # same file test.py
#index 81d3d1s..f3c1324 13423 # meta data(don't need to care)
#--- a/test.py # to a - symbol is assigned
#+++ b/test.py # to b + symbol is assinged
#@@ -3,4 +3,5 python
#print(1)
#print(2)
#print(3)
#-print(4)
#+print(5)
#+print(6)
# * @@ -3,4: from file A(-) starting from line 3, shows 4 lines (from print(1) to print(4))
# * @@ +3,5: from file B(+) starting from line 3, shows 5 lines (from print(1) to print(6) execept print(4))
Viewing Unstaged Changes
- Lists all the changes in the working directory that are not staged for the next commit
# compares staging area and the working directory and shows all "unstaged changes"
git diff
Viewing Staged Changes
- Lists only the changes between the staging area and the last commit
# compares staging area and last commit and shows "staged changes"
git diff --staged
git diff --cached
Viewing Working Directory Changes
- Lists the changes in the working directory after the last commit(HEAD)
- Shows the combination of unstaged & staged changes
# compares HEAD and the working directory and shows "all unstaged and staged changes" since HEAD
git diff HEAD
# compare one parent above of HEAD with HEAD
git diff HEAD~1 HEAD
git diff HEAD~1 # same as above
Viewing Specific File Change
# show changes(staged) in one file
git diff --staged test.txt
# show changes(unstaged & staged) in two file
git diff HEAD test1.txt test2.txt
Comparing Changes Across Branches
- Order between the commits matters, the previous one will have the - symbol
# show changes between branches git diff {branch1_name} {branch2_name} or git diff {branch1_name}..{branch2_name}
git diff main test_branch
git diff main..test_branch
# show change between branches of only one file git diff [branch_name] {branch2_name} {file_name}
git diff main test_branch test.txt
Comparing Changes Across Commits
- Order between the commits matters, the previous one will have the - symbol
# show changes between commits git diff {commit1_hash} {commit2_hash} or git diff {commit1_hash}..{commit2_hash}
git diff 324d23 5243d2
git diff 324d23..5243d2
# show change between commits of only one file git diff {commit1_hash} {commit2_hash} {file_name}
git diff 324d23 5243d2 test.txt