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!