Master Git in 30 Days

Day 13: Stashing Changes in Working Directory and Index

Have you ever encountered this situation: you’re halfway through developing a system, and suddenly your boss or client gives you an “urgent order,” asking you to fix a bug in an existing system urgently or add a feature? The program in front of you is about to be completed, and the boss’s “urgent matter” can’t be delayed. An incomplete software development state plus emergency adjustment requirements is simply a big test of software quality. If you have this trouble, Git can help you complete the task beautifully.

Understanding the git stash Command

We know that when using Git version control, there is a distinction between the “working directory” and the “index.” In the working directory, there will be files you have changed halfway and not finished (not yet added to the index), and there will also be new files that have not been added (not yet added to the index). The data placed in the index is the content you plan to create a version (create a commit object) through git commit.

When you’re halfway through feature development and are urgently interrupted, you will definitely be in a hurry, especially not knowing what to do with those half-rewritten programs at hand. In Git, there is a git stash command that can automatically help you create a “special version” (also a commit object) for those half-written files. We call these versions stash versions, or you can directly call them “staging versions.”

Creating Stash Versions

The files we’ve changed halfway may have the following states:

  • New files (untracked files)
  • New files (files already added to the index) (tracked/staged files)
  • Modified files (files not yet added to the index) (tracked/unstaged files)
  • Modified files (files already added to the index) (tracked/staged files)
  • Deleted files (files not yet added to the index) (tracked/unstaged files)
  • Deleted files (files already added to the index) (tracked/staged files)

If you want to create a “staging version” for these half-developed files, you have two choices:

  • git stash will create a staging version for all tracked files
  • git stash -u will include all tracked or untracked files, all will be created as a staging version

Note: git stash can also be written as git stash save. The result of the two commands is the same, it’s just that the save parameter can be omitted.

Example Workflow

Here’s a simple practical example:

# You're working on features
echo "new feature" > feature.txt
git add feature.txt

# Suddenly need to fix a bug
# Stash your work-in-progress
git stash

# Now working directory is clean
# Fix the bug on another branch
git checkout main
# ... make fixes ...
git commit -m "Fix urgent bug"

# Return to your feature work
git checkout feature-branch
git stash pop  # Restore your work

Restoring Stashed Changes

After stashing your changes, you can switch to another branch or work on the urgent task. When you’re ready to continue your original work, you can restore your stashed changes using:

  • git stash pop - Applies the most recent stash and removes it from the stash list
  • git stash apply - Applies the most recent stash but keeps it in the stash list

Listing Stashed Changes

To see what you have stashed, use:

git stash list

This will show you all stashed changes with their stash references (stash@{0}, stash@{1}, etc.).

Applying Specific Stashes

If you have multiple stashes, you can apply a specific one by referencing its number:

git stash apply stash@{2}

Dropping Stashes

To remove a specific stash from the list:

git stash drop stash@{0}

To remove all stashes:

git stash clear

Stashing Untracked Files

By default, git stash only stashes tracked files. To also stash untracked files:

git stash -u

Or to stash everything including ignored files:

git stash -a

Creating Named Stashes

You can give your stash a descriptive message:

git stash save "Work in progress on feature X"

This makes it easier to identify stashes later.

Viewing Stash Contents

To see what’s in a specific stash without applying it:

git stash show stash@{0}

For a detailed diff:

git stash show -p stash@{0}

Today’s Summary

The git stash command is a very practical tool that allows you to temporarily set aside your current work without committing it. This is especially useful when you need to quickly switch contexts to work on something else. The stash mechanism ensures that your work-in-progress changes are safely stored and can be restored later.

Let me reorganize the Git commands and parameters learned today:

  • git stash
  • git stash save
  • git stash -u
  • git stash list
  • git stash pop
  • git stash apply
  • git stash drop
  • git stash clear
  • git stash show