Check out a free preview of the full Everything You'll Need to Know About Git course

The "Stashing" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen discusses a stack of temporary changes called "stash", and demonstrates how to push changes to the stash, list and show stashes, and pop stashes. He also provides a problem for the students to practice using stash by making changes to different files in the "upstream" and "remote" branches.

Preview
Close

Transcript from the "Stashing" Lesson

[00:00:00]
>> We're at this point where you have a change you've made locally and now you have a change in the polling from remote. What do you do? Typically, most people say, stash it. You can technically commit and rebase. And then you have your commit on top, but you've pulled in the changes that you need.

[00:00:16]
You do have options here, but there is a third option we should talk about later which is worktrees, which is my favorite way to do this. So but for now we're gonna actually start talking about stash. So for those that don't know anything about stash, stash takes every track change by git inside the index and the working tree.

[00:00:32]
And stores them kind of into a special area the work in progress area, this stash area, and you can think of stash like a stack of temporary changes. This is from the main page using git stash, when you record the current state of the working directory in the index but wants to go back to a clean working directory.

[00:00:50]
The command saves your local modifications away and reverts the working directory to match HEAD commit. All right, so pretty straightforward. Sash is a stack. Now I wanna make sure I say that multiple times, it's just a stack of changes. Right, so operation you can push to your stack by executing git stash.

[00:01:11]
You can even give a friendly message with -m. So this right here would stash, but with message. Stashes can be listed out with git list or git stash list and get show with an index as well. Show will just show the changes or the diff, whereas lists will list out the amount of stashes you have.

[00:01:32]
You can pop the latest stash with git stash pop. You can pop out an index with --index. So if you want the third stash in, you can just --index 3. Or I think it might be 0 base, I forget if it's 0 or 1 base. I assume everything's 0 base and let's for using Lua, so we'll just say --2 for the third option.

[00:01:50]
All right, man gets asked as your friend if you forget how it works again, use the friendly manual. Love the friendly rent manual read the friendly manual. That's what RTFM stands for, by the way. So if you ever see it on the internet, just remember it's the friendly manual.

[00:02:03]
All right, so problem. We're gonna use trunk and I want you to create an upstream change. Remember, this is gonna be in hello-git. Just create a file called upstream and put upstream change into it. Then, in remote, I want you to do a downstream change in the README.

[00:02:20]
Okay, so remember these are two different files and I want you to do like this. So I will literally copy this. I'm gonna go to hello-git. Just execute that git add this git commit, upstream Then I'm gonna do this one right here, so I just don't mess it up.

[00:02:43]
Look at that. All right, there we go. So now remote has a change. Dang it, get checkout trunk. There we go. Look at that. All right, there we go. Get this thing, and then add this thing, and then commit this thing. There we go. Okay, so make sure you do it to trunk, as I stated explicitly in the instructions, and would never accidentally deviate from said things.

[00:03:12]
All right, so now, actually did deviate, I said don't commit. By the way, the second one, don't commit the changes. Did anyone commit the changes in remote-git? You committed it, new git reset --soft HEAD~1. I'll do it too, git reset --soft HEAD~1. There we go. Now I have that lovely little change right here and trunk is now pointing one step back.

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