The git stash command is used to temporarily save uncommitted changes in your working directory. It is useful when you are working on one task but need to quickly switch to another branch without committing unfinished work.
git stash pop or
git stash apply.
git stash temporarily saves changes that have not yet been committed.
git stash
After stashing, your working directory is normally returned to a clean state.
Suppose you are working on a feature but suddenly need to switch to another branch. Your current work is incomplete and you do not want to create a temporary commit.
You can temporarily save your work with:
git stash
The simplest stash command is:
git stash
Git saves supported local modifications and restores the working tree to a clean state.
Before stashing, check your current changes:
git statusgit diffThis helps you understand what you are about to temporarily store.
git status
git stash
git status
After the stash, the working tree should normally show that the changes have been temporarily stored.
Use the following command to list your stashes:
git stash list
Example:
stash@{0}: WIP on main
stash@{1}: WIP on feature-login
Git identifies each stash using a reference such as:
stash@{0}
The number identifies the position of the stash in the stash list.
The newest stash is normally stash@{0}.
Use git stash apply to restore a stash while keeping
the stash entry in the stash list.
git stash apply
This normally applies the latest stash.
You can specify a particular stash:
git stash apply stash@{1}
This applies the changes stored in the selected stash.
The git stash pop command applies the latest stash and removes that stash entry if the application completes successfully.
git stash pop
It is useful when you want to restore your work and no longer need that stash entry.
| Command | Apply Changes | Remove Stash Entry |
|---|---|---|
git stash apply |
Yes | No |
git stash pop |
Yes | Normally yes, if successful |
To remove a specific stash entry:
git stash drop stash@{0}
This removes the selected stash from the stash list.
To remove all stash entries:
git stash clear
You can give a stash a descriptive message:
git stash push -m "Work on login page"
A useful message makes it easier to identify the purpose of a stash.
You can inspect the changes stored in a stash using:
git stash show
To see more detailed changes:
git stash show -p
By default, git stash primarily handles changes to
tracked files. To include untracked files, use:
git stash push -u
The -u option means include untracked files.
The -a option can include untracked and ignored files:
git stash push -a
Use this carefully because ignored files are normally excluded from a standard stash operation.
Git can interactively select parts of your changes for a stash:
git stash push -p
Git asks which hunks should be included in the stash.
A common workflow is:
git status
git stash
git switch main
Your unfinished work is temporarily stored while you work on another branch.
After returning to the appropriate branch, restore your work:
git stash pop
Your previously stashed changes are applied to the working directory.
Applying a stash can sometimes produce conflicts if the current files have changed since the stash was created.
git stash pop
If conflicts occur, use:
git status
Then resolve the files just as you would resolve other Git conflicts.
A stash is not the same as creating a normal project commit. It is a Git-managed temporary storage mechanism for local changes.
git stash
The changes are stored in the repository's stash structure and can later be applied or removed.
A stash can be created while working on one branch and later applied to another branch. However, the changes may not apply cleanly if the branches have different file contents.
git stash
git switch another-branch
git stash apply
Git provides a convenient command for creating a new branch from a stash:
git stash branch new-feature
This creates a new branch from the commit where the stash was originally created and attempts to apply the stash to it.
# Check current changes
git status
# Temporarily save work
git stash push -m "Incomplete login work"
# Switch branch
git switch main
# Work on another task
# Return to feature branch
git switch feature-login
# View stashes
git stash list
# Restore work
git stash pop
git stash clear without checking the stash list.git stash apply removes the stash.git stash
git stash list
git stash show
git stash show -p
git stash push -m "Message"
git stash push -u
git stash push -a
git stash apply
git stash pop
git stash drop
git stash clear
git stash branch new-branch
# Check changes
git status
# Save unfinished work
git stash push -m "Unfinished feature"
# Check stash list
git stash list
# Switch branch
git switch main
# Work on another task
# Return to original branch
git switch feature-login
# Restore work
git stash pop
# Check result
git status
git switch -c feature-profile
# Modify profile.php
git status
# Temporarily save changes
git stash push -m "Profile page work"
# Switch to main
git switch main
# Fix another issue
git add .
git commit -m "Fix homepage issue"
# Return to feature branch
git switch feature-profile
# Restore profile work
git stash pop
git status
This workflow allows you to temporarily pause unfinished work and return to it later.
The git stash command temporarily saves uncommitted changes so that you can work with a clean working directory.
git stash
git stash list
git stash pop
Use git stash apply when you want to restore changes
while keeping the stash entry, and use git stash pop
when you normally want to restore the changes and remove the stash
entry after successful application.
git stash list displays saved stashes.git stash apply restores a stash without normally removing it.git stash pop restores the latest stash and normally removes it if successful.git stash drop removes a specific stash.git stash clear removes all stash entries.git stash push -m allows you to add a descriptive message.git stash push -u includes untracked files.git stash push -a includes untracked and ignored files.git stash show -p displays detailed stash changes.git stash branch can create a branch from a stash.Question: Which command temporarily saves your uncommitted changes?