Git
Understanding git show and Stash List in Git
When working with Git, the stash is a powerful tool for temporarily saving changes that are not ready to be committed. If you’ve ever needed to switch branches or pull updates without losing your progress, the git stash
command comes to the rescue.
This blog dives into how to view and manage your stashes using git stash list
and git show
.
What Is Git Stash?
The git stash
command allows you to save uncommitted changes without committing them to the repository. This is useful for:
- Saving progress to work on something else temporarily.
- Switching branches without losing changes.
- Cleaning your working directory before running commands like
git pull
.
When you stash changes, they are stored in a stack-like structure, and you can retrieve or inspect them later.
1. Viewing Stash Entries with git stash list
The git stash list
command shows all stashes in your repository in reverse chronological order. Each stash entry includes:
- Stash ID: A unique identifier like
stash@{0}
, where0
is the most recent stash. - Branch Reference: Indicates which branch the stash was created from.
- Message: A description of the stash, often based on the last commit message.
Example Usage
git stash list
Output:
stash@{0}: WIP on main: 1234567 Add feature A
stash@{1}: WIP on dev: abcdefg Fix bug B
stash@{2}: WIP on main: 789abcd Update README
This shows three stashes, with stash@{0}
being the most recent.
2. Inspecting a Stash with git show
To view the details of a specific stash, use git show
followed by the stash ID. This displays the changes stored in that stash.
Example Usage
git show stash@{0}
Output:
diff --git a/file1.txt b/file1.txt
index abc123..def456 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,4 @@
Line 1
+Added Line
Line 2
Line 3
The output includes a diff of the stashed changes, showing what was added, removed, or modified.
3. Combining git stash list
and git show
To explore all stashes and inspect specific ones:
- List all stashes:
git stash list
- Inspect a particular stash:
git show stash@{n}
Replacen
with the desired stash index.
4. Additional Options with git stash list
The git stash list
command supports various formatting options. For example:
- Customize Output Format:
git stash list --pretty=format:"%gd: %s"
Output:stash@{0}: WIP on main: Add feature A stash@{1}: WIP on dev: Fix bug B
- Filter by Content: Combine with
grep
to search specific stashes:git stash list | grep "README"
5. Practical Use Cases
Here are some scenarios where git stash list
and git show
are particularly useful:
- Recover Old Work: Use
git stash list
to locate an older stash and inspect its content withgit show
. - Debugging Changes: Before applying a stash, review it with
git show
to ensure you’re restoring the correct changes. - Documenting Progress: By examining stash messages and content, you can document progress without committing intermediate states.
6. Common Stash Commands
Here’s a quick reference to complement your stash workflows:
- Stash Changes:
git stash
- Apply the Most Recent Stash:
git stash apply
- Apply a Specific Stash:
git stash apply stash@{n}
- Drop a Stash:
git stash drop stash@{n}
- Clear All Stashes:
git stash clear
Conclusion
The combination of git stash list
and git show
provides a robust way to manage and inspect your stashed changes. Whether you’re juggling multiple tasks, debugging, or recovering old work, these commands help maintain control and visibility over your changes.
Mastering Git stash workflows enhances productivity and ensures you never lose track of valuable work.