NeuroAgent

How to View Git Stash Contents Without Applying

Learn how to view Git stash contents without applying them using git stash show command. Inspect changes, compare stashes, and make informed decisions before applying your saved work.

Question

How do I view the contents of a Git stash without applying it?

NeuroAgent

To view the contents of a Git stash without applying it, you can use the git stash show command, which displays the changes stored in your stashes without actually applying them to your working directory. This command allows you to inspect what files were modified and what changes were made before deciding whether to apply or drop the stash.

Contents

Basic Stash Viewing Commands

The primary command for viewing stash contents is git stash show. By default, this command displays a summary of changes in the most recent stash (stash@{0}).

bash
git stash show

This command will show you:

  • Which files were modified
  • The number of insertions and deletions for each file
  • A basic overview of the changes without showing the actual diff content

If you want to see the actual changes in patch format, use the -p or --patch option:

bash
git stash show -p

This displays the full diff output similar to git diff, showing exactly what changes are stored in the stash.

Listing Multiple Stashes

When you have multiple stashes, you can list them all to see which ones you want to inspect:

bash
git stash list

This command shows all your stashes with their creation timestamps, helping you identify which stash contains the changes you’re looking for.

To view a specific stash by name or reference:

bash
git stash show stash@{1}      # View the second stash
git stash show my-feature-stash  # View a stash with a custom name

Detailed Stash Inspection

For more detailed information about a stash, you can combine several options:

bash
git stash show -p --stat

The --stat option shows a summary of changes with file-by-file statistics, while -p shows the actual patch. You can also use:

  • git stash show --name-only - Shows only the names of files changed in the stash
  • git stash show --include-untracked - Shows untracked files in the stash (when used with -p)

Viewing Specific Files in Stash

You can view changes to specific files within a stash by specifying the file names:

bash
git stash show -p -- path/to/file.js
git stash show -p -- src/components/Header.jsx

This is particularly useful when you only want to inspect changes to certain files rather than the entire stash contents.

Practical Examples and Use Cases

Example 1: Before Applying a Stash

bash
# List all stashes
$ git stash list
stash@{0}: On feature-branch: WIP on feature-branch: abc1234 Add user authentication
stash@{1}: On main: WIP on main: def5678 Fix navigation menu

# View the most recent stash details
$ git stash show -p
diff --git a/src/components/Login.jsx b/src/components/Login.jsx
index a1b2c3d..e4f5g6h 100644
--- a/src/components/Login.jsx
+++ b/src/components/Login.jsx
@@ -10,6 +10,10 @@ function Login() {
   const [password, setPassword] = useState('');
   const [error, setError] = useState('');
 
+  const handleSubmit = (e) => {
+    e.preventDefault();
+    // Authentication logic here
+  };
+
   return (
     <form onSubmit={handleSubmit}>
       {/* Form fields */}

# View changes to specific file
$ git stash show -- src/components/Login.jsx

Example 2: Comparing Stashes

You can compare different stashes to see what changes were made:

bash
git stash show stash@{0} --stat
git stash show stash@{1} --stat

Example 3: Checking Untracked Files

When you’ve stashed untracked files:

bash
git stash show -p --include-untracked

Sources

  1. Pro Git Book - Stashing
  2. Atlassian Git Tutorial - Stashing
  3. Git Documentation - git-stash

Conclusion

Viewing Git stash contents without applying them is straightforward using the git stash show command. The key takeaways are:

  • Use git stash show for a basic overview of the most recent stash
  • Add -p or --patch to see the actual changes in diff format
  • Use git stash list to see all your stashes and their references
  • You can view specific files by adding paths to the command
  • Combine options like --stat and --name-only for different viewing formats

These commands help you make informed decisions about which stashes to apply, modify, or discard, giving you full control over your stashed changes before integrating them into your working directory.