Consider this, you are working on a Git branch, adding a new feature or fixing a bug but then we are required to work on another urgent issue. So in that case, we would be required to leave all our current work & move on to the new issue. So for situations like this, git provides us with a feature called  STASH’.

Stash command for git allows us to keep aside the current working directory & provides us with a clean working directory. All the files that have been stashed can then be retrieved once we have time to work on them.

Recommended Read: Simple way to change home directory of Jenkins Server

Also Read: Informative guide to Gitlab tutorial for Beginners

In this beginner’s guide, we will discuss all the different options that can be used with stash.


 

Create a Stash

To create a stash & get a clean working directory, use the following command,

$ git stash

Or we can also use,

$ git stash save

This will stash all tracked files. In order to move all files, tracked or untracked, we need to use the following command,

$ git stash -u

Note:- Tracked files are those files that have either been added to index or committed. To see list of such files, run the command

$ git ls-files

 

Apply the stash

Now when we want to bring back the stashed files/folder back to the current working directory, we can issue the command,

$ git stash apply

 

Drop the Stash

After we have applied the stash, we no longer need that stash on our system & we need to drop it, with the following command,

$ git stash drop

 

Apply & Drop stash

We can also apply & drop the stash with a single command,

$ git stash pop

 

Creating multiple stashes

We might be required to add more than one stash on our system. So to be able to do that, we need an identifier as to which stash is which. This can be done by adding a message when we save the stash, using the following command,

$ git stash save “First stash”

$ git stash save “2nd Stash saving”

 

List all stashes

To get the list of all stashes, use the following command,

$ git stash list

It will provide the list like,

stash@{0}: On Master: 2nd Stash saving

stash@{1}: On Master: First stash

& stash@{0} will always be the latest stash.

 

To check details of a stash

We first need the stash id, which we can get from the above-mentioned command. Now to check details of all files in the stash, run

$ git stash show stash@{0}

 

Apply a stash from many stashes

To apply a stash to the current working directory, we will need the stash id which we can get from ‘ list’ command. Then we can run the following command,

$ git stash apply stash@{0}

 

Drop a stash from many stashes

Similar to above, we would need a stash id to drop it from the list. Then run the following command,

$ git stash drop stash@{0}

 

Drop all stashes

To drop all stashes from the system, execute the following command

$ git stash clear

 

Restore stash to a new branch

We can also use the stash command to bring all changes from a stash to a new branch. To do so, first, we need to stash the current working directory,

$ git stash -u

Now we have a clean working directory, next, we will create a new branch with the stashed content,

$ git stash branch branch_new

So this command creates a new branch, switch to that branch, then applies the stash to it & in last also drops the stash.

This is it for our tutorial on how to use stash on git. Please feel free to send in any questions or queries or suggestions using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Donate us some of your hard-earned money: [paypal-donation]

Linux TechLab is thankful for your continued support.