How to Check If a Git Stash Is Applied

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a Git stash has been successfully applied to your working directory. We will explore the interaction between the git stash command and the repository's status, demonstrating how to verify applied changes and distinguish them from unapplied stashes.

Through practical steps, you will use git status to observe the state of your working directory before and after stashing and applying changes. You will also utilize git stash list to manage and inspect your stash entries, ensuring you can confidently determine the application status of your stashed work.


Skills Graph

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

Check git status for Applied Changes

In this step, we will explore how Git's stash command interacts with the working directory and how to check the status of your repository after applying a stash.

First, let's make sure we are in our project directory. Open your terminal and navigate to the my-time-machine directory:

cd ~/project/my-time-machine

Now, let's create a new file and add some content to simulate having uncommitted changes:

echo "This is a new feature." > feature.txt
echo "Adding some more content." >> message.txt

We have now created a new file feature.txt and modified the existing message.txt. Let's see how Git views these changes using git status:

git status

You should see output indicating that feature.txt is untracked and message.txt has been modified:

On branch 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

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        feature.txt

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

Now, let's stash these changes. Stashing is like putting your current work aside temporarily so you can switch to something else without committing incomplete changes.

git stash save "Work in progress"

You should see output confirming the stash was saved:

Saved working tree and index state On master: Work in progress

Your working directory should now be clean, as if you hadn't made those changes. You can verify this with git status:

git status

The output should show a clean working directory:

On branch master
nothing to commit, working tree clean

Now, let's apply the stash we just created. Applying a stash brings the stashed changes back into your working directory.

git stash apply

You should see output indicating the changes were applied:

On branch 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

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        feature.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9)

Finally, let's check the git status again to see the state of our working directory after applying the stash.

git status

The output should show that the changes from the stash are now back in your working directory, appearing as modified and untracked files, just like before we stashed them. This confirms that git stash apply brings the changes back without automatically staging or committing them.

Run git stash list to Verify

In this step, we will learn how to view the list of stashes we have saved using the git stash list command. This command is useful for keeping track of your temporary work.

Make sure you are still in the ~/project/my-time-machine directory.

In the previous step, we created a stash. Let's see it in the list of stashes. Type the following command:

git stash list

You should see output similar to this:

stash@{0}: On master: Work in progress

This output shows us that we have one stash saved. stash@{0} is the identifier for this specific stash. The number in the curly braces indicates its position in the stash list (0 is the most recent). "On master" tells us which branch we were on when we created the stash, and "Work in progress" is the message we provided when we saved the stash.

If you had multiple stashes, they would be listed here, with stash@{0} being the most recent, stash@{1} the next most recent, and so on.

The git stash list command is essential for managing your stashes. It allows you to see what stashes you have available and helps you identify the one you want to apply or drop.

Understanding how to list your stashes is the first step in effectively using the stash feature to manage your work in progress.

Test Unapplied Stashes

In this step, we will confirm that applying a stash does not remove it from the stash list. This is an important distinction between git stash apply and git stash pop.

Make sure you are still in the ~/project/my-time-machine directory.

In the previous steps, we created a stash and then applied it. Let's check the stash list again to see if the stash is still there.

git stash list

You should see the same output as before:

stash@{0}: On master: Work in progress

As you can see, even after applying the stash, it remains in the stash list. This means you can apply the same stash multiple times if needed, although this is not a common workflow.

If you wanted to apply the stash and also remove it from the list, you would use the git stash pop command instead of git stash apply. pop is like applying the changes and then immediately dropping the stash.

Let's demonstrate git stash pop. First, make sure your working directory is clean. Since we applied the stash, we have uncommitted changes. We can either commit them or discard them for this demonstration. Let's discard them to return to a clean state.

git restore .

This command discards all changes in the working directory. Now, check the status:

git status

The output should show a clean working directory:

On branch master
nothing to commit, working tree clean

Now, let's use git stash pop to apply the stash and remove it from the list.

git stash pop

You should see output indicating the stash was applied and then dropped:

On branch 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

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        feature.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9)

Finally, let's check the stash list again:

git stash list

This time, you should see no output, indicating that the stash list is empty:

This confirms that git stash pop removes the stash after applying it, while git stash apply leaves it in the list. Understanding the difference between apply and pop is crucial for managing your stashes effectively.

Summary

In this lab, we learned how to check if a Git stash has been successfully applied to the working directory. We started by creating and stashing changes in a project, then used git status to observe the state of the working directory before and after stashing and applying. A clean working directory after applying a stash indicates the changes were successfully brought back.

We also explored using git stash list to view the list of available stashes and confirm that a stash is removed from the list after it has been applied using git stash apply or git stash pop. This provides a clear way to verify that a specific stash is no longer pending application.