Stashing and Cleaning (Git & Dev Tools)
Learn Stashing and Cleaning (Git & Dev Tools) step by step with clear examples and exercises.
Title: Git Stashing and Cleaning: A full guide for Developers
Why This Matters
As a developer, you often find yourself working on multiple tasks simultaneously, each requiring different changes to your codebase. However, sometimes you might need to switch to another task urgently without committing the current changes, which can be frustrating and time-consuming to manage. Git's stashing feature comes to the rescue by helping you save your uncommitted changes temporarily, allowing you to work on something else without losing your progress. In this lesson, we will explore how to use Git stashing and cleaning effectively to streamline your development process.
Prerequisites
To follow along with this lesson, you should have a basic understanding of:
- Git fundamentals, such as creating repositories, committing changes, and branching
- The command line interface (CLI) or a Git GUI like GitKraken or SourceTree
- Familiarity with common Git commands like
git add,git commit,git checkout, andgit status - Understanding the concept of a Git branch and how to create, switch, and delete branches
Core Concept
What is Git Stashing?
Git stashing temporarily saves your local uncommitted changes, allowing you to switch to another task without losing your current work. When you stash your changes, Git creates a new commit that stores the changes in a stack. You can then apply the saved changes back to your working directory when needed.
How to Stash Changes
To stash your changes, run the following command in your terminal:
git stash save "stash message"
Replace "stash message" with a short description of your changes. This command creates a new stash entry and saves your uncommitted changes. If you don't provide a message, Git will automatically generate one based on the current branch name and the number of stashes.
Listing Stashed Changes
To view your stashed changes, run:
git stash list
This command displays a list of all your stashed changes along with their unique identifiers (stash@{...}).
Applying a Stashed Change
To apply a stashed change to your working directory, use the following command:
git stash apply "stash message"
Replace "stash message" with the message you provided when creating the stash. If you don't specify a message, Git will apply the most recent stash.
Deleting a Stashed Change
If you no longer need a stashed change and want to delete it, run:
git stash drop "stash message"
Again, replace "stash message" with the message you provided when creating the stash. This command removes the specified stash from the stack.
Clearing All Stashed Changes
To remove all stashed changes and discard them permanently, run:
git stash clear
This command deletes all stashes in the stack. Be cautious when using this command, as it will permanently delete your saved changes.
Stashing Changes Interactively
In some cases, you might want to customize which changes are stashed by interactively selecting specific files or hunks of code. To do this, run:
git stash -i "stash message"
This command opens an editor with a list of your uncommitted changes. You can then select the changes you want to stash and save the file to create the stash.
Stashing Changes on a Specific Branch
By default, Git stashes changes from the current branch. However, if you're working on a specific branch and need to stash only changes related to that branch, you can use the --index option:
git stash save --index "stash message"
This command will only stash changes that have been added to the index (i.e., staged changes) for the current branch.
Worked Example
In this example, we will demonstrate how to use Git stashing in practice:
- Create a new Git repository and navigate to it:
mkdir my-repo && cd my-repo
git init
touch file1.txt file2.txt
- Make some changes to both files:
echo "Hello World" > file1.txt
echo "This is a test" >> file2.txt
- Stage and commit the changes for the
mainbranch:
git add .
git commit -m "Initial commit on main"
- Now, let's say you want to create a new feature branch (
feature/new-feature) and make some changes there without committing them yet. Switch to the new branch:
git checkout -b feature/new-feature
- Make some changes on the
feature/new-featurebranch:
echo "New changes" > file1.txt
- Now, let's say you need to switch back to the
mainbranch and want to save your current changes temporarily. Stash them using:
git stash save "Save feature/new-feature changes" --index
- Switch back to the
mainbranch:
git checkout main
- Apply the stashed changes from the
feature/new-featurebranch:
git stash apply "Save feature/new-feature changes"
- Verify that your original changes have been applied:
cat file1.txt
- Now, let's delete the stashed change since we no longer need it:
git stash drop "Save feature/new-feature changes"
Common Mistakes
1. Forgetting to Stash Changes Before Switching Branches or Tasks
If you forget to stash your changes before switching branches or tasks, you might lose your work if the new task requires changes that conflict with your uncommitted changes. To avoid this, always remember to stash your changes before switching contexts.
2. Not Providing a Stash Message
Although not mandatory, providing a descriptive message for your stash can help you easily identify and manage your saved changes later on.
3. Confusing Git Stash with Git Add -p
Git add -p is used to selectively stage hunks of code for committing, while Git stash saves your uncommitted changes temporarily. Be careful not to confuse these two commands and use them appropriately based on your needs.
4. Stashing Changes from Multiple Branches Simultaneously
When you stash changes, they are stored in the current branch's stash stack. If you switch branches without applying or deleting the stashed changes, you might end up with multiple stashes for the same uncommitted changes across different branches. To avoid this, always apply or delete stashed changes before switching branches.
5. Not Understanding the Difference Between Git Stash and Git Stash Apply
Git stash saves your changes as a new commit, while Git stash apply applies the changes from a specific stash to your working directory. Be sure to use these commands appropriately based on your needs.
Practice Questions
- What command do you use to save your current uncommitted changes as a stash?
- How can you view all your stashed changes?
- To apply a specific stashed change, what command do you use and what information should you provide?
- If you have multiple stashes and want to delete the most recent one, which command do you use?
- What happens when you run
git stash clear? - How can you stash changes from a specific branch?
- What is the difference between Git stash and Git stash apply?
- What happens if you forget to stash your changes before switching branches or tasks?
- Can you stash untracked files along with tracked files when stashing changes?
- How can you recover a lost stashed change using Git's reflog?
FAQ
Q: Can I stash changes from a specific file or hunk of code?
A: Yes, you can stash changes interactively by using the -i flag, which opens an editor with a list of your uncommitted changes. You can then select the changes you want to stash and save the file to create the stash.
Q: What happens if I have untracked files when I try to stash my changes?
A: If you have untracked files in your working directory, Git will automatically include them in the stash. However, if you want to exclude specific files from the stash, you can use the --include-untracked or --exclude-untracked options when creating the stash.
Q: Can I stash changes from a different branch?
A: Yes, you can stash changes from any branch in your repository. To do this, switch to the branch containing the changes you want to stash and then run the git stash save command as usual.
Q: What if I accidentally lose my stashed changes?
A: If you lose your stashed changes by accident (for example, by running git stash drop without specifying a message), you can recover them using Git's reflog. The reflog keeps track of all your Git operations, including stashes. To view the reflog for stashes, run:
git log --oneline --decorate -g
You can then find the hash of the lost stash and recover it using:
git stash apply <stash-hash>