Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
The course shows how to use Git, the popular open-source version control software, to manage changes to source code and text files. Using a step-by-step approach, author Kevin Skoglund presents the commands that enable efficient code management and reveals the fundamental concepts behind version control systems and the Git architecture. Discover how to track changes to files in a repository, review previous edits, and compare versions of a file; create branches to test new ideas without altering the main project; and merge those changes into the project if they work out. The course begins by demonstrating version control in a single-user, standalone context, before exploring how remote repositories allow users to collaborate on projects effectively.
We've been taking a look at how we can use git stash to save our changes temporarily. We saw we could put changes in there, and we saw how we could list them, and we saw how we could use show in order to inspect them. Now what we want to do is be able to retrieve those stash changes out of there, essentially do the opposite of git stash save. If we put them in the drawer before, now we want to get them back out of the drawer, and when we do that it's going to bring those changes back into our working directory, whatever that working directory is, whichever branch we're on. And now just as a refresher, remember when I did the stash before I was on the shorten_title branch, now I'm on the master branch.
Git doesn't care. For all it knows this is absolutely what you intended, which is stash the changes, switch branches and then bring them out again. So it doesn't matter which branch you're on, it's going to try to bring them into your working directory and apply those changes. Like with merges, there's the possibility that there may be conflicts that those changes don't apply cleanly, and in that case it works like merge does. It does its best to try and figure out how to merge the changes, but if not, then there will be conflicts, and it'll be up to you to resolve those conflicts.
Now there are two commands that we can use to pull items out of the stash. The first is going to be git stash pop and the other one is git stash apply, both of them will pull what's in the stash out, and put it in the working directory. The difference is that stash pop also removes it from the stash as well. Git stash apply leaves a copy there, so pop is going to pop it out so that it's no longer in there at all.
We'd just popped it out of the drawer and back into our working directory. It is exactly the opposite of git stash save. Git stash apply pulls it from the stash into the working directory but leaves a copy in the stash. The idea here is that it might be some change that we want to apply to where we are now, then we might want to switch to a different branch and apply it there, switch to another branch and apply it there as well. So we want to keep it in the stash as we move between each of those branches. Or it might be something that we want to apply then make a few more commits and then apply it again, and so on.
Most of the time, pop is what you're going to want to use, and that's what we're going to use here. After pop, I need to specify which stash item I want it to pull out. If we don't say then by default, it's going to pull the first one, which is this one. I'll just go ahead and write that in there. So if we don't say, it will pull the same thing. It'd be exactly the same as if we did stash @ sign, curly braces around a zero. If we had three of them, and we wanted to pop up the third one, well then we'd do git stash and then 2, 2 because the third one is actually number 2 because they start numbering at 0; 0, 1 and 2.
But in this case, I want to pop out that first one. So I'll pop it out, auto-merging mission.html, changes not staged for commit, that means it's in my working directory, and it dropped the stash right here. So now if we say git stash list, you'll see that it's not in there anymore. And if we go and look at that file, mission, you'll see that it has the Our Mission in it, which is the change that we made. Now we can re-stash this change if we want.
If we say, oops, wait! I didn't want to be on the mission branch, we can say git stash save, and then we can store "change to mission page title." Now it's saved again, git checkout, and we'll check out our shorten_title branch, okay. Git status, nothing in there, git stash, and this time just for the experience of it, let's do apply, and we won't specify which one, that will pull the top one out of there.
Just take a list just so you see it, there it is. And let's this time do apply, now it still does the auto-merge but this time when we look at our stash, you can see that it's still there. It did not take it out of there. For now, you can just leave the changes we made to mission.html in your working directory. We're not going to commit it or worry about right now. What we want to focus on next is how we can delete items that are in the stash. In particular, I want to see how we can delete this item that we left in there.
Find answers to the most frequently asked questions about Git Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.