How to Stash Changes in Git

Git logo on a blue background

Need to switch to a different branch, but you’re not ready to commit the changes you’ve made in your current branch? You can stash your changes and come back to them later. It’s possible in Git, whether you use GitHub or another hosting service.

How to Stash Changes

You can stash your changes by running a simple command. Before doing that, though, you can run a different command to see exactly what you’ll be stashing. In your working branch, run this command:

git status
Run the git status command.

ADVERTISEMENT

This will show you both the staged and unstaged changes you’ve made in your branch. In our case, we’ve modified the “test.md” file. Keep in mind that git stash will stash both staged and unstaged changes.

Now that you’ve reviewed what will be stashed, run this command to stash the changes:

git stash
Run the git stash command.

Once executed, you’ll then receive a message stating that your changes have been stashed on <branch-name>.  Your branch will now look like it did before you made your changes, and it’s now safe to switch over to a new branch.

View Stashed Changes

If you’ve saved several stashes, you may want to see a list of the stashes before you try to retrieve one. When you view a list of your stashes, take note of the name of the stash you want to retrieve and continue working on.

In the terminal, run this command:

git stash list
Run git stash list command.

A list of stashes will then be returned. In the example above, our stash name is stash@{0}. The number inside the curly brackets is the index. If you have several stashes on the same branch, the number will be different.

An example of a stash with different numbers.

If you want to view the details of a stash, run:

git stash show
Run the git stash show command.

ADVERTISEMENT

You can also run git stash show -p to view the results in diff format.

Retrieve Stashed Changes

Once you’re ready to pick up where you left off, you’ll need to retrieve your stashed changes. There are two different ways you can do this. One command will keep a copy of your changes in the stash while also copying it over to your working branch. The other will copy everything over to your working branch, but will remove everything from the stash.

To keep a copy of your changes in the stash and also bring them over to your working branch, run:

git stash apply
run git stash apply command.

To bring the changes to your working branch but delete the copy of the stash, run:

git stash pop
Run the git stash pop command.

If there are multiple stashes on a single branch, simply add the name of the stash to the end of the command.

You can now continue working with your previous changes. Once you’ve made all the necessary changes to the branch and have merged it to the main branch, don’t forget to delete the branch to keep your repository clean!

Reference:
https://www.howtogeek.com/777899/how-to-stash-changes-in-git/

Leave a Comment

Your email address will not be published. Required fields are marked *