From the course: Working with LinkedIn Learning Exercise Files on GitHub

Stash changes with Stash - GitHub Tutorial

From the course: Working with LinkedIn Learning Exercise Files on GitHub

Stash changes with Stash

- [Instructor] In this video and the next several videos, we'll look at what to do when you've made some modification to one of the files in the branch and you want to switch to another branch. In this video, we'll look at how to stash the changes that puts them in a temporary location in the repository and then you can retrieve them later. We'll also look at restore, which is another way of undoing things, undoing doing the changes. And the last option is committing. If you decide you do want to save the changes to the branch. Now of all these operations, stash is my favorite. So let's review what we have here. I've opened this robot dot CS file. I'm on branch four 11. And when I go over to four two, the main difference here is for our demonstration purpose is what's here on these two lines. The counter name is changed and the if statement is changed. So I'm going to go back to four 11 and those, those are different. So now I'm in four 11, I'm following along with the instructor, and I decide to make some changes to the file. So I make a change here and I save my changes, and now I want to go back to branch two. And I get this error. The following unsaved or uncommitted files are preventing the checkout operation, and it's telling me I can stash and continue, which will proceed without losing changes, and changes will be reapplied after the checkout. Or I can force the checkout to override all changes, in other words, undo. And we're going to do the stash, but I think the better way of stashing it is not to use this dialog, instead cancel out of here and then go to git and choose commit or stash. And at the bottom of this screen, you'll see all the stashes that are in the repository. There are no stashes at the moment. I'll add a stash by typing in the message here, change the text like that. Click on this dropdown. The first three items are for committing, the last two items are for stashing. I'm going to choose this first item, stash all. And as soon as I do that, you'll notice that on the bottom of the screen, it shows there's one stash available in the repository now. You'll also notice that it reverted this line of code back to the original. So now it's safe for me to switch to the other branch. Read some code in here, decide I've got what I need, and I want to go back to branch 11. So these two branches are in their original state. So now I decide that I want to see what I placed in the stash. So there's several operations we can choose. First thing we want to do is right click here. And I can choose to drop the stash, which means remove it. I decided I didn't need to save the changes at all. I can apply the changes, which will apply them, but it'll leave the stash there. So it'll still be there if I want to use it later. Or the example I'm going to use is popping the stash. So when I pop the stash, it's going to apply the changes, and then it'll remove it from the stash locations. So let's try that. Pop. Now the stash disappears. And if you look on line 22, my changed text is available. So as I said before, this is my favorite way of dealing with temporary changes. I think it's more elegant than the other two choices, and I recommend that you consider using this one.

Contents