Sling Academy
Home/DevOps/Git Stash Command: The Complete Guide

Git Stash Command: The Complete Guide

Last updated: January 27, 2024

Introduction to Git Stash

Working with version control systems like Git is an integral part of modern software development. The git stash command is a powerhouse for developers who need to switch context quickly, save changes without committing, and keep a clean working directory. Understanding how to effectively use stashing can improve workflow efficiency. This guide will provide a comprehensive overview of the git stash command, including basic and advanced usage examples.

Understanding Git Stash

The git stash command temporarily shelves (or stashes) changes you’ve made to your working directory so you can work on something else, and then come back and re-apply them later on. Stashed changes are not part of your git repository until you apply them to a branch.

Basic Stashing

git stash

The above command takes the modified tracked files and stages any staged changes, then saves them on a stack of unfinished changes that you can reapply at any time.

Stashing Untracked Files

git stash --include-untracked

Or equivalently:

git stash -u

This includes any untracked files in the stash.

Creating a Named Stash

git stash push -m "My stash message"

Provides a custom message to your stash, helpful for identifying different stashes.

Listing Stashes

git stash list

Lists all the stashed changes in a stack-like format.

Applying a Stash

git stash apply

This will apply the most recent stashed changes. To apply a specific stash:

git stash apply [email protected]{n}

Where n is the stash index.

Popping a Stash

git stash pop

Applies the most recent stash and removes it from the stash list.

Exploring Advanced Stash Usage

For developers needing to collaborate or manage multiple changes, understanding the advanced features of git stash is vital.

Showing Stash Differences

git stash show

Displays the differences the stash would make if applied.

git stash show -p

To show the actual patch.

Stashing with keeping Index

git stash --keep-index

This stashes your changes but keeps them in your index as well.

Stashing Specific Files

git stash push [email protected]{n} -- file1 file2

Allows you to stash specific files rather than the whole working directory.

Creating Branch from Stash

git stash branch new-branch-name [email protected]{n}

Creates a new branch from the stash. If you don’t specify a stash, Git will use the last one created.

Clearing Stashes

git stash clear

This command will remove all the stashed entries.

Stash Conflicts

When re-applying a stash to the working directory, it’s possible to encounter merge-conflicts. In such cases, you resolve them just like you would do with a typical git merge.

Use Cases of Git Stash

Stashing proves beneficial in various scenarios:

  • Switching branches to work on higher priority tasks without committing half-done work.
  • Cleaning the working directory without losing local changes.
  • Temporarily storing changes to deal with it later.
  • Integrating workflows through applying stashes across branches.

Conclusion

The functionality of git stash is profound, enabling flexibility and cleanliness in managing the project’s working directory. With this comprehensive guide, developers can navigate stashing with more confidence, preserving work efficiently as they switch contexts or delay decisions on incomplete changes.

Next Article: How to discard local changes in Git (unstage modified files)

Previous Article: Git: How to compare changes across commits

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide