Bijay Das logo

Guide to Git Stash

3 min read
Guide to Git Stash

Git is an essential tool for developers, allowing them to manage code changes efficiently. One of its powerful yet sometimes underutilized features is git stash. This feature helps you save your work-in-progress changes and switch to another task without losing any progress. In this blog, we will dive deep into the git stash command, explore its various options, and provide sample commands to help you master its usage.

What is Git Stash?

In simple terms, git stash temporarily shelves (or stashes) changes you've made to your working directory. This means you can save your work without committing it and revert to a clean working state. When you're ready to continue working, you can reapply the stashed changes.

Why Use Git Stash?

  • Context Switching: Quickly switch between tasks without committing incomplete changes.

  • Collaboration: Keep your local changes safe while synchronizing with team updates.

  • Experimentation: Test new ideas without committing them until you're sure.

Basic Git Stash Commands

Stashing Changes

The basic command to stash your changes is:

git stash

This command saves your local modifications away and reverts the working directory to match the HEAD commit.

Listing Stashes

To see a list of all the stashes you have saved, use:

git stash list

This will display all the stashes with their corresponding identifiers.

Applying Stashed Changes

To reapply the most recently stashed changes and remove them from the stash list, use:

git stash pop

If you want to apply the stashed changes without removing them from the stash list, use:

git stash apply

Applying Specific Stash

You can apply a specific stash from your list by referencing its identifier:

git stash apply stash@{2}

Creating a Named Stash

To create a stash with a descriptive message, use:

git stash push -m "My descriptive message"

Stashing Untracked Files

By default, git stash does not include untracked files. To stash them as well, use the -u or --include-untracked option:

git stash -u

Stashing Specific Files

You can stash specific files instead of the entire working directory:

git stash push path/to/file

Dropping a Stash

If you want to remove a specific stash, use:

git stash drop stash@{1}

Clearing All Stashes

To remove all stashed entries, use:

git stash clear

Advanced Git Stash Usage

Stashing with Keep Index

If you want to stash changes but keep the staged ones, use:

git stash push --keep-index

Stashing with Patch Mode

For more granular control over what you stash, use the --patch option:

git stash push -p

This allows you to interactively select portions of files to stash.

Creating a Branch from Stash

If you decide that the stashed changes are worth creating a new branch, use:

git stash branch <branch-name>

This command creates a new branch from the stashed changes and applies them to the branch.

Conclusion

The git stash command is a powerful tool for managing your workflow and handling context switches without losing progress. By mastering the commands and options discussed in this guide, you can significantly enhance your productivity and streamline your development process.

Remember to use git stash wisely, especially in a collaborative environment, to ensure smooth and efficient coding. Keep experimenting with different options and find the workflow that best suits your needs.

Happy coding!