Introduction
Today, I’m going to show you how to effectively use Git Stash commands.
Git is an essential tool for developers, he can manage his code versions efficiently. One of the key features in Git that helps developers work more flexibly is Git Stash.
This feature allows you to temporarily save your changes without committing them, which is useful when you need to switch branches or tasks.
In this guide, we’ll walk through the Git stash commands, including What is Git Stash, how to stash and apply changes, view stashed content, and more.
What is Git Stash
In simple terms, Git stash lets you save your local changes (both staged and unstaged) temporarily.
This is ideal when you’re working on a task but need to switch branches or revisit your changes later without committing unfinished work.
By using Git stash, you can keep your repository clean, avoid committing incomplete features, and still safely switch between tasks.
Git Stash Commands:
git stash
Use: Saves your uncommitted changes (both staged and unstaged) and restores the working directory to match the HEAD commit.
Syntax: git stash
Example:
git stash
git stash list
Use: Displays a list of all stashes you’ve saved.
Syntax: git stash list
Example:
git stash list
git stash show
Use: Displays a summary of the changes in a stash, listing the modified files.
Syntax: git stash show
Example:
#Shows a summary of the files changed (modified/added).
git stash show
#Shows the detailed diff (patch) of changes made in the stash.
git stash show -p
#Includes untracked files in the summary (same as --include-untracked).
git stash show -u
#Same as -u, explicitly includes untracked files.
git stash show --include-untracked
#Shows the detailed diff of changes, including untracked files.
git stash show -p -u
#Shows a detailed diff of only untracked files.
git stash show -p --only-untracked
# Show the individual stash
git stash show stash@{1}
git stash show stash@{1} -p
git stash show stash@{1} -u
git stash show stash@{1} --include-untracked
git stash show stash@{1} --only-untracked
git stash apply
Use: Applies the changes from a specific stash without removing it from the stash list.
Syntax: git stash apply <stash>
Example:
# This command will apply the changes from stash@{0} but will not delete the stash from the list.
git stash apply stash@{0}
git stash pop
Use: Applies the changes from a specific stash without removing it from the stash list.
Syntax: git stash pop <stash>
Example:
# This applies and removes stash@{0} from your list of stashes.
git stash pop stash@{0}
git stash branch
Use: Create a branch from stash
Syntax: git stash branch <branch_name>
Example:
git stash branch <branch_name> stash@{0}
git stash clear
Use: Delete the stash
Syntax: git stash clear
Example:
git stash clear
git stash drop < stash >
Use: Delete the stash
Syntax: git stash drop <stash>
Example:
git stash drop stash@{2}
git stash save < message >
Use: Save your stash with a message
Syntax: git stash save <message>
Example:
git stash save "Your stash message"
Conclusion
The Git stash command is an incredibly useful tool for developers to manage unfinished work. Whether you need to save temporary changes, switch branches, or apply stashed work later, Git stash helps you stay organized.
By understanding the full range of Git stash commands, you can enhance your workflow, avoid unnecessary commits, and keep your repository clean.
In this guide, we’ve covered the Git stash commands and explained their syntax and examples. Now you can confidently use Git stash in your day-to-day development work to your tasks!
Thank you for reading this article.