Stashing Changes Before Switching Branches
When you need to switch between branches, it's important to ensure that your current working directory is in a clean state. However, sometimes you may have uncommitted changes that you don't want to lose or commit. In such cases, you can use the Git stash feature to temporarily save your changes and restore them later.
Understanding Git Stash
Git stash allows you to temporarily save your local changes, including modified files and new untracked files, without committing them. This is useful when you need to switch to a different branch but don't want to lose your current work.
Stashing Changes
To stash your current changes, use the git stash
command:
git stash
This will save your changes in the stash and revert your working directory to the last committed state.
Viewing Stashed Changes
You can view a list of your stashed changes using the git stash list
command:
git stash list
stash@{0}: WIP on feature/new-login-page: 2a1b3c4 Implement new login page
stash@{1}: WIP on bugfix/fix-checkout-process: 5e6f7a8 Fix checkout process issue
This will display all the stashed changes, with the most recent one at the top.
Applying Stashed Changes
To apply the most recent stashed changes, use the git stash apply
command:
git stash apply
This will apply the changes from the top of the stash stack to your current working directory.
If you want to apply a specific stashed change, you can specify the stash reference (e.g., stash@{1}
) after the apply
command:
git stash apply stash@{1}
By understanding how to use Git stash, you can seamlessly switch between branches while preserving your current work and avoiding the need to commit incomplete changes.