This guide will explore the different methods to view changes in Git, using commands like git diff
, git status
, and others.
Basic concepts
Before explaining the specific commands, it’s essential to understand a few basic concepts in Git:
- Working directory: This is your local checkout or copy of the repository.
- Staging area (index): This is a layer where changes are prepared (staged) before committing them to the repository.
- Commit: This is the action of recording changes from the staging area to the repository.
Viewing untracked changes with git diff
git diff
is a versatile command used to show differences between commits, commit and working tree, etc. It can also be used to view changes in untracked files by combining it with other tools:
View differences in modified files:
Terminalgit diffWhen you run
git diff
in the terminal, it shows the differences between your working directory (where you make changes) and the index (the staging area for changes to be committed).
Suppose you have modified a file named example.txt
. After modifying the file, you run git diff
. Here's what you might see:
diff --git a/example.txt b/example.txtindex 83c0b75..a1b2c3d 100644--- a/example.txt+++ b/example.txt@@ -1,4 +1,4 @@-Hello, world!+Hello, Git world!This is an example text file.It contains multiple lines.-This is the fourth line.+This is a new fourth line.
File comparison:
diff --git a/example.txt b/example.txt
indicates that Git is comparing the fileexample.txt
from two different states (from 'a' which is the pre-change state, to 'b' which is the post-change state).
Index line:
index 83c0b75..a1b2c3d 100644
shows the SHA1 hashes of the file content before and after the changes.100644
is a mode indicating the file's permissions have not changed.
File headers:
--- a/example.txt
and+++ b/example.txt
indicate the old file and the new file, respectively.
Diff chunks:
@@ -1,4 +1,4 @@
is the hunk header. It shows the context of the changes. The numbers mean the diff affects lines 1 through 4 of the old file and lines 1 through 4 of the new file.- Lines beginning with a
-
indicate lines that have been removed or modified from the original file. - Lines beginning with a
+
indicate new or modified lines as they appear in the current working version.
- The line
-Hello, world!
was replaced with+Hello, Git world!
, indicating that "world" was changed to "Git world". - Similarly,
-This is the fourth line.
changed to+This is a new fourth line.
, showing that the text on this line was modified.
Listing All Staged Files
To view what has been staged for the next commit:
List staged files:
Terminalgit diff --name-only --cachedThis command shows the names of files that are staged.
Staged files in Git are those that have been marked for inclusion in the next commit, indicating that Git should track their current changes. This staging process is done via the
git add
command, which moves changes from the working directory to the staging area.
Viewing changes in a specific file
If you’re interested in seeing what has changed in a specific file:
Git diff for a specific file:
Terminalgit diff HEAD -- <file>Replace
<file>
with the file path. This command compares the latest commit (HEAD
) with the current state of a specific file in your working directory.
Listing all changes in the repository
To get a broad view of all changes (including staged, unstaged, and untracked):
Comprehensive status:
Terminalgit status -uThis command lists each file that is modified, staged, or untracked in the repository. For a more detailed view, including the exact lines that have changed:
Detailed diff of all changes:
Terminalgit diff HEADThis shows all the changes in the working directory since the last commit.
Advanced usage
For more complex scenarios, such as when you want to see a summary of changes across multiple commits or specific branches:
Changes between branches:
Terminalgit diff main..feature-branchReplace
main
andfeature-branch
with the respective branches you want to compare.Changes over the last several commits:
Terminalgit diff HEAD~3..HEADThis shows the changes across the last three commits.
For further reading on viewing changes in Git, see the official documentation on the git diff command.