How to store changes in Git

Git logo on blue background

Need to switch branches, but not ready to commit the changes you’ve made to your current branch? You can store your changes and come back to them later. This is possible in Git, whether you use GitHub or another hosting service.

Why store your changes?

Storing your changes is a great way to keep track of your work in progress without committing them to the working branch. This allows you to work across multiple branches without pushing any changes.

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

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

How to store changes

You can store your changes by running a simple command. Before you do that, however, you can run a different command to see exactly what you’re going to store. In your working branch, run this command:

git status

Run the git status command.

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

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

git stash

Run the git stash command.

Once executed, you will then receive a message stating that your changes have been stored on . Your branch will now look like it did before you made your changes, and you can now safely switch to a new branch.

View stored changes

If you have saved multiple caches, you may want to see a list of caches before attempting to retrieve one. When you view a list of your stashes, note the name of the stash you want to recover and keep working.

In the terminal, run this command:

git stash list

Run the git stash list command.

A list of caches will then be returned. In the example above, our reserve name is stash@0. The number inside the braces is the index. If you have multiple caches on the same branch, the number will be different.

An example of reserve 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 display the results in diff format.

Retrieve stored changes

Once you’re ready to pick up where you left off, you’ll need to retrieve your stored changes. You can do this in two different ways. A command will keep a copy of your changes in the stash while copying it to your working branch. The other will copy everything to your working branch, but delete everything from the stash.

To keep a copy of your changes in stash and push them to your working branch as well, run:

git stash apply

run the git stash apply command.

To make the changes to your working branch but remove the copy from stash, run:

git stash pop

Run the git stash pop command.

If there are multiple caches on a single branch, just add the cache name 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 merged it with the master branch, don’t forget to delete the branch to keep your repository clean!

RELATED: How to delete a branch on GitHub


Leave a Reply

Your email address will not be published.

Back to top