Git is an essential tool in every developer’s belt. It makes project collaboration within a team very easy, and that’s just one of many benefits. One handy feature of Git is git stash which saves your changes without committing them and lets you work on other branches.
If you ever work with several branches at the same time, then you need to learn how to shelve your changes by stashing them. Read on as we explain what git stash is and how to use it to ease your developer workflow.
What Is Git Stash?
Git stash shelves your uncommitted changes, allowing you to switch branches and work on something else. Imagine you’re working on some changes and have written a lot of code that you aren’t yet ready to commit. Then you get assigned to another task and have to switch branches immediately.
Stashing allows you to keep the changes you’ve made, and work on another branch at the same time. It’s a lighter action than committing: you keep your local changes without updating the repository.
When you switch branches, you can continue working without losing your uncommitted changes. Once you’re done, switch back to the original branch and retrieve your previous changes to resume working. Git will automatically apply your earlier changes to the clean working directory.
Not only does Git stash save you time, but it also allows you to adopt a good developer workflow. You might be wondering why you shouldn’t just commit the changes before switching branches. While this is possible, it’s less flexible and can complicate your repository’s history. Your incomplete changes are not treated as work in progress but a complete checkpoint.
How to Use Git Stash
Using Git stash is relatively straightforward. First, you save the changes on your current branch—call it branch-a—using git stash. Then you can switch to another branch—branch-b—and carry out some work on it. Once you’re ready to resume working on branch-a, switch back and retrieve your stashed changes.
Save Changes With Git Stash
The first step in a typical workflow is stashing the changes in your working directory.
To preserve your local changes, whether they are staged or unstaged, save any files first, then run the following command:
You should get a confirmation message saying that git has saved the working directory. You are now free to switch branches and continue working without losing your changes:
git checkout branch-b
Retrieve Saved Changes
To reapply your stashed changes, you will need to checkout the original branch and then pop the stash, here’s what you need to do:
git checkout branch-agit stash pop
Once you execute these commands, Git will apply the saved changes to your working copy.
If you want to apply your changes without removing them from the stash, you can use the below code instead:
git stash apply
Stash Ignored and Untracked Files
When you run git stash, it will automatically shelve all staged and unstaged changes. But files that are ignored or not in the repository at all are not stashed. If you want to stash those changes, you will have to modify the git stash command.
To stash untracked files:
git stash -u
To stash untracked and ignored files use:
git stash -a
Work Smart With Git Stash
Switching branches can be overwhelming if you’ve recently begun working with a git workflow. Git stash eases your workflow by saving local changes temporarily.
Git is a vital asset for developers, and being proficient with advanced Git functionality is crucial to success.