DEV Community

Cover image for git stash
Stefan Alfbo
Stefan Alfbo

Posted on

git stash

The git stash is probably one of my most used git commands after pull and push.

This command can be useful when you need to switch branches, but the current changes are not ready to be committed. Or you're experimenting with the code but unsure if it's worth committing to the history of the repository. The stashing allows you to temporarily set aside the current work which can also be good if you want to sync the local branch with the remote repository.

The simplest workflow with git stash is just to use git stash which will save your local modifications away and reverts the working directory to match the HEAD commit.

git stash

Then later on when you want to come back to your modification, use git stash pop.

git stash pop

However there are more power to this command than the example above. Here are some other sub-commands to the git stash command.

# List the stash entries that you currently have. 
git stash list

# Creates and checks out a new branch named <branchname>
# with the stashed code
git stash branch <branchname>

# Stash with a message
git stash push -m"<descriptive message>"

# Stash untracked files
git stash -u

# Apply a specific, n, stash from the stash list
git stash apply stash@{n}
Enter fullscreen mode Exit fullscreen mode

I like to use the command git stash push -m"my message" when stashing, that will make it far easier to know what each stash is containing.

stash with message

Will look like this when using the git stash list command.

git stash list

Happy stashing!

Top comments (6)

Collapse
 
natescode profile image
Nathan Hedglin

git stash --all is generally what a dev wants. Stash everything that is or isn't tracked. I have my aliased to git sta

Collapse
 
stefanalfbo profile image
Stefan Alfbo

Indeed, that is a great flag to use and a great recommendation, thanks!

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! 👏

Collapse
 
chandrashekhar profile image
Chandrashekhar Mehta • Edited

didn't knew about this, will use it from now, thank you !!

Collapse
 
ccoveille profile image
Christophe Colombier

git stash is a dear old friend, I'm happy to see some people loves it and are promoting its use

Collapse
 
lnahrf profile image
Lev Nahar

My most used git command, don't know what I'd do without it.