Git & GitHub / Git Branching and Merging
Using Git Stash for Temporary Work
In this tutorial, you'll learn how to use the Git stash command to save changes that you're not ready to commit. This is a useful tool for managing your work in progress.
Section overview
5 resourcesCovers creating branches, merging changes, and resolving conflicts in Git.
Using Git Stash for Temporary Work
1. Introduction
This tutorial aims to provide an in-depth understanding of using Git Stash for temporarily storing changes that you're not ready to commit.
By the end of this tutorial, you will learn:
- What Git Stash is and why it is used.
- How to use Git Stash to save and retrieve uncommitted changes.
- How to apply stashed changes to your current working directory.
Prerequisites:
- A basic understanding of Git and its command-line interface.
- Git installed on your computer.
2. Step-by-Step Guide
git stash is a command that takes your modified tracked files, stages changes, and saves them on a stack of unfinished changes that you can reapply at any time. This is particularly useful when you want to switch branches, but you're working on something that you're not ready to commit yet.
Using git stash
To stash your changes, use the following command:
git stash
This will take your uncommitted changes (both staged and unstaged), save them away for later use, and then revert them from your working directory.
Listing stashed changes
You can list your stashed changes using:
git stash list
This will display all the stashed changes.
Applying stashed changes
To apply the most recently stashed changes, use:
git stash apply
If you want to apply a specific stash, use:
git stash apply stash@{n}
Replace n with the stash number.
3. Code Examples
Let's say you're working on your project and have made some changes, but you want to switch to another branch. Here's how you can use git stash.
Example 1
# You're working on your project and made some changes
echo "temp work" > temp.txt
git add temp.txt
# Save changes with git stash
git stash save "Work in progress for new feature"
In this example, a new file temp.txt is created and added to Git. These changes are then stashed with a message "Work in progress for new feature".
Example 2
# List all stashed changes
git stash list
This command will list all the stashed changes. The output might look something like: stash@{0}: On master: Work in progress for new feature
Example 3
# Apply the most recent stash
git stash apply
# Or apply a specific stash
git stash apply stash@{0}
These commands will apply the stashed changes to your current working directory. The first one applies the most recent stash, while the second one applies a specific stash.
4. Summary
In this tutorial, we've covered:
- Using
git stashto save uncommitted changes. - Listing stashed changes with
git stash list. - Applying stashed changes using
git stash apply.
Next, you can learn about more specific git stash commands, like dropping a stash, creating a branch from a stash, etc.
5. Practice Exercises
Exercise 1: Make some changes in your git repository, stash them, and list your stashes.
Exercise 2: Apply the most recent stash you've created, then verify it has been applied correctly.
Exercise 3: Create multiple stashes, apply a specific stash, and drop a stash.
Solutions:
- Use
git stash save "Your message"to stash changes andgit stash listto list stashes. - Use
git stash applyto apply the most recent stash. You can verify it by checking your working directory. - Create multiple stashes using the command in exercise 1. Apply a specific stash with
git stash apply stash@{n}. Drop a stash withgit stash drop stash@{n}.
Tips for further practice:
Try using git stash pop, git stash branch, and git stash clear commands and understand their use cases.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article