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
1. Stashing Changes
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"
2. Listing Stashes
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).
3. Applying Stashes
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}).
4. Popping Stashes
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}
5. Dropping Stashes
Dropping Stashes:
To delete a specific stash without applying it, use:
git stash drop stash@{index}
To clear all stashes, use:
git stash clear
6. Stashing Parts of Files
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
Step 1
You're working on a feature branch and have some changes you're not ready to commit
git stash
Step 2
You switch to another branch to address a different task
git checkout another-branch
Step 3
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.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
