Edited 1 week ago by ExtremeHow Editorial Team
GitStashWindowsMacLinuxCommand LineDevelopmentVersion ControlSource ControlWorkflowProductivity
This content is available in 7 different language
Git is one of the most widely used version control systems in the software development world. It was created by Linus Torvalds in 2005 for the development of the Linux kernel. Since then, it has been used in software projects of every size, by teams of developers…sometimes even by developers working alone. Git helps you track versions of code, collaborate with multiple developers, and ensure that you can always revert to previous versions of your software when needed.
When working on a Git-managed project, there may be times when you need to switch branches. For example, you might be working on a new feature and suddenly there is a critical bug report that needs to be fixed immediately. In such cases, you would want to switch context and work on the bug fix without losing your progress on the feature you were working on. This is where Git Stash comes in handy.
Git stash is a powerful feature that allows developers to temporarily save their changes without committing them to the repository. It effectively "shelfies" the changes you've made, allowing you to check out a new branch without committing a new feature that may not yet be ready for others to see. When you're ready, you can apply these changes back to your working copy or to another branch.
When you run git stash
command, your changes are saved to a stack. This stack can contain multiple stashes, and you can apply any of the stashes listed later. A single stash is a combination of:
Once the changes are stashed, your working directory is clean, and you can switch branches, compile your code, or do anything else you need to do. After you've completed that task, you can return to your original work by popping or applying the stash.
To start using stash in Git, you need to be familiar with some basic commands. Here's a look at the most important commands:
To store your changes, use the following command:
git stash push
This will save your local modifications and revert your working directory to match the HEAD commit. It's as if you never made those changes.
It's often useful to include a message with your hidden changes for easy identification:
git stash push -m "your stash message"
This message helps you remember what changes are in this stash without referring to line-level changes.
To see a list of all your saved collections:
git stash list
This command will print a list of all the stashes you have saved and their associated messages.
To reapply the latest stash:
git stash apply
This command applies the latest stash to your current working directory, but does not remove it from the stash list.
If you want to apply a specific stash, use:
git stash apply stash@{n}
Replace stash@{n}
with the specific stash you want to apply, which you can identify from git stash list
output.
To apply and immediately delete the latest stash:
git stash pop
This is similar to the apply command, but it also removes the applied stash from your list.
If you want to delete a particular stash (without applying it), use:
git stash drop stash@{n}
This will destroy the specified stash.
If you want to delete all stash, the command is:
git stash clear
Use this command with caution, as it will permanently destroy all stored changes.
Consider a scenario where you are developing a new feature on the branch feature-xyz
, and you have made several changes to your working copy. Suddenly, you need to fix a serious bug on the main branch.
git status
git stash push -m "Saving work on feature-xyz"
main
or any other branch you need to work on:
git checkout main
git commit -a -m "Fixed critical bug"
git checkout feature-xyz
git stash pop
If there is any disagreement, resolve it and then proceed with your work.
Git Stash offers some flexibility with more advanced options:
By default, git stash push
will only stash changes to tracked files. To include untracked files:
git stash push -u
This will archive both tracked and untracked files.
If you also want to stash ignored files (files explicitly excluded by .gitignore
):
git stash push --all
You can selectively archive parts of files using:
git stash -p
This command is interactive and allows you to select specific hunks to stash.
To reapply stored changes, including staged changes (in the index), use:
git stash apply --index
Be aware that this can cause conflicts if there are conflicting changes to the index.
While stashing and unstashing are generally straightforward, conflicts can arise, especially when the stash is applied to an updated codebase. If conflicts occur, Git will mark them like merge conflicts. You must edit the affected files, resolve the conflicts manually, and mark them as resolved:
git add <file>
Once resolved, you can continue working and eventually submit your changes.
Here are some tips to make working with Git Stash easier and more effective:
-m
option to label your stashes. This helps to easily identify them later.Git stash is a versatile tool in the developer's arsenal, particularly useful for preserving uncommitted work and temporarily changing references. By understanding how to use Git stash effectively, developers can seamlessly manage changes across different tasks, improving efficiency and flexibility in their workflow.
From saving unfinished work to focusing on important bugs, properly implementing git stash capabilities into your workflow can make handling code changes easier and more manageable. As you become comfortable with Git stash, you'll find it becomes an indispensable part of your development toolkit.
If you find anything wrong with the article content, you can