How to Check If a Git Repository Has Uncommitted Changes

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check for uncommitted changes in a Git repository. We will explore the essential git status command to view the overall state of your working directory and staged changes.

Furthermore, you will discover how to use the git diff command to inspect the specific modifications made to your files, allowing you to see exactly what has changed before committing. Finally, we will briefly touch upon combining checks for both staged and unstaged changes.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git/BasicOperationsGroup -.-> git/add("Stage Files") git/BasicOperationsGroup -.-> git/status("Check Status") git/BasicOperationsGroup -.-> git/diff("Compare Changes") subgraph Lab Skills git/add -.-> lab-560091{{"How to Check If a Git Repository Has Uncommitted Changes"}} git/status -.-> lab-560091{{"How to Check If a Git Repository Has Uncommitted Changes"}} git/diff -.-> lab-560091{{"How to Check If a Git Repository Has Uncommitted Changes"}} end

Run git status to View Changes

In this step, we'll learn how to use the git status command to see the current state of our Git repository. This command is your best friend when working with Git, as it tells you exactly what's going on.

First, make sure you are in your my-time-machine directory. You can do this by typing:

cd ~/project/my-time-machine

Now, let's run the git status command:

git status

You should see output similar to this:

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Let's break down this output:

  • On branch master: This tells you that you are currently on the master branch. Branches are like different timelines in your project.
  • Your branch is up to date with 'origin/master': This indicates that your local master branch is synchronized with the remote origin/master branch (we'll learn more about remote repositories later).
  • nothing to commit, working tree clean: This is the ideal state! It means there are no changes in your project that haven't been saved (committed) to your Git history.

The git status command is crucial because it helps you understand which files have been modified, which are new and untracked, and which changes are ready to be committed. It's the first command you should run after making any changes to your project to see how Git sees those changes.

Use git diff to Inspect Modifications

In this step, we'll learn how to use the git diff command to see the exact changes you've made in your files. While git status tells you which files have changed, git diff shows you what has changed within those files.

First, let's make a change to our message.txt file. Make sure you are still in the ~/project/my-time-machine directory.

Open the file using the nano editor:

nano message.txt

Add a new line to the file, for example:

Hello, Future Me
This is a new line.

Press Ctrl + X to exit, then Y to save, and Enter to confirm the filename.

Now that we've modified the file, let's see how Git sees this change using git status:

git status

You should see output indicating that message.txt has been modified:

On branch master
Your branch is up to date with 'origin/master'.

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:   message.txt

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

Git tells us that message.txt is modified and the changes are not staged for commit. This means we've changed the file, but we haven't told Git to prepare this change for a commit yet.

Now, let's use git diff to see the specific changes:

git diff

You will see output similar to this:

diff --git a/message.txt b/message.txt
index a1b2c3d..e4f5g6h 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
 Hello, Future Me
+This is a new line.

Let's understand this output:

  • Lines starting with --- and +++ show the original file (a/message.txt) and the new file (b/message.txt).
  • The line starting with @@ is called a "hunk header". It shows where the changes are located in the file. -1 +1,2 means that starting from line 1 in the original file, 1 line was removed, and starting from line 1 in the new file, 2 lines were added.
  • Lines starting with - show lines that were removed.
  • Lines starting with + show lines that were added.

In our case, we added one line, so you see a line starting with +.

The git diff command is incredibly useful for reviewing your changes before staging or committing them. It helps you catch mistakes and ensure you are only including the intended modifications in your commits.

Press q to exit the diff view and return to the command line.

Combine Staged and Unstaged Checks

In this step, we'll explore how Git handles both staged and unstaged changes and how git diff can be used to inspect them.

Recall from the previous step that we modified message.txt but did not stage the changes. Let's stage the changes now using git add:

git add message.txt

Now, run git status again:

git status

The output should show that the changes are now staged:

On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   message.txt

Git now shows Changes to be committed, indicating that the modifications to message.txt are in the staging area, ready for the next commit.

What happens if we run git diff now?

git diff

You might be surprised to see no output. This is because git diff by default shows the difference between your working directory and the staging area. Since the changes in our working directory are now the same as the changes in the staging area (because we just added them), there is no difference to show.

To see the difference between the staging area and the last commit, we need to use a different form of the git diff command:

git diff --staged

Or, equivalently:

git diff --cached

Both commands do the same thing. Let's run it:

git diff --staged

Now you should see the difference between the staged changes and the last commit:

diff --git a/message.txt b/message.txt
index a1b2c3d..e4f5g6h 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
 Hello, Future Me
+This is a new line.

This shows the exact change we made: adding the line "This is a new line.".

Understanding the difference between git diff (working directory vs. staging area) and git diff --staged (staging area vs. last commit) is fundamental to using Git effectively. It allows you to carefully review your changes at different stages before making a permanent commit.

Summary

In this lab, we learned how to check for uncommitted changes in a Git repository. We started by using the git status command to get an overview of the repository's state, including the current branch and whether there are any modified, new, or untracked files. This command is essential for understanding what changes are present.

Next, we explored the git diff command to inspect the specific modifications made within files. While git status tells us which files have changed, git diff shows the exact line-by-line differences, allowing us to review the content of our changes before staging or committing them.