Stashing

What is Stashing in Git?

Stashing is the process of saving uncommitted changes in a temporary area, allowing you to switch contexts without committing incomplete work. It's particularly useful when you need to switch branches but aren't ready to commit your current changes.

In the world of software development, version control systems play a crucial role in managing changes to source code over time. One of the most popular version control systems is Git. Git offers a variety of features to help developers manage their code, and one of these features is 'Stashing'. Stashing is a powerful tool that allows developers to save changes that they don't want to commit immediately. This article will delve into the concept of stashing in Git, providing a comprehensive understanding of its definition, explanation, history, use cases, and specific examples.

Stashing in Git is a mechanism that helps you to save your changes without committing them, allowing you to switch branches without losing your work. It's like a clipboard for your repository, where you can temporarily store changes and retrieve them later. This feature is particularly useful when you're in the middle of something but need to switch context. By the end of this article, you will have a deep understanding of how stashing works in Git, and how you can leverage it to improve your workflow.

Definition of Stashing

Stashing in Git refers to the process of temporarily saving changes that you have made to your working directory but do not want to commit yet. These changes are saved in a new, uncommitted state that can be reapplied at any time. The stash is a local operation; it does not affect the remote repository.

When you stash changes, Git takes the modified tracked files, stages changes, and saves them on a stack of unfinished changes that you can reapply at any time. It's important to note that only modifications to tracked files are stashed. New files, also known as untracked files, and ignored files are not included in the stash unless you specify them.

Stash Stack

The stash stack is where Git stores stashes. It's a Last-In-First-Out (LIFO) stack, meaning the most recent stash is the one that gets applied first. Each stash is identified by a name like 'stash@{0}', 'stash@{1}', and so on. You can view the stash stack using the 'git stash list' command.

Each stash is a snapshot of changes made to the working directory along with some metadata. The metadata includes the branch where the changes were made, the commit at which the changes were based, and a log message. This information helps you to understand the context of the changes when you decide to reapply them.

Explanation of Stashing

Stashing is a two-step process. The first step is to save the changes in a stash. You can do this using the 'git stash' or 'git stash save' command. When you run this command, Git saves your modifications (both staged and unstaged), reverts your working directory to the last commit, and puts the changes in a new stash. The second step is to reapply the changes from the stash when you're ready. You can do this using the 'git stash apply' command.

When you apply a stash, Git tries to reapply the changes to your working directory. If the same lines have been modified in both the stash and the current working directory, Git will attempt to merge the changes. If it cannot merge the changes automatically, it will create a merge conflict that you need to resolve manually.

Creating a Stash

To create a stash, you use the 'git stash' or 'git stash save' command followed by an optional message. The message helps you to remember what the changes in the stash are about. If you don't provide a message, Git will create a default one for you. The command takes a snapshot of your changes and saves them in a new stash. The changes are then removed from your working directory.

By default, only modifications to tracked files are stashed. If you want to include untracked files in the stash, you can use the '-u' or '--include-untracked' option. To include ignored files as well, you can use the '-a' or '--all' option.

Applying a Stash

To apply a stash, you use the 'git stash apply' command followed by the name of the stash. If you don't specify a stash, Git applies the most recent one. The command tries to reapply the changes to your working directory. If the same lines have been modified in both the stash and the current working directory, Git will attempt to merge the changes.

If Git cannot merge the changes automatically, it will create a merge conflict that you need to resolve manually. After resolving the conflicts, you can continue with the reapplication using the 'git stash apply --continue' command. If you want to abort the reapplication, you can use the 'git stash apply --abort' command.

History of Stashing

Git was initially released in 2005, but the stash feature was not part of the original release. It was introduced in version 1.5.3, which was released in 2007. The feature was added to help developers manage their changes more effectively when switching between branches.

Since its introduction, stashing has become a popular feature among Git users. It has been improved and refined over the years to make it more powerful and flexible. For example, in earlier versions of Git, you could only stash changes from the command line. But in recent versions, many graphical user interfaces (GUIs) for Git, like GitHub Desktop and Sourcetree, have added support for stashing.

Use Cases of Stashing

Stashing is a versatile feature that can be used in a variety of situations. One common use case is when you're in the middle of a task and need to switch to a different task. Instead of committing half-done work, you can stash your changes, switch to the other task, and then come back and reapply your changes when you're ready.

Another use case is when you want to pull the latest changes from the remote repository but have local changes that you're not ready to commit. You can stash your changes, pull the latest changes, and then reapply your stash. This way, you can keep your local changes without preventing the pull operation.

Stashing in Multi-Branch Workflows

In multi-branch workflows, stashing is a handy tool. You might be working on a feature in a branch and then need to switch to another branch to fix a bug. Instead of committing your half-done feature, you can stash your changes, switch to the bug-fix branch, fix the bug, and then switch back to the feature branch and reapply your stash.

Stashing is also useful in multi-branch workflows when you want to test your changes against different branches. You can stash your changes, switch to the branch you want to test against, apply your stash, run your tests, and then drop the stash.

Stashing in Collaborative Workflows

In collaborative workflows, where multiple developers are working on the same repository, stashing can help you manage your local changes effectively. If a teammate pushes a commit that affects your work, you can stash your changes, pull the latest changes, and then reapply your stash. This way, you can integrate your teammate's changes without losing your work.

Stashing is also useful in collaborative workflows when you're reviewing someone else's work. You can stash your changes, check out the branch with the work you're reviewing, make any necessary comments or modifications, and then switch back to your branch and reapply your stash.

Examples of Stashing

Let's look at some specific examples of how to use stashing in Git. Suppose you're working on a new feature in a branch called 'feature'. You've made some changes, but the feature is not ready to be committed yet. Suddenly, you need to switch to the 'master' branch to fix a critical bug. Here's how you can use stashing to manage your changes:

First, you stash your changes in the 'feature' branch using the command 'git stash save "Work in progress on feature"'. This command saves your changes in a new stash with the message "Work in progress on feature". Your working directory is now clean, and you can switch to the 'master' branch.

Applying a Stash in a Different Branch

After fixing the bug in the 'master' branch, you want to switch back to the 'feature' branch and continue your work. You can do this by first switching to the 'feature' branch using the command 'git checkout feature'. Then, you apply your stash using the command 'git stash apply'. This command reapplies your changes to the 'feature' branch. You can now continue your work on the feature.

If you have multiple stashes and want to apply a specific one, you can do so by specifying the name of the stash. For example, if you want to apply the stash 'stash@{1}', you can use the command 'git stash apply stash@{1}'.

Dropping a Stash

Once you've reapplied a stash and made sure everything is working as expected, you might want to remove the stash from the stash stack. You can do this using the 'git stash drop' command. If you don't specify a stash, Git drops the most recent one. For example, you can drop the stash 'stash@{0}' using the command 'git stash drop stash@{0}'.

If you want to apply a stash and drop it in one command, you can use the 'git stash pop' command. This command applies the stash and then drops it. For example, you can pop the stash 'stash@{0}' using the command 'git stash pop stash@{0}'.

Conclusion

Stashing is a powerful feature in Git that allows you to save changes that you don't want to commit immediately. It's a versatile tool that can be used in a variety of situations, from switching tasks to managing changes in multi-branch and collaborative workflows. By understanding how stashing works and how to use it effectively, you can improve your Git workflow and become a more efficient developer.

Remember, stashing is a local operation that does not affect the remote repository. It's a temporary storage for changes that you can reapply at any time. Use it wisely, and it can be a lifesaver in many situations. Happy stashing!

High-impact engineers ship 2x faster with Graph
Ready to join the revolution?
High-impact engineers ship 2x faster with Graph
Ready to join the revolution?

Do more code.

Join the waitlist