How to Check If a Git Stash Is Empty

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a Git stash is empty. We will explore different methods to inspect and verify the contents of your stashes.

You will first use the git stash show command to inspect the changes within a stash, including viewing a summary and the detailed diff. Then, you will learn how to use git stash list to verify the presence and contents of stashes. Finally, you will test these commands with non-empty stashes to solidify your understanding.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/DataManagementGroup(["Data Management"]) git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git/BasicOperationsGroup -.-> git/status("Check Status") git/BasicOperationsGroup -.-> git/diff("Compare Changes") git/DataManagementGroup -.-> git/stash("Save Changes Temporarily") subgraph Lab Skills git/status -.-> lab-560106{{"How to Check If a Git Stash Is Empty"}} git/diff -.-> lab-560106{{"How to Check If a Git Stash Is Empty"}} git/stash -.-> lab-560106{{"How to Check If a Git Stash Is Empty"}} end

Run git stash show to Inspect

In this step, we will learn how to inspect the changes stored in a Git stash using the git stash show command.

Before we can inspect a stash, we need to have one. Let's create a simple file and stash the changes.

First, make sure you are in your project directory:

cd ~/project/my-time-machine

Now, create a new file called notes.txt and add some content:

echo "Important notes for the future." > notes.txt

Check the status to see the new file:

git status

You should see notes.txt listed as an untracked file.

Now, let's stash these changes. This will save the changes without committing them and clean your working directory:

git stash save "Add important notes"

You should see output indicating that the changes have been saved and the working directory is clean.

Now that we have a stash, we can inspect its contents using git stash show. By default, git stash show shows a summary of the changes in the most recent stash.

Run the command:

git stash show

You should see output similar to this:

 notes.txt | 1 +
 1 file changed, 1 insertion(+)

This output tells us that one file (notes.txt) was changed, and it had one line added.

To see the actual content changes (a diff), you can add the -p or --patch option:

git stash show -p

This will show you the diff of the changes in the stash, similar to how git diff works. You should see something like this:

diff --git a/notes.txt b/notes.txt
new file mode 100644
index 0000000..a1b2c3d
--- /dev/null
+++ b/notes.txt
@@ -0,0 +1 @@
+Important notes for the future.

This output shows that a new file notes.txt was created and the line "Important notes for the future." was added.

The git stash show command is very useful for quickly checking what changes are contained within a stash without having to apply it. This helps you decide which stash you might want to apply or drop later.

Use git stash list to Verify

In this step, we will learn how to view a list of all the stashes you have created using the git stash list command. This is useful when you have multiple stashes and need to remember what each one contains.

Make sure you are still in your project directory:

cd ~/project/my-time-machine

In the previous step, we created one stash. Let's use git stash list to see it.

Run the command:

git stash list

You should see output similar to this:

stash@{0}: On master: Add important notes

Let's break down this output:

  • stash@{0}: This is the name of the stash. stash indicates it's a stash, and {0} is the index of the stash in the list. The most recent stash is always at index {0}.
  • On master: This tells you which branch you were on when you created the stash.
  • Add important notes: This is the message we provided when we created the stash using git stash save. If you don't provide a message, Git will automatically generate one based on the current commit and the files changed.

If you had multiple stashes, they would be listed with increasing indices, like stash@{0}, stash@{1}, stash@{2}, and so on.

Let's create another stash to see how the list changes.

First, modify the message.txt file we created in a previous lab:

echo "Adding another line for testing." >> message.txt

Now, stash these new changes:

git stash save "Add another line to message"

Run git stash list again:

git stash list

Now you should see two stashes:

stash@{0}: On master: Add another line to message
stash@{1}: On master: Add important notes

Notice that the new stash is now at index {0}, and the previous stash has moved to index {1}. Git always puts the most recent stash at the top of the list with index {0}.

The git stash list command is essential for managing multiple stashes. It provides a clear overview of your saved work, allowing you to identify and reference specific stashes when you need to apply or drop them.

Test Non-Empty Stashes

In this step, we will practice using git stash show and git stash list with multiple stashes to reinforce our understanding.

Make sure you are in your project directory:

cd ~/project/my-time-machine

We currently have two stashes. Let's list them again to refresh our memory:

git stash list

You should see:

stash@{0}: On master: Add another line to message
stash@{1}: On master: Add important notes

Now, let's inspect the contents of the first stash (stash@{0}). We can specify which stash to show by adding its index to the command:

git stash show stash@{0}

This will show the summary of the changes in the most recent stash, which is the one where we added a line to message.txt. The output should be similar to:

 message.txt | 1 +
 1 file changed, 1 insertion(+)

To see the diff for this stash, use the -p option:

git stash show -p stash@{0}

You should see the diff showing the added line in message.txt.

Now, let's inspect the second stash (stash@{1}). This is the stash where we created the notes.txt file.

git stash show stash@{1}

The output should be similar to:

 notes.txt | 1 +
 1 file changed, 1 insertion(+)

And to see the diff for this stash:

git stash show -p stash@{1}

This will show the diff for the creation of notes.txt.

Being able to list and inspect individual stashes is crucial when you have saved multiple sets of changes. It allows you to quickly identify the stash you need without having to apply them one by one.

In the next labs, we will learn how to apply and manage these stashes.

Summary

In this lab, we learned how to inspect the contents of a Git stash using the git stash show command. We first created a simple file and stashed the changes to have a stash to work with. We then used git stash show to see a summary of the changes in the most recent stash, including the number of files changed and lines added/deleted. We also explored the -p or --patch option with git stash show to view the actual diff of the changes within the stash, allowing us to see the specific lines that were added, modified, or deleted. This command is a valuable tool for quickly understanding the contents of a stash without needing to apply it.