Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Use `git stash` to Save Local Changes While Pulling

    Chris AchardChris Achard
    gitgit
    githubGitHub

    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}

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 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.

    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.

    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."

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.