Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to git pull while ignoring the local changes?

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.

like image 813
M.A.B Avatar asked Jul 03 '17 14:07

M.A.B


2 Answers

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.
like image 92
LeGEC Avatar answered Sep 22 '22 22:09

LeGEC


If you pull new code from server it will overwrite your local code.

If your changes are important you must commit them and then pull from server. And them you will be able to compare the changes and merge those properly.

do : git commit -m "Any message"then git pull And then ONLY IF NEEDED git will ask you for solving merge conflicts.

I strongly advice you to use any GUI Based git manager. It will simplify a lot your work.

If your changes are so minor that it does not deserve a commit, you can put your local changes in a temporary place and then bring it back. do the fallowing.:

  1. stash your changes git stash . I will put all your local changes in a Temp place.

  2. pull from server git pull. All your changes will be overwrite.

  3. bring back your local changes from stash git stash pop. It will bring the local changes back.

like image 27
Daniel Santos Avatar answered Sep 23 '22 22:09

Daniel Santos