Git Stash: In Git, the git stash
command is used to temporarily save changes that you're not ready to commit yet, so you can switch to a different branch or perform other operations without committing incomplete work.
Git Pop: The git stash pop
command is used to apply the most recent stash and remove it from the stash list. It's like a combination of git stash apply
and git stash drop
.
Example Scenario: Imagine you're working on a feature branch and need to switch to another branch to fix a bug. However, you don't want to commit the incomplete changes on the feature branch. This is where git stash
comes in handy.
Step-by-Step Example:
Create a New Feature Branch:
git checkout -b feature/my-feature
Make Changes: Edit files in your codebase.
Stash Changes: Stash the changes you've made but aren't ready to commit.
git stash
Switch to a Different Branch:
git checkout main
Fix a Bug on Main Branch: Make necessary changes on the
main
branch to fix a bug.Commit Bug Fix:
git add <changed_files> git commit -m "Fixed bug"
Switch Back to Feature Branch:
git checkout feature/my-feature
Apply Stashed Changes: Apply the stashed changes from the feature branch.
git stash pop
This will apply the stashed changes and remove the stash from the stash list.
Continue Working: Now you can continue working on your feature branch, which now includes the stashed changes.
Commit Stashed Changes: If needed, commit the stashed changes.
git add <changed_files> git commit -m "Added stashed changes"
Additional Stash Commands:
git stash list
: Lists all stashes.git stash apply stash@{n}
: Applies a specific stash without removing it from the stash list.git stash drop stash@{n}
: Removes a specific stash from the stash list.git stash clear
: Removes all stashes.
Note: It's important to understand that using git stash
is a temporary solution. It's generally recommended to commit your changes properly before switching branches. Stashing is more suitable for quick switches or cases where you're not ready to commit yet.
git stash
and git stash pop
are valuable commands in Git when you need to temporarily save your changes, switch branches, and then reintegrate your changes. This allows you to maintain a clean and organized development workflow while still preserving your work in progress.