Use `git stash` to Save Local Changes While Pulling

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

If we make a change to the same function both locally and remotely, then when we try to pull down the remote changes, we'll run into a conflict - and git won't pull the remote changes down.

So first, we'll use git stash to stash the local changes, and then git pull to get the remote changes. To apply the stashed changed, we'll then use git stash pop

Since we changed the same function however, we'll get a merge conflict! So we'll fix the merge conflict, and then add and commit that change.

Finally, we'll drop the change from the stash with: git stash drop stash@{0}

Instructor: [00:00] We are in a feature branch here called JS changes. I want to change my "Hello, world" function to say, "Alert. Hi from local." I will save that. Now if I do a git status, I have unstaged changes.

[00:16] What happens if someone else changes that file? If we go over to GitHub here and go to our JS changes branch, we can check out the app.js file. This is the last version that was pushed. We can actually edit it right here on GitHub.

[00:32] Here I can say, "Alert. Hi from GitHub." Down here, I can commit that change. I can say, "Hi from GitHub change." I will commit directly to the JS changes branch. Now our app.js on GitHub says "Hi from GitHub."

[00:55] Let's go back to our code here, then, and try a git pull. What we get is that our local changes will be overwritten by the merge. We have to do something to prevent our local changes from being overwritten for now. It says here our two options are to stash them or commit them. We're going to show how to stash them here.

[01:15] If we do git stash, then it will take the code we just wrote and put it in a special stash. If we do git status, we can see we have no changes right here. If we do a git stash list, we can see our change was added over to the stash at zero.

[01:34] Now we can do a git pull. Here is our "Hello from GitHub" that we added. Now we have to git our stashed changes back. We can always do git --help stash to see how to get it back. The two ways we might do that are pop and apply.

[01:51] Pop and apply are similar, but pop will remove the change from the stash and applies the change from the stash but keeps it in the stash as well, in case you want to use it later. We're going to git stash pop. Here, we have another problem, which is a merge conflict.

[02:08] What happened is like we committed this into the stash. Now when we're popping it off, we get the same thing that we would if we committed it, which is a merge conflict. We have to fix it.

[02:18] The way to fix merge conflicts is just to manually go in and do it. You have to figure out what code you want to keep and what code you want to get rid of. In this case, we want both. We are going to remove the merge conflict lines. Now we have both lines, "Alert from GitHub" and "Alert from local." We can save that.

[02:37] If we do a git status, we see that we have both branches modified app.js. We have to add app.js, and then we can commit the "Hello from local" merged with GitHub. Then we can push that. Now the changes are both locally and on GitHub.

[02:59] There's one last thing, which is, if we do a git stash list right now, we still see our stash. We did git stash pop, and it didn't come off. That's because, if there's a merge conflict, the code stays in the stash even if you do a pop, in case you mess up the merge conflict somehow. We have to get rid of this stash manually if we want to do that.

[03:18] We can do git stash drop. Then we'll do the stash@{0and it dropped that stash. Now if we do a git stash list, it's not in the stash anymore, so that's cleaned up, and we still have it in our code over here.