In Git, stashing lets you save uncommitted changes temporarily, clearing your working directory to focus on other tasks or switch contexts. This command is particularly helpful when you’re working on something and need to temporarily set aside your changes without committing them. Let’s explore how to use Git’s stashing feature effectively.
To create a stash, use the following command:
git stash
The above command stashes all uncommitted changes (both staged and unstaged) in tracked files. Once the stash is created, your working directory returns to the last committed state, and all modified files are removed temporarily.
Example: If you have modified file1.txt
and file2.txt
, running git stash
will save those changes and clear your working directory, allowing you to work with a clean state.
To label the stash with a message for easier identification later, use:
git stash push -m "Message describing the changes"
Example: git stash push -m "Updated homepage layout and styles"
creates a stash with a custom message. This helps in easily identifying the purpose of each stash.
To view all the stashes in your repository, use:
git stash list
This command shows all stashes in a stack format. Each stash is labeled as stash@{index}
, where index
represents the position of the stash in the stack (starting from stash@{0}
for the most recent).
stash@{0}: WIP on main: 4567abc Updated homepage layout
stash@{1}: WIP on main: 1234def Add search functionality
To see what changes a stash contains, use:
git stash show stash@{0}
For a detailed view of the exact modifications:
Applying a stash reintroduces the saved changes into your working directory.
Use apply
to retrieve the changes from a stash without removing it from the stack:
git stash apply stash@{0}
If you don’t specify an index, Git will apply the latest stash (stash@{0}
) by default.
apply
is ideal when you want to keep a copy of the stash for future use, as it doesn’t delete the stash from the stack.The pop
command works like apply
but also removes the stash from the stack after applying it:
git stash pop stash@{0}
Explanation: Use pop
when you no longer need the stash after retrieving it, helping to keep your stash stack tidy.
To stash untracked files (files not added to the Git index), use:
git stash push -u
To stash files ignored by Git (as per .gitignore
), use:
git stash push -a
If you only want to stash specific changes, use interactive patch mode:
git stash push -p
Explanation: In patch mode, Git will prompt you to select which changes to include in the stash. This is useful when you want to stash certain parts of a file while leaving other parts in your working directory.
When stashes are no longer needed, you can delete them individually or clear all at once.
To remove a particular stash, use:
git stash drop stash@{1}
To delete all stashes at once:
git stash clear
Explanation: This command is especially useful when you’ve applied or no longer need the changes stored in the stash stack and want to keep your repository clean.
Sometimes, when applying a stash, you may encounter conflicts if there are changes in your working directory that clash with the stashed changes. Git will alert you to these conflicts, and you’ll need to resolve them just as you would with merge conflicts.
stash@{0}
, Git will leave the conflicted files in your working directory, marking the areas of conflict. After resolving, stage the changes and continue with your workflow.Stashing changes in Git is a powerful tool for managing temporary changes, giving you the flexibility to save work-in-progress without committing. By understanding how to create, apply, delete, and manage stashes, you’ll be able to handle interruptions or multitask within a Git repository efficiently. Happy coding !❤️