To stash your current changes (both staged and unstaged), use:
git stashOr to include untracked files as well:
git stash -uTo give your stash a description for easier identification later:
git stash save "your description here"To view all stashed changes, use:
git stash listEach stash will be listed in the form:
stash@{index}: description
Where index is the stash's position in the list.
To view the changes in a specific stash:
git stash show stash@{index}To view the full diff of a specific stash:
git stash show -p stash@{index}To apply the latest stash to your working directory:
git stash applyTo apply a specific stash:
git stash apply stash@{index}After applying a stash, it remains in the list until manually dropped. To drop the latest stash:
git stash dropTo drop a specific stash:
git stash drop stash@{index}To clear all stashes:
git stash clearTo apply a stash and then immediately remove it from the stash list:
git stash popTo pop a specific stash:
git stash pop stash@{index}If you accidentally dropped a stash and want to recover it, you can try:
git fsck --lost-foundLook for the commit SHA of the dropped stash, then create a new branch from that commit:
git checkout -b recovered-branch <commit-sha>To create a new branch from a specific stash:
git stash branch new-branch-name stash@{index}To stash only the unstaged changes, without touching the staged ones:
git stash --keep-indexTo stash only staged changes (not unstaged or untracked files):
git stash --patchKeep these commands handy to effectively manage and recover stashed changes in Git.