Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

I hope that, you are finding my articles on Git Tutorials helpful. Till now, as part of the Git Tutorial Series we learned about the changes, the commit to the staging area, viewing the working tree status and pushing the changes to the remote repository.

 

In this part of the tutorial series, we will learn how to stash the changes of a working directory and pop it where/when require. Lets start discussing about “git stash” command and it’s parameters.

 

Stash the changes in a dirty working directory (www.kunal-chowdhury.com)

 

When you want to record the current state and index of the dirty working directory, but want to go back to a clean working directory, you can use the command “git stash”. This command saves your local modifications away and reverts the working directory to match the HEAD commit.

 

The modifications stashed away by this command can be listed with “git stash list”, inspected with the command “git stash show”, and restored with “git stash apply” on top of  different commit. Calling “git stash” without any arguments is equivalent to “git stash save”, which we are going to list down later on this post.

 

The latest stash you created is stored in “refs/stash”; older stashes are found in the reflog of this reference and can be named using the usual reflog syntax (e.g. stash@{0} is the most recently created stash, stash@{1} is the one before it, stash@{2.hours.ago} is also possible).

 

Here is the list of command parameters, which you will find helpful while working with Git repositories, Git branches and commits to stash your working directory.

 

Record the current state of working directory, index it and clean:

$ git stash

$ git stash create

$ git stash save

 

Stash your local modifications with a message:

$ git stash create <message>

$ git stash save <message>

 

Stash local modifications including all untracked files:

$ git stash -u

$ git stash --include-untracked

$ git stash save -u <message>

$ git stash save --include-untracked <message>

 

Stash local modifications including all ignored files:

$ git stash -a

$ git stash --all

$ git stash save -a <message>

$ git stash save --all <message>

 

List the stashes that you currently have:

$ git list

 

Show the changes recorded in the latest stash:

$ git show

 

Show the changes recorded in the stash no. provided:

$ git show <stashname>

 

Remove the last stashed state from the stash list and apply:

$ git stash pop

$ git stash pop --index

 

Remove a single stashed state from the stash list and apply:

$ git stash pop <stashname>

$ git stash pop --index <stashname>

 

Apply the latest stash without removing from stash list:

$ git stash apply

$ git stash apply --index

 

Apply the stash without removing from stash list:

$ git stash apply <stashname>

$ git stash apply --index <stashname>

 

Create and check out a new branch from the stash:

$ git branch <branchname> <stashname>

 

Remove all the stashed states:

$ git stash clear

 

Remove the last stashed state from the stash list:

$ git stash drop

 

Remove a single stashed state from the stash list:

$ git stash drop <stashname>

 

 

Was this post useful? If you came to this page by searching online about “git basics” or “git tutorials”, please have a look into my other blog posts. Subscribe to the RSS feed or the email newsletter to keep yourself updated.

 

 

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.