How to view changes in Git

Greg Foster
Greg Foster
Graphite software engineer

This guide will explore the different methods to view changes in Git, using commands like git diff, git status, and others.

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.

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:

    Terminal
    git diff

    When 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:

Terminal
diff --git a/example.txt b/example.txt
index 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.
  1. File comparison:

    • diff --git a/example.txt b/example.txt indicates that Git is comparing the file example.txt from two different states (from 'a' which is the pre-change state, to 'b' which is the post-change state).
  2. 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.
  3. File headers:

    • --- a/example.txt and +++ b/example.txt indicate the old file and the new file, respectively.
  4. 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.

To view what has been staged for the next commit:

  • List staged files:

    Terminal
    git diff --name-only --cached

    This 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.

If you’re interested in seeing what has changed in a specific file:

  • Git diff for a specific file:

    Terminal
    git 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.

To get a broad view of all changes (including staged, unstaged, and untracked):

  • Comprehensive status:

    Terminal
    git status -u

    This 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:

    Terminal
    git diff HEAD

    This shows all the changes in the working directory since the last commit.

For more complex scenarios, such as when you want to see a summary of changes across multiple commits or specific branches:

  • Changes between branches:

    Terminal
    git diff main..feature-branch

    Replace main and feature-branch with the respective branches you want to compare.

  • Changes over the last several commits:

    Terminal
    git diff HEAD~3..HEAD

    This 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.

On this page
Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2