Git Stash: A Comprehensive Guide

Git is a powerful version control system that allows developers to manage changes to their codebase efficiently. One of the useful features of Git is git stash. This feature allows you to temporarily save changes that you are not ready to commit yet, without losing them. This tutorial will guide you through the basics and some advanced usage of git stash.

What is Git Stash?

git stash temporarily shelves (or stashes) changes you’ve made to your working directory. This can be useful if you need to switch branches or work on something else without committing your current changes.

When to Use Git Stash?

  • When you want to switch branches but have uncommitted changes.
  • When you need to pull in changes from a remote branch but have modifications that you’re not ready to commit.
  • When you want to temporarily set aside changes to work on something else.

Basic Commands

Stashing Your Work

To stash your changes, use the following command:

git stash

This command stashes:

  • Tracked files that have been modified.
  • Staged changes.

Viewing Stashes

To see a list of all stashes, use:

git stash list

This command will display a list of stashes with an identifier, like stash@{0}, stash@{1}, etc.

Applying Stashes

To reapply the most recent stash, use:

git stash apply

If you want to apply a specific stash, use:

git stash apply stash@{n}

Where n is the stash number.

Removing Stashes

After applying a stash, it remains in the stash list. To remove it, use:

git stash drop stash@{n}

To clear all stashes, use:

git stash clear

Popping Stashes

To apply and remove the most recent stash in one go, use:

git stash pop

Advanced Usage

Stashing Untracked and Ignored Files

By default, git stash does not include untracked files. To stash untracked files as well, use:

git stash -u

To include ignored files as well, use:

git stash -a

Creating a Named Stash

You can give a name to your stash for easier identification:

git stash push -m "your message"

Stashing Specific Files

To stash changes from specific files or directories, use:

git stash push -p

This will prompt you interactively to select the changes you want to stash.

Applying Stashes with Conflicts

When you apply a stash, it may result in conflicts. Resolve them as you would with any merge conflict:

git status

Then, edit the conflicting files and stage the resolved changes:

git add <resolved_files>

Finally, commit the resolved changes:

git commit -m "Resolved stash conflicts"

Examples and Best Practices

Example 1: Basic Workflow

  1. Make changes to your working directory.
  2. Realize you need to switch branches.
  3. Stash your changes: git stash
  4. Switch branches: git checkout another-branch
  5. Work on the other branch, then switch back: git checkout original-branch
  6. Apply the stashed changes: git stash pop

Example 2: Stashing Untracked Files

  1. Make changes including new files.
  2. Stash changes, including untracked files: git stash -u

Best Practices

  • Naming Stashes: Use descriptive messages when stashing with -m to make it easier to identify the stash later.
  • Regular Cleanup: Regularly clean up your stash list to avoid clutter using git stash drop or git stash clear.
  • Selective Stashing: Use interactive stashing (git stash push -p) for fine-grained control over what you stash.

Git stash is a powerful tool that can enhance your workflow by allowing you to temporarily set aside changes without committing them. Understanding how to effectively use git stash can save you time and help maintain a clean and organized codebase.