19. Stashing: Temporarily Saving Changes

Software development is a dynamic process and developers often need to manage different tasks at the same time. During this process, the need may arise to switch between branches without wanting to commit current work that is not yet ready to be committed. This is where the concept of Stashing in Git becomes extremely useful.

What is Stashing?

git stash is a powerful command that allows developers to save their local changes temporarily in an area that Git calls "stash". These changes can be either modifications to crawled files or new files that have not yet been added to the index (staged). The stash is a stack, which means you can have multiple stashes and manage them as a list.

When to use Stashing?

Stashing is particularly useful when:

  • You're in the middle of work and you need to quickly switch contexts, like fixing a critical bug in another branch.
  • You want to try something in your code without creating a commit for those changes.
  • You need to clean up your working directory to pull upstream changes without committing your own changes.

How to Stash Changes

To create a new stash, you can simply run:

git stash

This will save your changes and clean up the working directory. If you want to include changes to files that have not yet been tracked by Git (untracked files), you can use:

git stash -u

Or, to also include ignored files:

git stash -a

If you want to give your stash a specific name to remember the context of the changes, you can use:

git stash save "explanatory message"

Listing and Inspecting Stashes

To see all the stashes you've created, use:

git stash list

Each entry in the list will have a unique identifier in the form of stash@{} where is a number indicating the position of the stash in the stack .

To see what's inside a specific stash without applying it, you can use:

git stash show -p stash@{}

Applying a Stash

When you're ready to continue working on the changes you saved, you can apply the latest stash with:

git stash apply

Or a specific one with:

git stash apply stash@{}

This reapplies changes saved to your working directory, but keeps the stash on the stack. If you want to apply and remove stash from the stack, you can use:

git stash pop

Dealing with Conflict

When you apply a stash, conflicts may occur if your current working directory has changes that overlap with the stash changes. In this case, Git will alert you about the conflicts and you will have to resolve them manually, just as you would with a merge.

Removing a Stash

If you want to discard a stash that you know you will no longer need, you can remove a specific one with:

git stash drop stash@{}

Or, to clear all stashes:

git stash clear

Branches and Stashes

Sometimes it can be useful to stash a new branch. This can be done with:

git stash branch  stash@{}

This creates a new branch from the commit where the stash was created, applies the stash, and if successful, removes the stash from the stack.

Best Practices with Stashing

  • Use descriptive messages when saving stashes to make them easier to identify in the future.
  • Avoid accumulating too many stashes. Apply or discard them regularly to keep your stash stack manageable.
  • Before applying a stash, make sure you are on the correct branch and that your working directory is clean.

Conclusion

Stashing is an incredibly useful feature of Git that allows developers to manage temporary changes efficiently. It gives you the flexibility to handle workflow interruptions and helps keep your working directory clean. Understanding and using stashing can significantly increase your productivity and make it easier to manage parallel tasks in software development.

Now answer the exercise about the content:

Which of the following Git commands is used to temporarily save local changes to an area called "stash"?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Working with tags 20

Next page of the Free Ebook:

Working with tags

Estimated reading time: 4 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks