Back to Git & Dev Tools
2026-05-056 min read

stash changes (Git & Dev Tools)

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

Why This Matters

In the realm of software development, it's common for developers to switch between tasks or collaborate with other team members while working on a project. Git Stash is an invaluable tool that helps manage these situations efficiently by allowing developers to store their local modifications temporarily without committing them. This feature is crucial for maintaining a clean and organized workflow, especially in complex projects where multiple tasks need attention simultaneously.

By using Git Stash, developers can ensure that they don't lose any progress when switching between tasks or collaborating with others. It helps prevent conflicts and keeps the project's history clean, making it easier to track changes and collaborate effectively.

Prerequisites

Before diving into the core concept of Git Stash, it's essential to have a basic understanding of Git and its fundamental concepts:

  1. Familiarity with Git commands such as git init, git add, git commit, and git checkout
  2. Understanding the Git branching model, including creating, switching, and merging branches
  3. Knowledge of how to view your changes using git status and git diff
  4. Familiarity with common Git workflows like feature branching and pull request-based workflows
  5. Basic understanding of Git stashing, such as the difference between stash and commit
  6. Understanding the concept of untracked files in Git
  7. Knowledge of how to stage files using git add

Core Concept

Stashing Changes

Stashing changes in Git allows developers to save their local modifications temporarily and continue working on something else without losing their progress. To stash your local modifications, use the following command:

git stash save "stash message"

Replace "stash message" with a descriptive message that helps you identify the stashed changes later. Git will store your changes in a stack of stashes, allowing you to apply them back to your working directory when needed.

Stash List and Apply

To view your stashed changes, use:

git stash list

This command displays the list of stashed changes along with their unique stash IDs. To apply a specific stash, use:

git stash apply "stash ID"

Replace "stash ID" with the unique identifier of the stash you want to apply. If you don't specify an ID, Git will apply the latest stash by default.

Stash Drop and Pop

After applying a stash, if you no longer need it, you can remove it from the stack using:

git stash drop "stash ID"

Alternatively, you can apply and drop a stash in one step with the git stash pop command:

git stash pop "stash ID"

Stash Apply Stash at Index (Stash @)

In some cases, you may want to apply a stash while keeping your current changes. To do this, use the git stash apply stash@{n} command, where n is the number of stashes from the latest:

git stash apply stash@{1}

This command applies the second-to-last stash, leaving your current changes intact.

Stashing Untracked Files

By default, untracked files are not included in the stash. If you want to include them, use git stash -u. This command will stage and stash all untracked files along with modified and deleted files.

Stashing Only Specific Files

If you only want to stash specific files instead of the entire working directory, first stage those files using git add:

git add file1.txt file2.txt

Then, save the stash as usual:

git stash save "Stashing selected files"

Worked Example

Let's walk through a practical example of using Git Stash:

  1. Initialize a new repository and create a feature branch:
git init
git checkout -b feature-branch
  1. Make some changes to a file, add them, and commit:
touch new_file.txt
echo "Hello, World!" > new_file.txt
git add .
git commit -m "Add new file with initial content"
  1. Switch to the main branch and make some changes there:
git checkout main
echo "This is the main branch." >> README.md
git add .
git commit -m "Update README in main branch"
  1. Now, you need to work on another task in the feature branch temporarily. To do this, stash your changes:
git stash save "Stashing new file and content"
  1. Switch back to the feature branch and continue working:
git checkout feature-branch
  1. After completing the task, you can apply the stashed changes:
git stash apply
  1. If needed, you can view your stashed changes with git stash list. To remove the stash, use git stash drop "Stashing new file and content".

Common Mistakes

1. Not Providing a Stash Message

Always provide a descriptive message when stashing changes to help identify them later. A good practice is to include the name of the feature branch or task in the message:

git stash save "Stashing feature-branch changes"

2. Applying the Wrong Stash

Be careful when applying stashes, as Git will apply the latest one by default if you don't specify an ID. Always double-check the stash ID before applying to avoid overwriting your current work:

git stash apply "stash ID"

3. Not Committing Changes Before Stashing

If you have uncommitted changes when stashing, Git will include them in the stash as well. Ensure that all your changes are committed before stashing to avoid confusion:

git commit -m "Commit changes before stashing"
git stash save "Stashing after committing"

4. Not Including Untracked Files in the Stash

If you have untracked files that need to be included in the stash, use git stash -u. This command will stage and stash all untracked files along with modified and deleted files:

git stash -u save "Stashing untracked files"

5. Stashing Changes Without Staging Them First

If you stash changes without staging them first, Git will only store the changes that are already tracked in the repository. To include untracked files or changes not yet added to the index, stage them using git add before stashing:

touch new_file.txt
echo "Hello, World!" > new_file.txt
git add .
git stash save "Stashing new file and content"

Practice Questions

  1. You're working on a feature branch and need to switch to another task in the main branch temporarily. How would you stash your current changes?
git stash save "Stashing feature-branch changes"
  1. You have multiple stashes and want to apply the second-to-last one while keeping your current changes intact. What command should you use?
git stash apply stash@{1}
  1. After applying a stash, if you no longer need it, how would you remove it from the stack?
git stash drop "stash ID"
  1. If you have untracked files that need to be included in the stash, what command should you use?
git stash -u save "Stashing untracked files"
  1. What would happen if you stash changes without staging them first?

Git will only store the changes that are already tracked in the repository. To include untracked files or changes not yet added to the index, stage them using git add before stashing.

FAQ

1. What happens to my untracked files when I stash changes?

By default, untracked files are not included in the stash. If you want to include them, use git stash -u.

2. Can I apply a stash from another repository?

No, Git stashes are specific to each repository and cannot be applied across repositories.

3. How can I view the differences between my working directory and the last stashed changes?

To view the differences, use git diff stash@{0} after applying a stash but before committing the changes.

4. Is it possible to stash only specific files instead of the entire working directory?

Yes, you can stash only specific files by first staging them using git add and then saving the stash as usual:

git add file1.txt file2.txt
git stash save "Stashing selected files"

5. What happens if I try to stash changes on a detached HEAD?

When you're on a detached HEAD, Git treats the current commit as a standalone commit that is not associated with any branch. In this case, you cannot stash changes because there is no working directory to save the modifications. To resolve this issue, switch back to a branch using git checkout before trying to stash your changes.

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