What is Git Stash and its use cases

Reading Time: 4 minutes

Sometimes you want to switch the branches, but you are working on an incomplete part of your current project. You don’t want to make a commit of half-done work. Git stash allows you to do so. The git stash command enables you to switch branches without committing the current branch.

The below figure demonstrates the properties and role of stashing concerning repository and working directory.

Generally, the stash’s meaning is “store something safely in a hidden place.” The sense in git is also the same for stash; Git temporarily saves your data safely without committing.

Stashing takes the messy state of your working directory, and temporarily save it for further use. Many options are available with git stash. Some useful options are given below:

Stashing Work

Let’s understand it with a real-time scenario. I have made changes to my project final-capstone in two files from two distinct branches. I am in a messy state, and I have not entirely edited any file yet. So I want to save it temporarily for future use. We can stash it to save as its current status. To stash, let’s have a look at the repository’s current status. To check the current status of the repository, run the git status command.

Syntax:

$ git status 

From the above output, you can see the status that there are two untracked file Jenkinsfile and sts.yml available in the repository. To save it temporarily, we can use the stash command.

$ git stash

We can check the status of the repository.

My work is just stashed in its current position. At this point, you can switch between branches and work on them.

Git Stash Save (Saving Stashes with the message):

To stash a change with a message, run the below command:

$ git stash save "<Stashing Message>" 

The above stash will be saved with a message

Git Stash List (Check the Stored Stashes)

To check the stored stashes, run the below command:

$ git stash list  

If we have more than one stash, then It will display all the stashes respectively with different stash id. It will show all the stashes with indexing as stash@{0}: stash@{1}: and so on.

Git Stash Apply

You can re-apply the changes that you just stashed by using the stash command. To apply the commit, use the stash command, followed by the apply option.

$ git stash apply

The above output restores the last stash. Now, if you will check the status of the repository, it will show the changes that are made on the file. Consider the below output:

$ git status

From the above output, you can see that the repository is restored to its previous state before stash. It is showing output as “Changes not staged for commit.”

In case of more than one stash, you can use “stash apply” command followed by stash index id to apply the particular commit.

$ git stash apply <stash id> 

 

Git Stash Pop (Reapplying Stashed Changes)

Git allows the user to re-apply the previous commits by using stash pop command. The popping option removes the changes from stash and applies them to your working file.

The stash pop command is quite similar to stash apply. The main difference between both of these commands is stash pop command that deletes the stash from the stack after it is applied.

$ git stash pop

Git Stash Drop (Unstash)

This command is used to delete a stash from the queue. Generally, it deletes the most recent stash. Caution should be taken before using stash drop command, as it is difficult to undo if once applied.

The only way to revert it is if you do not close the terminal after deleting the stash.

$ git stash drop

In the above output, the most recent stash (stash@{0}) has been dropped from given three stashes. The stash list command lists all the available stashes in the queue.

We can also delete a particular stash from the queue. To delete a particular stash from the available stashes, pass the stash id in stash drop command.

$ git stash drop <stash id> 

Git Stash Clear

The stash clear command allows deleting all the available stashes at once. To delete all the available stashes, operate below command:

$ git stash clear

it will delete all the stashes that exist in the repository.

To know more about git follow the given below link:

https://blog.knoldus.com/git-submodule-an-overview/

Written by 

Jubair Ahmad is a Software Intern at Knoldus.Inc.He loves learning new technology and also have interest in playing cricket.

Leave a Reply