Stash changes
Use git stash when you want to change to a different branch but you have
uncommitted changes that are not ready to be committed.
Create stash entries
By default, git stash stores tracked changes in your working directory and any staged changes.
You can use options to control which changes are included.
To stash tracked changes:
git stashTo stash changes with a message:
git stash push -m "describe your changes here"To stash changes but keep staged changes in your working directory:
git stash push -kThe
-k(--keep-index) option stashes your changes but also keeps them in the working directory. Use this option when you want to temporarily save changes but keep working on them.To stash changes and include untracked files:
git stash push -uThe
-u(--include-untracked) option also stashes files that Git is not yet tracking. Without this option, new files that have not been committed remain in your working directory.To stash only staged changes:
git stash push -SThe
-S(--staged) option stashes only changes that are staged. Use this option when you want to save staged changes while you keep working on unstaged changes.
Apply stash entries
If you make many changes after you stash your work, conflicts might occur when you apply the stash. You must resolve these conflicts before the changes can be applied.
To apply the most recent stash entry and keep it in the stash:
git stash applyTo apply a specific stash entry:
git stash apply stash@{3}To apply the most recent stash entry and remove it from the stash:
git stash pop
View stash entries
To see all stash entries:
git stash listTo see stash entries with more detail:
git stash list --stat
Delete stash entries
To delete the most recent stash entry:
git stash dropTo delete a specific stash entry:
git stash drop <name>To delete all stash entries:
git stash clear
Example: Create and apply a stash entry
To try using Git stashing:
Modify a file in a Git repository.
Stash the modification:
git stash push -m "Saving changes from edit"View the stash list:
git stash listConfirm there are no pending changes:
git statusApply the stashed changes and remove the entry from the stash:
git stash popView the stash list to confirm the entry was removed:
git stash list