When using Git, you will frequently need to compare newer and older versions of the same files. The git diff command can be used to view changes across the files.

What You’ll Learn

What You’ll Need

First, initialize and set up your Git repository.

linux:~$ mkdir files
linux:~$ cd files/
linux:~/files$ git init
Initialized empty Git repository in /home/expert/files/.git/
linux:~/files$ git config --global user.name "Uncle Bob"
linux:~/files$ git config --global user.email uncle@bob.com
linux:~/files$ git branch -m master main

Before you use the git diff command, you will need to commit a file to your Git local repository.

Use your text editor to create a file called “musicians.” Add your five favorite musicians to the file. Save the file inside your Git repository.

musicians-file

You can copy and paste the following text into your text editor if needed.

linux:~/files$ cat musicians
Rick Astley
Adele
George Michael
Paul McCartney
Madonna

Stage and commit the file.

linux:~/files$ git add musicians
linux:~/files$ git commit -m "Add musicians file"
[main (root-commit) f9a67dc] Add musicians file
 1 file changed, 5 insertions(+)
 create mode 100644 musicians

You want to replace the musician in line 3 with a new musician. In my example, I have replaced George Michael with Lady Gaga. After you make changes to line 3, save your file.

musicians-file

Next, pretend you have taken an hour-long lunch; 60 pretend minutes go by.

When you get back, you have forgotten what you have done in Git. To refresh your memory, check the status of your repository.

linux:~/files$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   musicians

no changes added to commit (use "git add" and/or "git commit -a")

The musicians file is listed as modified; this confirms that you have made changes to the file. Unfortunately, you cannot remember the changes that you have made. To compare the newest version of the musicians file in your working directory to the older version of the musicians file in your latest commit, you can use the git diff command.

linux:~/files$ git diff
diff --git a/musicians b/musicians
index 4b10feb..f560f31 100644
--- a/musicians
+++ b/musicians
@@ -1,5 +1,5 @@
 Rick Astley
 Adele
-George Michael
+Lady Gaga
 Paul McCartney
 Madonna
\ No newline at end of file

Let’s break down the output of the git diff command. The top section of the git diff command output is the legend that enables us to decipher the output of the rest of the command.

git-diff

Here is an explanation of the previous output:

- - - a/musicians: Notice the three minus signs. The minus sign will later be used in the output of the command to signify lines that are only in the older version of our musicians file.

+++ b/musicians: Notice the three plus signs. The plus sign will later be used in the output of the command to signify lines that are only in the newer version of our musicians file.

@@ -1,5: The numbers 1 and 5 represent the starting line and how many lines in total are being displayed. Because there is a minus sign in front of the numbers 1 and 5, this output signifies the lines being displayed in the older musicians file. For example, the -1,5 tells us that later in the git diff output, we will see the data in the older musicians file starting at line 1, and the output will display a total of five lines.

+1,5 @@: The numbers 1 and 5 represent the starting line and how many lines in total are being displayed. Because there is a plus sign in front of the numbers 1 and 5, this output signifies the lines being displayed in the newer musicians file. For example, the +1,5 tells us that later in the git diff output, we will see the data in the new musicians file starting at line 1, and the output will display a total of five lines.

Now that we understand the legend part of the git diff command, we can properly interpret the rest of the command. The bottom half of the git diff command displays the data that resides in both the older and newer musicians files.

git-diff

Here is an explanation of the previous output:

Remember, based on the git diff legend output, we determined the output will start at line 1 for both the older and the newer musicians files and will show a total of five lines. In the output above, we can see lines 1 through 5 in both of the files.

There are several lines that do not have a plus or minus sign, signifying lines that reside in both the older and the newer musicians files. In my example, that would be Rick Astley, Adele, Paul McCartney, and Madonna.

There is one line that has a minus sign (-George Michael). This shows that George Michael was in line 3 in the older musicians file.

There is one line that has a plus sign. This shows that George Michael is no longer in line 3 of the newer musicians file and has been replaced by Lady Gaga (+Lady Gaga). Sorry, George!

Stage and commit the file.

linux:~/files$ git add musicians
linux:~/files$ git commit -m 'Add Lady Gaga to line 3 in the musicians file'
[main d5da2bc] Add Lady Gaga to line 3
 1 file changed, 1 insertion(+), 1 deletion(-)

Create a new file in your working directory. Use your text editor to create a file called “countries.” Within the countries file, list the top 10 countries that you want to travel to. Save the file inside your Git repository.

countries-file

You can copy and paste the following text into your text editor if needed.

linux:~/files$ cat countries
England
Egypt
Australia
New Zealand
United States
France
South Africa
Japan
India
Spain

Stage and commit the file.

linux:~/files$ git add countries
linux:~/files$ git commit -m 'Add countries file'
[main 812e4c7] Add countries file
 1 file changed, 10 insertions(+)
 create mode 100644 countries

You want to replace the country in line 3 with a new country. In my example, I have replaced India with Brazil. After you make changes to line 9, save your file.

countries-file

Next, pretend you have taken another hour-long lunch; 60 pretend minutes go by.

When you get back, you have again forgotten what you have done in Git. To refresh your memory, check the status of your repository.

linux:~/files$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   countries

no changes added to commit (use "git add" and/or "git commit -a")

The countries file is listing as modified; this confirms that you have made changes to the file. Unfortunately, you cannot remember the changes that you have made to the file. To compare the newest version of the countries file to the older version of the countries file, you can use the git diff command.

linux:~/files$ git diff
diff --git a/countries b/countries
index bce1644..6a015fc 100644
--- a/countries
+++ b/countries
@@ -6,5 +6,5 @@ United States
 France
 South Africa
 Japan
-India
+Brazil
 Spain
\ No newline at end of file

Attempt to read the output of your git diff command and answer the following questions:

  1. What is the starting line that is being displayed in both the older and newer versions of the countries files?

  2. How many lines are being displayed in the older and newer versions of the countries files?

  3. What is the difference between the older and newer versions of the countries files?

The answers are:

  1. Line 6.

  2. Five lines for each file.

  3. India was in line 9 of the older countries file. Brazil was in line 9 of the newer countries file.

You might be asking: Why are only five lines displayed in the output of the command when there are 10 lines in the countries file? Imagine if you had a large script that had thousands of lines of code, and you made only a change to one line of code. It wouldn’t be feasible to display all 1000 lines of code just to see one line of code that has been changed. Because of this, the git diff command doesn’t display the entire contents of a file; it will only show portions of a file that were modified. These condensed views of the files are referred to as chunks.

You want to replace the musician in line 5 with a new musician. In my example, I have replaced Madonna with Miley Cyrus. After you make changes to line 5, save the file.

musicians-file

Next, pretend you have taken another hour-long lunch; 60 pretend minutes go by. When you get back, you have yet again forgotten what you have done in Git. To refresh your memory, check the status of your repository.

linux:~/files$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   countries
        modified:   musicians

no changes added to commit (use "git add" and/or "git commit -a")

You have confirmed that you have made changes to the musicians and countries files. By default, the git diff command compares all changed files in your working directory to their respective files in the latest commit. Because of this default behavior, you should see both the musicians and countries files in the output of the command. Run the git diff command to view the changes for these files.

linux:~/files$ git diff
diff --git a/countries b/countries
index bce1644..6a015fc 100644
--- a/countries
+++ b/countries
@@ -6,5 +6,5 @@ United States
 France
 South Africa
 Japan
-India
+Brazil
 Spain
\ No newline at end of file
diff --git a/musicians b/musicians
index f560f31..ae1a1aa 100644
--- a/musicians
+++ b/musicians
@@ -2,4 +2,4 @@ Rick Astley
 Adele
 Lady Gaga
 Paul McCartney
-Madonna
\ No newline at end of file
+Miley Cyrus

In the output above, can you see how many lines are displayed for each file? Can you determine what has changed within each file? To view only changes for a specific file, use the git diff command followed by the filename.

linux:~/files$ git diff musicians
diff --git a/musicians b/musicians
index f560f31..ae1a1aa 100644
--- a/musicians
+++ b/musicians
@@ -2,4 +2,4 @@
 Rick Astley
 Adele
 Lady Gaga
 Paul McCartney
-Madonna
\ No newline at end of file
+Miley Cyrus
\ No newline at end of file

Stage your files only; do not commit them.

linux:~/files$ git add musicians countries

Run the git diff command.

linux:~/files$ git diff

Why is there no output when running the command above?

Remember, by default, the git diff command compares all changed files in your working directory to their respective files in the latest commit. When you ran the git add command, you moved the countries and musicians files from the working directory to the staging area.

To compare all changed files in your staging area to their respective files in their latest commit, run the command.

linux:~/files$ git diff --staged
diff --git a/countries b/countries
index 7bec9d3..a1b5a31 100644
--- a/countries
+++ b/countries
@@ -6,5 +6,5 @@ United States
 France
 South Africa
 Japan
-India
+Brazil
 Spain
\ No newline at end of file
diff --git a/musicians b/musicians
index f560f31..313f98f 100644
--- a/musicians
+++ b/musicians
@@ -2,4 +2,4 @@
 Rick Astley
 Adele
 Lady Gaga
 Paul McCartney
-Madonna
\ No newline at end of file
+Miley Cyrus
\ No newline at end of file

Commit your files.

linux:~/files$ git commit -m 'Update musicians and countries files'
[main bf7abe7] Update musicians and countries files
 2 files changed, 2 insertions(+), 2 deletions(-)

Run the command to view the previous commits.

linux:~/files$ git log --oneline
1547d0a (HEAD -> main) Update musicians and countries files
812e4c7 Add countries file
d5da2bc Add Lady Gaga to line 3 in the musicians file
f9a67dc Add musicians file

View the changes to the files across two of your oldest commits by specifying their commit IDs obtained in the previous command. Make sure to specify your older commit ID first, followed by the more recent commit ID.

linux:~/files$ git diff f9a67dc d5da2bc
diff --git a/musicians b/musicians
index 4b10feb..f560f31 100644
--- a/musicians
+++ b/musicians
@@ -1,5 +1,5 @@
 Rick Astley
 Adele
-George Michael
+Lady Gaga
 Paul McCartney
 Madonna
\ No newline at end of file

You can also use the git diff command to compare files across different commits in different branches.

Create and switch to a new Git branch.

linux:~/files$ git branch new-branch
linux:~/files$ git checkout new-branch
Switched to branch 'new-branch'

You decide to add an 11th country to your list. After the changes, save your file.

countries-file

Stage and commit the files.

linux:~/files$ git add countries
linux:~/files$ git commit -m 'Add 11th country to countries file'
[new-branch cbf6133] Add 11th country
 1 file changed, 2 insertions(+), 1 deletions(-)

Compare the latest commit in the new-branch to the latest commit in the main branch by specifying the branch names at the end of the git diff command.

linux:~/files$ git diff main new-branch
diff --git a/countries b/countries
index a1b5a31..856ea9e 100644
--- a/countries
+++ b/countries
@@ -7,4 +7,5 @@
 France
 South Africa
 Japan
 Brazil
-Spain
\ No newline at end of file
+Spain
+Portugal
\ No newline at end of file

Why are we seeing Spain both removed and added to the file? After I changed branches, I likely accidently added an extra whitespace at the end of the Spain entry in the countries document. If you are seeing the same output, you can run the git diff command with the -w argument to ignore whitespace changes.

linux:~/files$ git diff main new-branch -w
diff --git a/countries b/countries
index 9238152..831b86f 100644
--- a/countries
+++ b/countries
@@ -8,3 +8,4 @@
 South Africa
 Japan
 Brazil
 Spain
+Portugal
\ No newline at end of file

Nice work! You can now interpret and use the git diff command!

Learn More