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.
Then later on when you want to come back to your modification, use 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}
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.
Will look like this when using the git stash list
command.
Happy stashing!
Top comments (6)
git stash --all
is generally what a dev wants. Stash everything that is or isn't tracked. I have my aliased togit sta
Indeed, that is a great flag to use and a great recommendation, thanks!
Great post! đź‘Ź
didn't knew about this, will use it from now, thank you !!
git stash is a dear old friend, I'm happy to see some people loves it and are promoting its use
My most used git command, don't know what I'd do without it.