Git Stash: Temporarily Saving Changes

When you need to quickly switch branches, but you have uncommitted changes in your working directory. Committing these unfinished changes might not be desirable, and discarding them would result in a loss of your work. This is where Git’s stash command comes into play.

Git Stash: Temporarily Saving Changes

Overview

When working with Git, there are times when you need to quickly switch branches, but you have uncommitted changes in your working directory. Committing these unfinished changes might not be desirable, and discarding them would result in a loss of your work. This is where Git’s stash command comes into play.

The git stash command allows you to temporarily "stash" your changes, saving them for later use without committing them to the current branch. You can then safely switch branches or perform other Git operations, and later "apply" or "pop" the stashed changes back when you're ready.

In this post, we'll dive deep into:

  1. What is git stash and when to use it?
  2. How to stash your changes.
  3. Applying stashed changes.
  4. Managing multiple stashes.
  5. Advanced git stash options (e.g., stashing specific files, stashing untracked files).
  6. Best practices for using git stash.

1. What is git stash and When to Use It?

The git stash command allows you to save your changes in the working directory (both tracked and untracked files) to a stack (or list) of changes called "stashes." This means you can temporarily store work that is not ready to be committed, allowing you to switch branches, pull changes, or perform other Git operations without losing your uncommitted work.

When to use git stash:

  • Switching branches: You need to switch to another branch to work on something else, but you don't want to commit your incomplete work.
  • Pulling changes: You want to pull the latest changes from a remote branch, but you have local changes that may conflict with the incoming updates.
  • Quick fixes: You need to make a quick fix or change on another branch but don't want to lose your progress on the current task.
  • Temporary hold: You're experimenting with some changes but don't want to commit them until you're sure they work.

git stash is incredibly useful for preserving your work in progress while keeping your branch history clean.

2. How to Stash Your Changes

Stashing Your Work

To stash your current changes, simply run the following command in your Git repository:

git stash

This command will:

  • Save all your modifications (both staged and unstaged changes) to the stash stack.
  • Reset your working directory to match the latest commit (as if you had not made any changes).
  • Leave your local repository in a clean state, allowing you to switch branches or pull changes without any conflicts.

Example:

Let's say you've made changes to file1.txt and file2.txt, but you're not ready to commit them. Run the following command to stash them:

git stash

After running the command, your working directory will be clean, and Git will output something like this:

Saved working directory and index state WIP on feature-branch: 34ab7c9 Initial feature commit

Git has successfully stashed your changes and restored your working directory to the previous state.

Viewing Stashed Changes

If you want to see a list of all the stashes you’ve saved, use:

git stash list

The output will look something like this:

stash@{0}: WIP on feature-branch: 34ab7c9 Initial feature commit
stash@{1}: WIP on main: 56bde7f Fix typo in README

Each stash is labeled with an identifier (stash@{n}) and a short description, allowing you to keep track of what was saved.

3. Applying Stashed Changes

When you're ready to bring your stashed changes back into your working directory, you can apply them using one of the following methods.

Method 1: Applying a Stash Without Removing It from the Stash List

git stash apply

This will apply the most recent stash (i.e., stash@{0}) to your working directory without removing it from the stash list. The stashed changes will be reapplied, but the stash itself will remain in the list, allowing you to reapply it later if necessary.

Method 2: Applying and Removing a Stash from the List (Using pop)

If you want to apply the stash and remove it from the stash list, use:

git stash pop

This command applies the latest stash (i.e., stash@{0}) and removes it from the stash stack. After running this command, the applied stash is no longer available in your stash list.

Applying a Specific Stash

If you have multiple stashes and want to apply a specific one, reference its identifier:

git stash apply stash@{1}

This applies the stash with identifier stash@{1} without removing it from the stash list.

4. Managing Multiple Stashes

Listing All Stashes

As mentioned earlier, you can view all the stashes using:

git stash list

This will show all the saved stashes in the order they were created.

Deleting a Specific Stash

If you no longer need a specific stash, you can delete it using the drop command:

git stash drop stash@{1}

This will remove the specified stash from the list without affecting your working directory.

Deleting All Stashes

To remove all stashes from the stash list, use:

git stash clear

This will delete all stashes in the stash stack, clearing your stash history.

5. Advanced git stash Options

Stashing Untracked Files

By default, git stash only stashes changes to tracked files (i.e., files that are already part of the repository). However, if you want to stash both tracked and untracked files, you can use the --include-untracked (or -u) option:

git stash --include-untracked

This will save changes to both tracked and untracked files, allowing you to stash everything in your working directory.

Stashing Specific Files

If you only want to stash changes from specific files, you can specify them like this:

git stash push path/to/file1.txt path/to/file2.txt

This will stash changes only for file1.txt and file2.txt, leaving other changes in your working directory untouched.

Creating a Stash with a Custom Message

By default, Git provides a generic "WIP" message when creating stashes. However, you can provide your own custom message to describe the stash more clearly:

git stash push -m "Work on new API endpoint"

This custom message will appear in the stash list, making it easier to identify specific stashes.

6. Best Practices for Using git stash

1. Stash Before Switching Branches

If you’re frequently switching branches while working on a task, make it a habit to stash your changes before switching. This ensures that you don't lose work or accidentally commit incomplete changes.

git stash
git checkout main

2. Clean Up Stashes Regularly

Keep your stash list clean and manageable by regularly clearing out old or unused stashes. You can do this manually by dropping individual stashes:

git stash drop stash@{0}

Or clear all stashes once you're sure you no longer need them:

git stash clear

3. Name Your Stashes

When creating a stash, always provide a meaningful message. This will help you identify stashes more easily, especially when working on multiple features or tasks simultaneously.

git stash push -m "WIP: Add user login feature"

4. Use Stash for Experimentation

The git stash command is great for quickly experimenting with changes. You can stash your main work, experiment with new ideas, and if the experiment doesn’t work out, simply discard the changes and apply the stash again.

Conclusion

The git stash command is a powerful tool that helps you manage your workflow by temporarily saving uncommitted changes. It’s particularly useful when switching between branches or performing other Git operations that require a clean working directory. In this post, we explored how to use git stash, how to apply stashed changes, and advanced options like stashing specific files or untracked changes.

By incorporating git stash into your daily Git workflow, you can increase productivity, keep your branch history clean, and ensure that your work is never lost.

Read next

Handling and Resolving Conflicts During Rebasing

Rebasing is a common technique to keep a clean and linear history. However, one of the challenges developers face while performing a rebase is dealing with merge conflicts. During a rebase, conflicts may arise if the changes in your branch overlap or contradict the changes in the branch

🪐 Publishing the HHGTTG Bash Module on GitHub

I packaged my small Bash module, cleaned the code, added structure, and finally published it on GitHub. In this post, I walk through preparing a project for public release, choosing a layout, documenting it properly, and getting it ready for future automation.