10 Useful Commands for Git Stash

Feb 7, 2021 ยท 1 min read
posted in devops

a lego man with a stash

The git stash command is handy when you need to switch to another branch but you're not ready to create a commit.

1. Stash Your Work for Later

git stash

It's like doing a commit, without having to create a message. When you run this command you'll see your work disappear in front of your eyes. Don't worry it's safe!

2. Stash With a Message

git stash save message...

Your stashes are usually saved with a default name like stash@{1} for the most recent stash. This is what you'll use to apply the stash later. When you have multiple stashes it's handy to have names for them so you have a quick idea of what the stash was for.

3. Stash on a New Branch

git stash branch <name> stash@{1}

This will create a new branch with your stash already applied.

4. View Your Stashes

git stash list

5. View Your Stashes With Dates

git stash list --date=local

6. Inspect a Stash

git stash show stash@{0}

7. Restore the Most Recent Stash and Delete It

git stash pop

Not something I use often. I prefer to keep the stash after I've applied it, just incase I want to reset and start over.

8. Delete a Specific Stash

git stash drop stash@{0}

9. Delete All Your Stashes

git stash clear

10. Apply a Stash

git stash apply stash@{0}

Apply the stash named stash@{0} (most recent one), while retaining it.

Final Note

I hope you found this useful. Let me know if you know of any useful git stash commands.



Join the mailing list to get new articles straight to your inbox.

No spam, sales or ads. Unsubscribe any time.