GIT Tutorials

HOME

Git is a Distributed Version Control System (VCS) which is originally developed in 2005 by Linus Torvalds (Creator of Linux) and is open source, i.e. freely available to use. It is the most popular and most used version control tool right now. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. 

Chapter 1 How to install Git on Windows 10
Chapter 2 How to create a new Git Repository – git init Command
Chapter 3 How to clone a Git Repository – git clone – NEW
Chapter 4 How to stage changes in Git – git add Command
Chapter 5 How to unstage the changes in Git – git rm command
Chapter 6 How to commit changes in GIT – git commit command
Chapter 7 How to track commits in Git – git log command
Chapter 8 How to commit an empty folder in GIT – gitkeep
Chapter 9 How to ignore files in GIT – gitignore
Chapter 10 How to create a branch in GIT
Chapter 11 How to stash changes in GIT – git stash command
Chapter 12 How to push new local GIT Repository to GitLab
Chapter 13 How to change a remote repository’s URL using git?
Chapter 14 Git Cheat Sheet – NEW

How to stash changes in GIT – git stash command

HOME

The previous tutorial has explained about committing the changes in GIT. This tutorial explains about stashing the changes in GIT.

What is git stash?

git stash temporarily stashes or shelves the changes made in the working copy so that we can work on something else and later apply these changes.

Imagine a situation where you are working on updating a feature, but suddenly there is a production bug in the existing functionality of the feature that needs to be fixed immediately. So, we need to switch to a bug fix and leave working on the update work. As the update work is not completed, we can stash these changes and work on the bug fix. Later, we can re-apply our stashed changes back to the code.

The git stash command can stash the uncommitted changes (both staged and unstaged), save them away for later use, and then revert them from your working copy. 

How to reapply stashed changes?

To re-apply the stashed changes, use this command

git stash pop

This command removes the changes from your stash and reapplies them to your working copy.

Alternatively, you can reapply the changes to your working copy and keep them in your stash with : 

git stash apply

Stashing untracked or ignored files

If a new file is created and not staged, then git stash is not going to stash that file.

Adding the -u option (or –include-untracked) tells git stash to also stash your untracked files:

git stash -u

Congratulation!! We have learned about stash in GIT.