Introduction
In this lab, you will learn how to inspect the contents of a Git stash to determine if it contains specific changes. We will begin by making and stashing some changes in a sample repository.
You will then use the git stash show -p command to view the detailed diff of the most recent stash, allowing you to see the exact lines that were added, removed, or modified. Finally, you will explore how to examine multiple stashes to find the changes you are looking for.
Run git stash show -p
In this step, we will learn how to inspect the changes stored in a Git stash. When you use git stash, Git saves your uncommitted changes. To see exactly what was saved, you can use the git stash show command.
First, let's make some changes to a file in our my-time-machine repository. Navigate back to the directory if you are not already there:
cd ~/project/my-time-machine
Now, let's add some new content to our message.txt file. We'll use the echo command with >> to append to the file, so we don't overwrite the existing content.
echo "Adding a new line for stashing." >> message.txt
You can verify the content of the file using cat:
cat message.txt
You should see both the original line and the new line:
Hello, Future Me
Adding a new line for stashing.
Now, let's stash these changes. Remember, stashing saves your uncommitted changes so you can work on something else and come back to them later.
git stash save "Added a line for stashing demo"
You should see output indicating that the changes have been saved:
Saved working tree and index state On branch master: Added a line for stashing demo
Now that the changes are stashed, let's use git stash show to see what's in the stash.
git stash show
This command gives you a summary of the changes in the most recent stash. You might see output like this:
message.txt | 1 +
1 file changed, 1 insertion(+)
This tells us that one file (message.txt) was changed, and there was one insertion (a new line).
To see the actual content of the changes, we need to add the -p flag (which stands for patch). This will show us the diff, just like git diff.
git stash show -p
This command will display the exact lines that were added, removed, or modified in the stashed changes. The output will look similar to a standard Git diff:
diff --git a/message.txt b/message.txt
index <some_hash>..<some_hash> 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
Hello, Future Me
+Adding a new line for stashing.
The lines starting with + indicate additions. In this case, we added the line "Adding a new line for stashing."
Using git stash show -p is crucial for understanding what's in a stash before you decide to apply it or drop it. It helps you confirm that the stash contains the changes you expect.
Search Stash Diff for Changes
In the previous step, we used git stash show -p to view the changes in a stash. Sometimes, the diff output can be very long, especially in large projects. In such cases, you might want to search for specific changes within the stash diff.
Git's diff output is just text, so you can pipe it to standard Linux command-line tools like grep to search for patterns.
Let's try searching for the line we added in the previous step: "Adding a new line for stashing."
Make sure you are still in the ~/project/my-time-machine directory.
cd ~/project/my-time-machine
Now, run the git stash show -p command and pipe its output to grep. We'll search for the word "stashing".
git stash show -p | grep "stashing"
You should see the line containing the word "stashing" from the diff output:
+Adding a new line for stashing.
This demonstrates how you can use grep to filter the output of git stash show -p and find specific lines or patterns within the stashed changes.
You can use any of grep's options to refine your search. For example, you could use -i for case-insensitive search, or -C to show context lines around the match.
Being able to search the stash diff is a powerful technique, especially when you have multiple stashes or large stashes. It helps you quickly locate the changes you are interested in without having to manually scroll through the entire diff output.
Test Multiple Stashes
In real-world scenarios, you might need to stash changes multiple times. Git allows you to have multiple stashes, and they are managed as a stack. The most recent stash is at the top of the stack, referred to as stash@{0}. Older stashes are stash@{1}, stash@{2}, and so on.
Let's create another set of changes and stash them to see how this works.
First, make sure you are in the ~/project/my-time-machine directory.
cd ~/project/my-time-machine
Now, let's add a different line to message.txt:
echo "Adding a second line for another stash." >> message.txt
Verify the content:
cat message.txt
You should see three lines now:
Hello, Future Me
Adding a new line for stashing.
Adding a second line for another stash.
Now, stash these new changes:
git stash save "Added a second line for stashing demo"
You should see output indicating the new stash:
Saved working tree and index state On branch master: Added a second line for stashing demo
To see the list of stashes you have, use the git stash list command:
git stash list
You should see something like this:
stash@{0}: On master: Added a second line for stashing demo
stash@{1}: On master: Added a line for stashing demo
Notice that the most recent stash ("Added a second line...") is stash@{0}, and the previous one ("Added a line...") is stash@{1}.
You can use git stash show -p with the stash identifier to view the contents of a specific stash. For example, to view the first stash we created (stash@{1}):
git stash show -p stash@{1}
This will show you the diff for the changes in stash@{1}:
diff --git a/message.txt b/message.txt
index <some_hash>..<some_hash> 100644
--- a/message.txt
+++ b/message.txt
@@ -1 +1,2 @@
Hello, Future Me
+Adding a new line for stashing.
And to view the most recent stash (stash@{0}):
git stash show -p stash@{0}
This will show you the diff for the changes in stash@{0}:
diff --git a/message.txt b/message.txt
index <some_hash>..<some_hash> 100644
--- a/message.txt
+++ b/message.txt
@@ -2 +2,2 @@
Adding a new line for stashing.
+Adding a second line for another stash.
Being able to manage multiple stashes and inspect their contents individually is very useful when you are juggling different tasks or experiments.
Summary
In this lab, we learned how to inspect the contents of a Git stash using the git stash show command. We started by making changes to a file and then stashing those changes with a descriptive message using git stash save. We then used git stash show to get a summary of the changes and, more importantly, git stash show -p to view the detailed diff of the stashed changes, allowing us to see the exact lines that were added or modified.
The subsequent steps, which were not fully detailed in the provided content, would likely involve searching the stash diff for specific changes and testing multiple stashes to understand how to inspect different stashes in the stack. This process is crucial for verifying that a stash contains the expected modifications before applying or dropping it.



