How to git pull while ignoring the local changes?

M.A.B picture M.A.B · Jul 3, 2017 · Viewed 9.2k times · Source

As I am trying to pull some changes from origin/master, I am getting this error :

error: Your local changes to the following files would be overwritten by merge

because I made local changes. I don't want to commit these changes, so I suppose I should use git stash on my branch, then git pull origin master

My question is :

  1. Will the pull simply ignore the changes and merge correctly
  2. Can I still use my local changes after the pull + the ones merged without any additional step ?

Thank you !

PS : yes, I googled, I just want to make sure I understood correctly.

Answer

LeGEC picture LeGEC · Jul 3, 2017

Here is how your repo evolves when all is applied :

  1. Before you pull, with your local modifications :

     --*--*--*--*--*--*--A--B (<- some changes you did not commit yet)
           \             ^
            \            master: your latest local commit on master
             \
              *--*--C origin/master: the latest commit on the remote repo
    
  2. after git stash :

                           B <- stash: git stored local changes in its stash
                          /         
     --*--*--*--*--*--*--A <- master: the files on your disk have been restored
           \                          to their state in "master"
            \            
             \
              *--*--C origin/master: the latest commit on the remote repo
    
  3. after git pull :

                           B <- stash
                          /         
     --*--*--*--*--*--*--A--D <- master: merged remote branch in local branch
           \               /
            \             /   
             \           /
              *--*--C---/
                    ^ origin/master
    

git applies the action it usually does when pulling: merge the remote changes in your local branch.

  1. after git stash apply :

                           B <- stash
                          /         
     --*--*--*--*--*--*--A--D--B' <- modifications stored in B (not committed yet)
           \               /
            \             /   
             \           /
              *--*--C----
                    ^ origin/master
    

git re-applies the modifications stored in the stash on top of the new leading commit


The caveats are :

  • step 3. (git pull) may trigger some conflicts, which you will need to solve before actually reaching the state D in the diagram
  • step 4. (git stash apply) may also trigger some conflicts, if your local changes (the ones stashed in B) actually interfere with some modification from origin/master.
    You will need to solve these before reaching B' in the diagram.