Git Basics: How to stash the changes in a dirty working directory?


Hopefully you are finding my posts on Git Basics helpful. As of 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.

 

 

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

0 comments

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top