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.

Why Stash Your Changes?

Stashing your changes is a great way to keep up with your current work without committing them to the working branch. This allows you to work between several branches without pushing any changes.

There are several cases in which you may need to stash your changes. Let’s say, for example, that you’re working on branch A. However, there’s a serious bug in the code of branch B that needs your immediate attention. You need to switch over to branch B to fix the bug, but you’re not ready to commit the work you’ve been doing in branch A.

Thanks to git stash, you can stash your changes in branch A without pushing them, switch over and fix the bug in branch B, and then switch back to branch A and pick up where you left off.

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.

This will show you both the staged and unstaged changes you’ve made in your branch. In our case, we’ve modified the “” 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 [email protected]. 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.

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!

RELATED: How to Delete a Branch on GitHub


Source link


Next Post

Kotlin 1.6.20 arrives with better Java class interop

Wed Apr 6 , 2022
[ad_1] JetBrains has released Kotlin 1.6.20, a new release of the Kotlin language that features better interoperability with generic Java classes as well as faster build times. Some features cited in the release are still in preview form. Improved interoperability with generic Java classes and interfaces was enabled through the […]
Kotlin 1.6.20 arrives with better Java class interop