Stashing Changes
Git stashing is a useful feature that allows you to temporarily save changes in your working directory that you're not ready to commit. This is particularly helpful if you're working on something but need to switch to another task or branch quickly. Here’s a detailed explanation of how it works:
What is Git Stashing?
When you "stash" changes, Git takes the current state of your working directory and index (staging area) and saves it on a stack of stashes. This allows you to clean your working directory and switch branches without losing your uncommitted changes. Later, you can apply those stashed changes back to your working directory.
Basic Git Stash Commands
-
Stashing Changes:
To stash your current changes, use:
git stash
This command saves your modified tracked files and the staged changes, and then reverts your working directory to the state of the last commit.
You can also include untracked files in the stash by using:
git stash -u
or
git stash --include-untracked
To stash changes with a message, use:
git stash save "message"
-
Listing Stashes:
To see a list of all stashed changes, use:
git stash list
This will show a list of stashes with their index and message (if provided).
-
Applying Stashes:
To apply the most recent stash, use:
git stash apply
This applies the changes from the stash to your working directory, but does not remove it from the stash list.
To apply a specific stash, use:
git stash apply stash@{index}
Replace
index
with the number of the stash you want to apply (e.g.,stash@{0}
). -
Popping Stashes:
If you want to apply the most recent stash and remove it from the stash list, use:
git stash pop
This applies the stash and then deletes it from the stash list.
To pop a specific stash, use:
git stash pop stash@{index}
-
Dropping Stashes:
To delete a specific stash without applying it, use:
git stash drop stash@{index}
To clear all stashes, use:
git stash clear
-
Stashing Parts of Files:
If you want to stash only some changes within a file, you can use:
git stash push -p
This will interactively allow you to select which changes to stash.
Workflow Example
-
You’re working on a feature branch and have some changes you’re not ready to commit:
git stash
-
You switch to another branch to address a different task:
git checkout another-branch
-
Once you’re done with the other task and switch back to your feature branch:
git checkout feature-branch git stash apply
or, if you prefer to apply and remove the stash in one step:
git stash pop
Using stashes effectively helps you manage your workflow, especially when you need to switch contexts quickly or manage multiple tasks simultaneously.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.