FREELessons: 24Length: 3.1 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

6.2 Stashing Changes

Sometimes, you need to stow away a few changes while you focus on something more important. Git’s stash allows you to do this, so you don’t have to worry about accidentally committing something.

6.2 Stashing Changes

So let's say that you're working on a new feature for your project. When you're alerted about a serious bug that you need to fix right away. After you've actually made the bug fix and you're ready to commit it, you run into a problem. And that's this, you're already part way through working on the feature and when you come to staging the bug fix, you don't wanna accidentally stage part of a feature. Of course, you can just selectfully choose the right files, but there's actually a little bit of an easier way to do this and a way that gives you a lot more flexibility. And that is using the git stash command. If I run the git status command, you can see that we actually have modified our README file, we haven't staged the changes yet but we've made some modifications. Now assume this is part of a feature that we've created. We're not ready to commit it yet, but we need to go make a bug fix. What we're gonna do is run git stash, and what this is going to do, is take the changes that we've made, and it's gonna put them to the side, and just stash them for us, while we make some changes to other files. So if I run git stash, you can see that we have this message here, we've saved working directory in index state, working progress on master. And it says the latest commit was made a change in README.markdown, and the head is now at this specific commit. We have taken the changes that we've made, we've stashed them aside, so the latest commit is now this commit and the working directory is now clean. And if I run and get status, you can see that the working directory is clean. We have nothing to commit. This means that those changes we've made aren't really visible anymore. So now I can go ahead, open up fix.txt and to do something like this. Now I'll save and close that. Then if I run git status, notice that the only file that's been modified is the fix.txt file. If I run git add dot and then run git status again, you can see that the only file that has been staged is the fix.txt file. Even though our read me file has changes that have been made. We stashed those changes, so git is currently ignoring them. All right, so let's go ahead and commit this. And now that I've done that, if I run git status, you can see that our working directory is once again clean. So now we wanna bring that that stash back. However, first let's actually look at the stash. So if I run git stash list, it will give me a list of all the little bits that we've stashed. So we've stashed that one bit there, but what we could actually do is stash something else. So if I go into our todo file and I wanna stash the removing of these last three todo items, you can see if I run git status, that our todo file has been modified. If I run git stash, I can actually, let me stash it a bit differently this time. I'm gonna include the save command and I'm gonna pass it a message. And this is almost like a commit message, except we're making a bit of a temporary commit. You can think of it that way. We're stashing those changes off to the side. So now, If I clear the screen and I run git stash list again, you can see that we actually have two stashes that have been made. The latest one, which has the stash number of zero, is removed last three todo items, and the one before that is the one that we made earlier. So as you can see, we have these multiple stashes here. So now we wanna pull one of these stashes back onto our working directory, so we can apply those changes. Well, we can do this in a couple of ways. First, we could do git stash, and then we could just run apply. And what git stash apply will do, if we don't pass it a reference to a specific stash, it's just gonna take the latest stash, stash zero, and it's gonna apply those changes to the working directory. If we wanted to apply a specific stash, I could actually copy this part right here, this stash at sign curly brace number, and if I copied that and pasted it there, then that's the stash that would be applied. Now let me make it clear that the apply command leaves the stash on the stash stack, after we have applied the stash. If you want to apply this stash and get rid of it from the stash stack, you're going to use pop. So that will take the last stash off and apply it to the working directory. I'll show you how this works. If I run git stash apply, you can see that those changes are now showing up again in modified to do item, but if I run git stash list, you can see that that stuff is still on our stash list. Let me go ahead and commit that with git commit-am,now if I run git status, you can see that our working directory is clean. And now if I run git stash list, you can see, as I said before, it's still there. Now if I instead, I run git stash pop, this will pop our chosen stash off of the stash deck. And we'll apply it to our working directories. So I'm gonna choose to pop stash one, instead of just the default stat 0 and if I do that, you can see here that the README file modifications have back to their working directory, but notice these measurements down at the bottom, drop stash one. So drop is the equivalent of removing the stash from the stash stack. So if you think about it then, doing a stash pop is the same thing as doing a stash apply and then a stash drop. So pop just commands the application of it and the dropping of the stash. So now, if I run git stash list, you can see that the only thing left there is the first stash that we applied, instead of pop. So now if I do git stash drop, it's gonna drop that stash, and now if I do git stash list, you can see that we have no stashes left. So I'll go ahead and commit those changes that we made. And now our working directory is clean and our stash stack is empty, and you have learned how to use the git stash command.

Back to the top