Check out a free preview of the full Git In-depth course

The "Stashing" Lesson is part of the full, Git In-depth course featured in this preview video. Here's what you'd learn in this lesson:

Nina reviews stashing, which is saving work that is not committed to a git repo and is also safe from destructive operations. Nina takes questions from students.

Preview
Close

Transcript from the "Stashing" Lesson

[00:00:00]
>> Nina: There's one more place where it git stores code, that's through git stash. The git stash is a way to save uncommitted work. The stash is pretty safe from most disruptive git operations, and it's handy for so many things. For example, switching between branches while you're in the middle of work.

[00:00:24]
If you're using operations that can delete or overwrite your changes like git reset or git checkout, that might also be a good time to stage your changes. And I'm gonna talk about further destructive operations later in the class. So, we'll really deep dive into that section. Now, git stash, basic usage.

[00:00:52]
I'm sure that you're familiar with the first two, git stash, stash and git stash list, show what's in your stash. Now, when you git stash list, you will see a bunch of references that kind of look like this, stash@{0}. You can actually refer to that as a reference.

[00:01:19]
So, if I wanted to show the contents of my stash, I could do git stash show with that reference. If I wanted to apply my last stash, git stash apply, but I can also apply any stash, not just the last one by using that reference.
>> Nina: It's really useful when you are working with a stash older than just the last one.

[00:01:47]
Now, this is a feature that is really great. By default, git only stashes changes and files that are already tracked. That means that files that are either in the repository or in the staging area. I kind of find this really inconvenient cuz I may have made some new files, I don't want to get add them just yet, I need to switch to a new branch to a bug fix.

[00:02:16]
So I git stash, and the new file that I created is still in my working area, it's not associated with the stash. I come back to my work, I might have moved it. I might have accidentally committed that file to my critical bug fix, my working area is dirty.

[00:02:34]
So, I find that really inconvenient. You can include untracked files with stashed content, by using the --include-untracked flag. So, git stash --include-untracked. The next command used with caution, it'll stash all your files, even files present in your git ignore. So, if you're ignoring large binary files or something like that, git stash --all will grab them all.

[00:03:06]
>> Speaker 2: So then say you applied a stash that had untracked files in it. Once you applied it, are they still untracked, they're just back?
>> Nina: Yes.
>> Speaker 2: Okay.
>> Nina: They're still untracked. Yeah, this is one of my most useful, most used commands, and for some reason I find that not a lot of people know about it.

[00:03:34]
>> Nina: Now, some more advanced stashing operations. You can name your stash for easy references, if you just get stash it usually, all right, just takes the I think the message of the last commit. It's not relevant it's not useful when you go back to apply your stash you're like what is this even?

[00:03:56]
So, you can name your stashes for easy reference with git stash save and the name. You can also start a new branch from a stash by saying git stash branch
>> Nina: And,
>> Nina: Sorry, that should be,
>> Nina: Optional branch name, not optional stash name, I believe. I'll have to double-check that.

[00:04:38]
>> Nina: I'll get back to that in a few minutes. Another really useful thing that we can do, is grabbing a single file from the stash. Maybe you're working on something, you wanna check it out, you don't wanna apply the whole stash again. You can do with git checkout stash name -- filename.

[00:05:02]
But note, if that you have changes to that same file in your working area or your staging area, running this command will overwrite it from the copy that's in the stash. So, use with caution.
>> Nina: A little bit more about advanced stashing. I find it really useful to clean the stash.

[00:05:27]
Sometimes when I'm working on a long running project, my stash is like 40 things long. Contains things from months ago. I'm like, what is this stuff? I don't need it. Some things that we can do to keep our stash cleaner, we can use git stash pop. And that's gonna remove the last stash and apply those changes, unless there's a merge conflict.

[00:05:53]
If a merge conflict occurs when unstashing, git is gonna keep that stash.
>> Nina: We can also git stash drop to remove the last stash. So if we git stash apply, everything looks good, we can follow up with a git stash drop. We can also parse a stash reference to get stash drop, or we can remove all stashes with get stash clear, if we know we don't need them and we're not gonna use them.

[00:06:26]
If we want to look in our stash, we can do that with two operations. Git show, given a stash reference, will show the files that were in the stash, and you can get that reference with git stash list. So, I'll keep those pretty brief considering that most of you are already pretty familiar with stashing.

[00:06:59]
The two advanced tips that I use most in my daily work flow is like I said before, keeping my untracked files along with stash. I don't have to mix my work in progress changes with my staging changes when stashing. I also tend to use naming my stashes. Helps me keep track what I was working on, what I was doing when I get back to that stash, it provides a lot of context.

[00:07:27]
There is one more type of stashing that occasionally use it's kind of advanced so it's not the side but I will mention it, just like you can git add -p to add parts of files to your stage, you can also get stash-p. And that will let you selectively stash changes.

[00:07:53]
>> Speaker 2: I noticed in your stash list on another slide that you had some of the names of the branches they came from in there.
>> Nina: Yes.
>> Speaker 2: Do you find it hard with stashes to keep track of that? Cuz you don't know which branch it came from right?

[00:08:08]
Unless you write it in the name of the stash?
>> Nina: It should say.
>> Speaker 2: It puts it in there?
>> Nina: I'm pretty sure, yeah.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now