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] The scenario for this video is I'm on this 04-02 branch and I'd like to switch to another branch in the chapter but I have modified the contents of one of the files while I was working on the code and that's going to prevent me from switching to the other branch. So first of all, let's go and look at the code file. It's this file Robot.cs and I'll make a change to line 22. I'll change the tab Buffer string to an asterisk, like that and then I'll save the changes. Now let's switch back to the terminal. And over here, I'll attempt to check out the other branch. And I get an error, Your local changes to the following files would be overwritten by checkout. It's telling me that changes are in Robot.cs and then it's telling me there's two choices, commit your changes or stash them before you switch branches. In this video, we'll look at the stash command and later we'll look at the commit command. I can also verify this another way by calling git status. That says, that there's one modified file. And it's telling me here that the changes are not staged for commit. Now, if I was working on a normal software project and I wanted to persist these changes, so I can merge them later, I would stage the files and then I would commit the files but that's not what we're doing here, we're working in some training videos and some training code, and I just want to switch branches and I've made some changes and I just want to stash them for later. So I'll clear the screen and then I'll type in, git stash, it's that simple. It tells me it saved my working directory and index state on 04-02. And there's extra information here. Now let's go back to that text file, that code file and over here in notepad++, it tells me that that file has been modified. So I click on Yes, and I see that it returns to the original state. So let's review what happened. I made some changes, I stashed the changes that reverted all the files in that branch to their original state. Now that I've done that, I can successfully switch branches. Now I will clear the screen and then I will use the up arrow to try this checkout again. Now I switched to the other branch. I work in this branch for a while, or look at the code in here and then I want to switch back to branch 02, I was successful at that. And now I want to get the items out of the stash. That's called popping the stash. So I will do git stash pop and do you notice that it says, this is the same message we saw earlier. Your branch is up to date, there are some changes that are not staged for commit, it's Robot.cs, and if I call git status again, you see that same message. So when I popped the stash, it ran get status for me. So I can see what has changed when I pull the items out of the stash. Now let's go back to the code file. And once again, it's telling me that the file has been modified, I want to click on Yes, I see the change. So let's review what we saw. I had made some changes, I couldn't switch branches, so I stashed the changes, moved around through branches in the repository. And then at some point when I want to retrieve my old code, that I stashed, I called stash.pop and it switches all the files that are in the branch to use whatever items I stash. Now, in this example, I only stashed the changes for a single file, but this works if you have made multiple changes in the branch.

Contents