Saving Changes with Git Stash – CraftQuest

In the Up and Runningn with Git course, I was in the middle of making some changes to the homepage of our sample site when another change request came in. I needed to quickly save, or save, my changes and then reapply them to the repository later, after my other work was complete.

The easiest way to do this is with git-stash, a useful git command that allows you to store the modifications you’ve made in our working directory and return to a clean working directory.

From the Git Manual (run git stash -help to view it on your own):

Use git stash when you want to record the current state of the working directory and index, but want to return to a clean working directory. The command saves the local modifications and rolls back the working directory to match the HEAD commit.

Git stash isn’t a replacement for clever branch usage (we don’t want to start a new feature on a parent branch and then save it until you’re ready to apply it), but it can be a lifesaver if we find ourselves needing to quickly shift gears to another task, bug report, or request.

Start an existing project to test git stash.

First, let’s check if we have any existing hiding places.

List of git caches

The list option displays all existing caches, if any. If you have some, they will appear like this

: [email protected]{0}: In development: testing git stash [email protected]{1}: WIP in master: 4fd1101 fixing the design in the product list of the home page [email protected]{2}: In development: Product package download template Each stash

is listed on a separate line, starting with the stash name consisting of the word “stash” and an index (starting with zero) enclosed in braces. The last stash is the one at the top, with the index of zero (confusing, right?).

Let’s add a stash. To do that we use the stash save command.

stash save “updated offline file”

The last two parts of that stash command are optional. We can simply do:

stash instead

and it will still create a stash with any changes to the working directory, but without a custom message. The save option is automatically assumed if it is not included.

Here is

the list of stash now: [email protected]{0}: In development: updated the offline file [email protected]{1}: In development: testing git stash [email protected]{2}: WIP in master: 4fd1101 fixing the design in the product list of the homepage [email protected]{3}: In development: Product package download template

To apply a stash to the working directory, We have a couple of options. The first is:

git stash pop

which will remove the most recent stash from the list and apply it to the current working directory. It’s just reverting what you did when you saved the stash (but keeping subsequent changes in the repository intact).

You can also be specific and pop any

stash on the list: git stash pop [email protected]{1}

We should see something like this at the end of the output

😀 rop [email protected]{0} (38f88c1479dc8a3c63f794feed7cd276ae3c6c7e)

The other option when applying

a stash is to use:git stash apply

As with pop, we can choose not to specify a stash and it will use the last one. Or, we can specify one, like this:

git stash apply [email protected]{1}

With apply, we will get a similar output when applying the stash, but without the message that the stash was deleted. Why? Because git stash applies the stash we specify, but does not remove it from the list of storages.

If we run

:git stash list

we get the complete list of hiding places, including the one we just applied

. [email protected] {0}: In development: Updated offline file [email protected]{1}: In development: testing git stash [email protected]{2}: WIP in master: 4fd1101 fixing the design in the product list of the homepage [email protected]{3}: In development: Product package download template

These are the basic commands when using day-to-day caches. There’s more, and I encourage you to run:

git stash -help

to see all the commands.

Contact US