Back to Git & Dev Tools
2026-01-217 min read

git stash (Git & Dev Tools)

Learn git stash (Git & Dev Tools) step by step with clear examples and exercises.

Why This Matters

In this extensive lesson, we delve into the invaluable Git feature called git stash. This tool is an indispensable asset for developers who need to switch between branches or handle unexpected interruptions without losing their current work. We'll explore its core concept, worked examples, common mistakes, practice questions, and frequently asked questions in detail.

Why This Matters

As developers, we often find ourselves in situations where we have made changes to our working directory but need to attend to other tasks or switch branches. Git stash allows us to save our changes temporarily so that we can switch contexts without losing progress. It is particularly useful during code reviews, bug fixing, and handling interruptions like meetings or emergencies.

Prerequisites

Before diving into the core concept of git stash, it's essential to have a solid understanding of Git and its commands:

  1. Git Basics: Familiarize yourself with Git terminology such as commits, branches, and working directory.
  2. Git Commands: Understand fundamental Git commands like git init, git add, git commit, git checkout, git merge, and git status.
  3. Staging Area: Learn about the staging area (index) in Git where files are prepared for committing.
  4. Branching and Merging: Understand how to create, switch, and merge branches in Git.
  5. Git Configuration: Familiarize yourself with Git configuration settings like user name and email address.
  6. Understanding Git Workflows: Learn about common Git workflows such as Feature Branch Workflow, GitFlow, and Forking Workflow to better understand when and why you might need to use git stash.

Core Concept

In a nutshell, git stash saves your uncommitted changes so that you can switch branches or contexts without losing progress. When you have made changes to your working directory but not yet committed them, you can use the command git stash save. This command temporarily stores your changes and creates a stash entry in Git's history.

$ git stash save "My Changes"

To apply the saved changes back to your working directory, use the command git stash apply. If you have multiple stashes, you can specify which one to apply using the stash@{n} notation, where n is the number of the stash.

$ git stash apply "My Changes" # or git stash apply stash@{0}

If you want to both apply and remove the stash entry, use the command git stash drop.

$ git stash drop "My Changes" # or git stash drop stash@{0}

Stash Lifecycle

Git stashes are stored in a FIFO (First-In, First-Out) stack. When you create a new stash, it is added to the top of the stack. If your repository reaches the maximum number of stashes (usually 10), older stashes will be automatically dropped from the bottom of the stack to make room for newer ones.

Stash Patch Format

Each Git stash entry consists of a patch file that describes the changes made in the working directory. These patch files are stored locally and can be inspected using the git stash show -p command.

Worked Example

Let's walk through a practical example to better understand how git stash works:

  1. Create a new Git repository and navigate into it:
$ mkdir my-repo && cd my-repo
$ git init
  1. Create a file called file.txt with some content:
$ echo "Hello, World!" > file.txt
  1. Make changes to the file and stage them for commit:
$ echo "New Line" >> file.txt
$ git add file.txt
  1. Before committing the changes, create a stash:
$ git stash save "Unfinished Changes"
  1. Now let's switch to another branch and make some changes there:
$ git checkout -b new-branch
$ echo "New Branch Changes" > file.txt
$ git add file.txt
$ git commit -m "Changes on new-branch"
  1. To return to the original branch and apply the saved changes, use git stash apply:
$ git checkout master
$ git stash apply "Unfinished Changes"
  1. Verify that your changes have been applied:
$ cat file.txt
Hello, World!
New Line
  1. To inspect the patch for the stash, use git stash show -p "Unfinished Changes". This will display the changes made in the working directory before creating the stash.
  1. If you want to remove the stash entry after applying it, use git stash drop "Unfinished Changes".

Stashing Large Files

It's worth noting that Git stash does not handle large files efficiently. For large files, it's recommended to manually move them out of the working directory and then use git add to stage them again after applying the stash.

Common Mistakes

  1. Not specifying a stash message: When creating a stash, always provide a descriptive message to help identify the stash later.
  2. Applying the wrong stash: Be mindful of which stash you're applying when working with multiple stashes. Use the stash@{n} notation to specify the correct one.
  3. Losing track of stashes: Git stash entries are temporary and will be automatically dropped after a certain number of stashes (usually 10). To avoid losing your work, consider using git stash list to view your stashes or use git stash show -p to inspect the changes in a stash.
  4. Not committing before stashing: If you have committed some changes but not others, only the uncommitted changes will be stashed. Make sure to commit any necessary changes before creating a stash.
  5. Stashing changes from multiple files at once: When stashing changes from multiple files, it's recommended to stage them using git add -u before creating the stash. This command stages all modified and deleted files in the working directory and its subdirectories.
  6. Not cleaning up stashes: If you no longer need a stash and want to save space in your Git repository, consider using git stash drop or git stash clear to remove unnecessary stashes.
  7. Ignoring conflicts when applying a stash: When applying a stash with conflicts, Git will pause and allow you to resolve the conflicts manually. After resolving the conflicts, use git add to stage the resolved file and then continue applying the stash using git stash apply --continue.

Practice Questions

  1. What command is used to save your uncommitted changes as a stash?
  2. How can you apply a specific stash entry back to your working directory?
  3. How do you remove a stash entry after applying it?
  4. What happens if you have both committed and uncommitted changes in your working directory before creating a stash?
  5. How can you view the list of saved stashes in your Git repository?
  6. What is the maximum number of stashes that can be stored in a Git repository by default?
  7. How can you inspect the patch for a specific stash?
  8. What happens when you reach the maximum number of stashes and try to create a new one?
  9. Can you stash changes from multiple files at once? If so, how?
  10. How do you clean up unnecessary stashes in your Git repository?
  11. What are some common reasons for using git stash?
  12. How can you handle conflicts when applying a stash?
  13. What happens to your stashed changes if you create a new Git repository?
  14. Can you apply multiple stashes at once? If so, how?
  15. How do you handle large files when using git stash?

FAQ

Q: Can I stash changes from multiple files at once?

A: Yes, you can stash changes from multiple files by using git add -u before creating the stash. This command stages all modified and deleted files in the working directory and its subdirectories.

Q: What happens to my stashed changes if I create a new Git repository?

A: Stashed changes are local to your Git repository and will not be included when you create a new one. To move your stashes, you can use git stash save --include-patch which saves the stash as a patch file that you can apply later in another repository.

Q: Can I apply multiple stashes at once?

A: Yes, you can apply multiple stashes by using git stash apply followed by git stash apply , and so on. Be aware that applying multiple stashes may lead to conflicts if the changes made in different stashes overlap.

Q: How do I handle conflicts when applying a stash?

A: When applying a stash with conflicts, Git will pause and allow you to resolve the conflicts manually. After resolving the conflicts, use git add to stage the resolved file and then continue applying the stash using git stash apply --continue.

Q: Can I stash changes from a specific branch only?

A: No, Git stash works on the entire working directory and not on specific branches. However, you can switch to the desired branch after creating the stash and then apply it there.

Q: How do I handle large files when using git stash?

A: For large files, it's recommended to manually move them out of the working directory and then use git add to stage them again after applying the stash. Alternatively, you can split the large file into smaller parts before stashing or consider using a tool like LFS (Large File Storage) for managing large files in Git repositories.

Q: Can I use git stash with GitHub?

A: Yes, you can use git stash with GitHub by pushing and pulling your repository as usual. However, keep in mind that GitHub does not store stashes directly; they are stored locally on your machine. If you need to share or collaborate on stashes, consider using a tool like GitHub Actions or GitLab CI/CD to automate the process of saving and applying stashes across multiple machines.

git stash (Git & Dev Tools) | Git & Dev Tools | XQA Learn