Git stashing - 14 days of Git
Tags: 14daysofgit, git, devops
Published: 2022-10-04 12:52:55

I'm continuing on my 14 days of Git learning journey, you can see a history of what I've learnt so far[ here](https://www.techielass.com/tag/14daysofgit/).  But today is all about the stash command and what it can be used for.

## **What is a git stash?**

The git stash command temporarily stashes (shelves) any changes you've made to your working branch so you can work on something.  When you are ready you can come back to the stashed changes and re-apply them later.

It's worth nothing that any stash is done only to your local Git repository copy, stashes are not transferred to the remote location.

### **Where is git stash saved?**

When you issue the _git stash_ command, they are stored inside a file called stash inside the .git/refs folder.

You can find all your stashes by using the command _git stash list_.

If you drop or clear any stashes it will remove it from the stash list, but you might still have some unpruned data within your local Git repository.  So that is something to be mindful of.

### **How do I use git stash?**

The sequence of events for using git stash are as follows

1. Save changes to branch 1
2. Run _git stash_
3. Check out branch 2
4. Work on the bug or feature in branch 2
5. Commit and push branch 2 changes to remote
6. Check out branch 1
7. Run _git stash pop_ to get your stashed changes back

### **How to create a stash**

Now we've looked at what stash does, where it stores information and the sequence of events when we are using.  Let's take a look at how to use the commands in detail.

If we have unsaved, uncommitted changes within a branch we are working on we can just type _git stash_.

![git stash command](https://community.ops.io/remoteimages/uploads/articles/3h2wzri7wjicx3kgx0d7.PNG)

This command will store or stash the uncommitted changes, either staged or unstaged files, and will overlook untracked or ignored files.

If you wanted to stash untracked files, then you could use the command:

_git stash -u _or _git stash --include-untracked_

![git stash -u command](https://community.ops.io/remoteimages/uploads/articles/2n51pvm8x8839wdumedz.PNG)


Alternatively, if you want to stash untracked files and ignored files you can use the command:

_git stash -a _or _git stash --all_

![git stash -a command](https://community.ops.io/remoteimages/uploads/articles/witlz.PNG)

If there are specific files you want to stash then you can use the command:

_git stash -p_ or _git stash --patch_


### **List git stashes**

You can use the command _git stash list_

![git stash list](https://community.ops.io/remoteimages/uploads/articles/exno4dwncryppuefr7nt.PNG)

### **Managing multiple stashes**

From the above list we can see we don't have much context as to what each of those stashes are.  It's recommended best practice when using stash to write a save message.  Your command would look like this:

When you list out your stashes you'll now have more information refer to.

![git stash list command](https://community.ops.io/remoteimages/uploads/articles/nci4sbyzulj5fyszfmnx.PNG)

### **Retrieve git stash changes**

Now you have something stashed away, how do you retrieve it? There are two commands that you could use, _git stash apply_ or _git stash pop._

Both of the commands reapply the changes that were stashed. There is a slight difference between what they do.

* Git stash apply reapplies the changes
* Git stash pop removes the changes from stash and reapplies them to the working copy

Git stash apply allows you to reapply the stashed changes more than once.  You can only use the Git stash pop command once.

### **Clear stash**

You can clear out a specific stash by using the command:

Or if you want to get rid of all stashes then you can use the command:

### **14 days of Git**

Today was really interesting, I love the fact that there is the stash feature.  I would love to hear from people if they are using this feature in the wild and how it works and if it's got the potential to cause some disasters in your environment?

Be sure to [subscribe](https://www.techielass.com/newsletter) and join me for that step in the learning journey!

You can follow along here:[ https://github.com/weeyin83/14d](https://github.com/weeyin83/14daysofgit)