Git Stash
In the dynamic world of software development, interruptions and context switches are common.
Git's git stash
command comes to the rescue, providing a flexible way to save
and apply changes on the fly. We'll explore the diverse applications of
the git stash
command, offering you a toolkit to manage your work seamlessly.
1. Stash Uncommitted Changes:
Stash changes in your working directory without committing them:
bashgit stash save "Your stash message"
Replace "Your stash message" with a descriptive message about the changes you're stashing. This creates a new stash with a unique identifier.
2. Stash Including Untracked Files:
Include untracked files in your stash using the -u
or
--include-untracked
option:
bashgit stash save -u "Stashing with untracked files"
This is useful when you want to stash both modified and untracked files.
3. Stash Only Untracked Files:
If you want to stash only untracked files without modifying existing tracked files:
bashgit stash save --keep-index "Stashing only untracked files"
The --keep-index
option ensures that the changes you've already staged are not
stashed.
4. Stash and Include Ignored Files:
To stash changes, including ignored files:
bashgit stash save -a "Stashing changes with ignored files"
This command includes all changes, even those listed in your .gitignore
file.
5. List Stashes:
View a list of your stashes:
bashgit stash list
This provides a quick overview of your stashes, showing the stash index, branch, and message.
6. Apply the Latest Stash:
Apply the changes from the latest stash to your working directory:
bashgit stash apply
This command leaves the stash in the stash list, allowing you to apply it again if needed.
7. Apply a Specific Stash:
Apply changes from a specific stash using its index:
bashgit stash apply stash@{2}
Replace {2}
with the index of the stash you want to apply.
8. Pop the Latest Stash:
Apply the changes from the latest stash and remove it from the stash list:
bashgit stash pop
This is a convenient way to apply and drop the latest stash in one command.
9. Drop a Stash:
Remove a specific stash from the stash list:
bashgit stash drop stash@{1}
Replace {1}
with the index of the stash you want to drop.
10. Clear All Stashes:
Remove all stashes from the stash list:
bashgit stash clear
This action is irreversible, so use it with caution.
11. Create a Branch from a Stash:
Create a new branch and apply changes from a specific stash:
bashgit stash branch new-branch stash@{0}
Replace new-branch
with the desired branch name and {0}
with the
index of the stash.
12. Keep Staged Changes:
Stash only unstaged changes, keeping the staged changes in your working directory:
bashgit stash save --keep-index --include-untracked "Stashing unstaged changes"
This is useful when you want to separate staged and unstaged changes.
13. Stash Interactive Mode:
Stash changes interactively, choosing which changes to stash:
bashgit stash save -p "Stashing interactively"
This opens an interactive prompt, allowing you to select individual changes to stash.