Introduction
Git, a well-known distributed version control system (DVCS), provides a comprehensive suite of features for managing a project’s history and collaborating effectively. While working with Git, situations arise where you want to switch to a different branch while you have uncommitted changes. Attempting to switch branches without committing your changes results in an error message like the following:
| |
This error indicates that changes in the file file.txt are blocking the branch switch. In such scenarios, you have the option to either commit your changes or temporarily save them. In this tutorial, we’ll explore how to temporarily save your changes using Git stash, allowing you to switch to a different branch.
Understanding Git Stash
Git stash is a utility that temporarily stores changes made in your working directory. It enables you to save your current work-in-progress without committing it, giving you the flexibility to switch branches. Stashes are stored in a stack-like manner, allowing you to retrieve them when needed.
Using Git Stash
1. Save Your Changes
To save the modifications in your current working directory to a stash, use the following command:
| |
This command stores changes in tracked files in a stash, leaving your working directory in a clean state.
2. List Stashed Changes
To view a list of your saved stashes, execute this command:
| |
This command displays a list of your stashes. Each stash is referenced by a unique identifier.
3. Apply Changes from a Stash
To reintroduce changes from a saved stash, use the following command:
| |
This command applies the most recent stash to your working directory. If you want to apply changes from a specific stash, you can specify its stash name:
| |
4. Drop a Stash
To remove an applied stash, use the following command:
| |
To remove a specific stash, specify its stash name:
| |
To remove all stashes, use the clear command:
| |
If you want to apply and remove a stash in one step, you can use the pop command:
| |
Practical Examples of Git Stash
When an urgent bug fix is required in a different branch while you’re working on a feature:
1 2 3 4 5git stash git checkout bugfix-branch # Work on the bug fix git checkout original-branch git stash popBefore fetching changes from a remote Git repository:
1 2 3git stash git pull origin master git stash pop # Reapply after resolving any conflicts if they ariseManaging multiple stashes:
1 2 3 4git stash save "Description of work 1" git stash save "Description of work 2" git stash list git stash apply stash@{1}
Points to Note
Untracked files are generally excluded from stashes. To include them, use the
-uoption:1git stash -uIgnored files are not included in stashes by default. To include them, use the
-aoption:1git stash -aStashes are not branch-specific. This means they can be applied from any branch.
Be cautious when applying stashes, as conflicts can occur.
Conclusion
Git stash is a powerful tool that enables you to temporarily save your ongoing work and restore it later. It increases the flexibility of your workflow and can be especially useful when handling urgent tasks or switching branches. By leveraging the capabilities of Git stash, developers can optimize their Git workflow and maintain a streamlined development environment.