Stashing Your Changes, A Quick Fix for Git Pull Errors


Have you ever encountered this error when running git pull?

error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

This message indicates that Git has detected some modifications in your working directory that haven’t been staged or committed yet. To prevent conflicts or potential data loss during the pull, Git is asking you to either commit or stash your changes. Today, I’ll show you how to use the stash command to safely set aside your changes, pull from the remote repository, and then reapply your modifications.

Step 1: Stash Your Changes

The simplest way to save your unstaged changes is to run:

git stash

This command temporarily saves your modifications and reverts your working directory back to the state of the last commit. Now that your workspace is clean, Git can safely pull the changes from the remote repository.

Step 2: Pull the Latest Changes

With your working directory clean, go ahead and pull the remote changes:

git pull

Since your changes are safely stashed away, the pull operation should complete without any issues.

Step 3: Reapply Your Stashed Changes

After successfully pulling the latest updates, you’ll want to reapply the changes you stashed earlier. Use this command to bring your modifications back:

git stash pop

This command reapplies the stashed changes to your working directory and simultaneously removes the stash from your list. If you prefer to apply the stash without removing it (perhaps as a backup), you can use:

git stash apply

Optional: Review Your Stashes

If you ever need to see a list of all your stashed changes, simply run:

git stash list

This can be incredibly helpful if you’ve created multiple stashes and want to revisit an earlier change.

Wrapping Up

Using git stash is a quick and effective way to manage temporary changes while keeping your Git operations smooth and error-free. Next time you encounter the pull error due to unstaged changes, remember that stashing provides you with a safe way to temporarily shelf your work, update your repository, and then seamlessly reintegrate your modifications.


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC