Git
Git Stash
Version Control
Code Management
Software Development

How to preview the changes that git stash apply will make?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Git is a powerful version control system that enables developers to track and manage changes in their codebases. One of the many useful features in Git is the stash, which allows you to temporarily save changes that you don't want to commit immediately. This comes in handy when you want to switch between branches without losing your current work. However, before applying a stash with git stash apply, you might want to preview the changes to avoid conflicts or unexpected modifications. In this article, we'll explore how to preview these changes effectively using Git commands.

Understanding Git Stash

A stash in Git is essentially a stack of incomplete changes that you can store temporarily. The command git stash save (or simply git stash) saves your working directory and index changes, allowing you to revert to a clean working state. These stashes can then be reapplied using the git stash apply or git stash pop commands.

Previewing Changes Before Applying a Stash

To preview the changes that git stash apply will introduce into your working directory, you can utilize the git diff command. This command can compare various commit ranges, branches, or worktrees, which makes it incredibly versatile.

Steps to Preview Changes

  1. Identify the Stash you Want to Preview: To view the list of stashed changes, use:
bash
   git stash list

This will provide you with a list that's similar to:

 
   stash@{0}: WIP on branch: initial commit
   stash@{1}: WIP on branch: refactor

Identify the desired stash you wish to inspect, such as stash@{0}.

  1. Use git stash show to Get a Summary:
    For a quick overview of the changes in a stash, use:
bash
   git stash show stash@{0}

This provides a summary of file modifications, but not detailed differences.

  1. Get Detailed Modifications with git stash show -p:
    To see detailed differences (or patch details), including line-by-line changes:
bash
   git stash show -p stash@{0}

This outputs a standard diff format that displays precise line changes, deletions, and additions.

Example Scenario

Assume you have made changes to two files foo.txt and bar.txt, and stashed those changes:

bash
git stash

You decide you want to inspect stash@{0} before applying it:

  • List Stashes:
bash
  git stash list
  • Summary of Changes:
bash
  git stash show stash@{0}
  • Detailed Diff:
bash
  git stash show -p stash@{0}

This gives you an output like:

 
1  diff --git a/foo.txt b/foo.txt
2  index e69de29..f1311f8
3  --- a/foo.txt
4  +++ b/foo.txt
5  @@ -0,0 +1 @@
6  +Hello, World!
7  diff --git a/bar.txt b/bar.txt
8  index e69de29..asdf123
9  --- a/bar.txt
10  +++ b/bar.txt
11  @@ -0,0 +1 @@
12  +Some changes here

Key Considerations

Before applying a stash, consider the following:

  • Conflict Potential: Applying a stash may result in conflicts if there are new changes in your working directory that overlap with those saved in the stash.
  • Stash Naming: By default, stashes are named with references like stash@{0}. You can give them descriptive messages to make identification easier, for example, git stash save "add login feature".
  • Safety: Use git stash apply if you want to keep the stash in the list post-application, or git stash pop which will apply and then drop the stash.

Summary Table

CommandPurposeExample Usage
git stash listLists all created stashesgit stash list
git stash showShows a brief summary of a stash's changesgit stash show stash@{0}
git stash show -pShows detailed differences in a stashgit stash show -p stash@{0}
git stash applyApplies a stash without removing it from listgit stash apply stash@{0}
git stash popApplies a stash and removes it from the listgit stash pop stash@{0}

Conclusion

Previewing stash changes before applying them can prevent complications and ensure that your working directory remains stable. The git stash show -p command allows for a detailed inspection of changes, providing a clear insight into what modifications will occur upon application. By understanding and utilizing Git stash previews, you can maintain a more organized and conflict-free development workflow.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.