How to make changes visible with Git Diff

Git Diff allows you to compare changes in files, branches and commits. It spots mistakes quickly and tracks positive developments.

$1 Domain Names – Grab your favorite one
  • Simple registration
  • Premium TLDs at great prices
  • 24/7 personal consultant included
  • Free privacy protection for eligible domains

What is Git Diff and how does it work?

Many changes are made during a project and teams often move from snapshot to snapshot. This is common whether you’re working alone on a small project in Git or working in a large team with several local repositories. This leads to different branches being created and causes commits and files to accumulate. Use the Git Command Git Diff to compare two data sources to track or check your changes effectively. This will compare two versions and show you how they differ from each other.

This makes Git Diff an important tool for daily tasks on a project. Git Status and Git Log are often used in conjunction with Git Diff. Analyzing different versions allows you to keep track of your repository and identify errors in the code. This can stop problems quickly before they escalate. The syntax of Git Diff is always the same. The command looks like this:

git diff

Adding arguments after the command shows Git which sources you want to compare.

What is an application example for Git Diff?

An illustrative example is the most practical way to explain how Git Diff works. Let’s imagine you have created a new project called “new_project” and you have inserted two text documents. These documents are called first_color.txt and second_color.txt. The document first_color.txt contains the sentence “The first color is blue”. The document second_color.txt contains the sentence “The second color is red”. Nothing happens after entering Git Diff initially, as the repository does not contain any changes that could be compared yet. You must move the two documents to the staging folder using the Git Add command. You can also find this in our handy Git cheat Sheet with PDF download. The command looks like this:

git add first_color.txt second_color.txt

Firstly, check what changes have been made and are ready for a commit with the Command Git Status. The two text documents first_color.txt and second_color.txt will be displayed.

You can make a change to the second document now. Change the statement “The second color is red” to “The second color is yellow”. This change will then be visible with Git Diff. The output will look like this:

$ git diff
diff --git a/second_color.txt b/second_color.txt
index [index]
--- a/second_color.txt
+++ b/second_color.txt
@@ -1 +1 @@
-The second color is red
\ No newline at end of file
+The second color is yellow
\ No newline at end of file

The individual lines explained

This output is a little bit confusing, so we’ll explain what is going on in each line.

  • diff --git a/second_color.txt b/second_color.txt: This shows you which input sources were compared with Git Diff. Version a is the original version, b shows the changed version.
  • index: The metadata is displayed in the index line. However, this isn’t usually of interest to users.
  • --- a/second_color.txt: Git Diff assigns the three minus signs as symbols to the original input source to track the changes.
  • +++ b/second_color.txt: The new version gets the plus sign as symbol.
  • @@ -1 +1 @@: This is the block header which summarizes the changes. The header is very short in this example, as there was only one change on one line. It states that something was changed in the first line. If you were to formulate multiple lines and replace a word on the fifth line, Git would notice it.
  • Remaining lines: The remaining lines show the exact changes. The content of the original version (“-The second color is red”) is marked with a minus and the content of the changed version (“+The second color is yellow”) is marked with a plus.

What are other comparisons with Git Diff?

You can carry out several comparisons with Git Diff. These are the most important ones are as follows:

git diff HEAD

Use this command to compare changes in the working directory with the index:

git diff <branch_1> <branch_2></branch_2></branch_1>

This command compares the commits in branch_1 with the commits in branch_2. The order of the two branches is taken into account by Git Diff:

git diff <commit_id> <commit_id></commit_id></commit_id>

This command compares the changes between two commits. You must first query the ID of each commit with Git Log to do this.

Tip

Manage all your projects from one dashboard with Deploy Now by IONOS and take advantage of its many useful features! The first month includes up to three starter projects. Try it out for free now!

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top