AlgoMaster Logo

Viewing Changes (git diff)

Last Updated: January 3, 2026

6 min read

Understanding the differences between commits, branches, and your current working directory is a vital skill in Git.

The git diff command is your go-to tool for visualizing those differences. It helps you see what has been changed, added, or removed, and can be a lifesaver in the development process.

The Basics of git diff

At its core, git diff shows the differences between various states of your Git repository.

This could be differences between:

  • Your working directory and the staging area
  • The staging area and the last commit
  • Two different commits or branches

The command outputs a line-by-line comparison of files, clearly indicating what has changed, been added, or deleted.

Understanding the Output

The output of git diff can be complex at first glance. Here's a simplified explanation of what to look for:

  • Lines starting with a minus (-) indicate lines that were removed.
  • Lines starting with a plus (+) indicate lines that were added.
  • Context lines (lines without a prefix) provide context for the changes.

Here's an example of a git diff output:

In this output:

  • The first line shows the file being compared.
  • The @@ line indicates the hunk of differences, showing context.
  • The line with a minus indicates that the original line has been changed, while the line with a plus shows its new version.

Comparing Different States

Working Directory vs. Staging Area

One common use case for git diff is to compare your working directory with the staging area. This shows what changes you have made but haven't yet staged for commit.

The command for this is simply:

You can also specify a file:

This allows you to see what modifications are pending for that specific file before staging or committing.

Staging Area vs. Last Commit

After staging your changes, you might want to review what you've staged against the last commit. This helps ensure you are committing exactly what you intend to.

To do this, use:

The --cached flag compares the staging area with the last commit. If you want to see the changes for a specific file, just add the filename:

This command is particularly useful just before you run git commit to ensure that only the desired changes are included in your next commit.

Comparing Commits

Between Two Commits

Git allows you to compare any two commits. This can be useful for understanding what has changed over time. The command format is:

You can specify commit hashes, tags, or branch names. For example:

This compares the last commit (HEAD) to its immediate predecessor (HEAD~1).

Visualizing Commit Differences

Sometimes, using the commit hash directly provides more context. For instance:

This command will show the differences between the two specific commits, allowing you to analyze what changes were made over time.

Between Branches

You can also compare entire branches. This is useful when you want to see what changes are in a feature branch that aren't in the main branch. The command is:

This shows the changes that would be applied to main if you were to merge feature-branch into it.

Using Diff Options

The git diff command comes with several options that can enhance its functionality. Here are some noteworthy ones:

Word Diff

Sometimes, you may want to see changes at the word level instead of line level. You can achieve this by using the --word-diff option:

This will show insertions and deletions at the word level, making it easier to spot changes in longer lines.

Stat Option

If you want a summary of changes, the --stat option is very useful. It provides a concise overview of how many files were changed, along with the number of insertions and deletions:

This gives you a quick summary before diving deeper into the specifics.

Color Output

Git typically provides color-coded output, but if you find it difficult to read, you can adjust your settings. To always see color output, you can set:

This makes it easier to differentiate between added and removed lines in the output.

Now that you understand how to effectively use git diff, you are ready to explore .gitignore.

In the next chapter, we will look at how to manage which files should be tracked by Git and which should be ignored, ensuring a clean and efficient workflow.