Stash Selected Files
To save changes in one or more files temporarily, list their paths after --. Git saves their staged and unstaged edits, then returns those files to their last committed state.
Before starting, unstage changes in files you want to leave out. A stash can otherwise also capture those staged changes.
git stash push -m "Work in progress" -- <file-1> <file-2> Keep Staged Changes in Place
Add --keep-index to put aside unstaged edits in the selected files while leaving their staged changes available, for example to test a prepared commit. The stash still records the staged changes as well.
git stash push --keep-index -m "Work in progress" -- <file-1> <file-2> Include New Files
New, untracked files are left out by default. Add --include-untracked and include the new files among the paths you specify. The saved new files are removed from the working tree until you restore them.
A new file that has already been staged with git add is included without this option.
git stash push --include-untracked -m "Work in progress" -- <file-1> <file-2> Inspect a Stash
List your stashes and choose the entry you want to inspect. The example shows the latest entry, stash@{0}, including any new files it contains.
git stash list
git stash show --patch --include-untracked "stash@{0}" Restore the Changes
Apply the chosen stash when you are ready to continue. apply keeps the saved entry, so you can check the restored changes before removing it.
git stash apply "stash@{0}" - Remove the Entry After Review: Once you have checked the restored files and resolved any conflicts, drop the saved entry. Check its current number if you have added or removed other stashes in the meantime.
git stash drop "stash@{0}" Considerations
- Stashing requires at least one commit in the repository.
--include-untrackedleaves ignored files out of the stash.- To also restore which changes were staged, use
git stash apply --index "stash@{0}". - Applying a stash can cause conflicts if the files have changed since it was created. Keep the stash until you have verified the result.
- Stashes belong to your local repository; pushing a branch does not publish them.