Resolving Git merge conflicts

前端 未结 4 1940
长发绾君心
长发绾君心 2020-12-13 13:55

A Git repository has been cloned on several developers\' local machines. Some changes have been made to the code in the repository. We\'re now getting the error:

<         


        
相关标签:
4条回答
  • 2020-12-13 14:44

    It's good practice to always commit any local changes before pulling (merging) new code. If you don't commit, then Git doesn't know how you want to manage your local changes. Merge only with a clean working tree.

    There may be conflicts in the merge, due to the same files being changed locally and by somebody else. In my experience, resolving conflicts from an actual merge operation is slightly simpler than resolving the same conflict from a stash pop operation.

    0 讨论(0)
  • 2020-12-13 14:45

    git stash is perfectly legitimate, though as Greg said, for some reason fixing the conflicts can get strange. But they are still fixable, you won't actually fubar anything. The command as I know to re-apply the stash is git stash apply, though pop may be an alternative that I'm not aware of (or it could do something different, I don't know, so you probably want to use apply.)

    Is there a reason you don't want to commit those changes before merging? Generally that's the right thing to do.

    Another option is:

    git stash
    git checkout -b newwork
    git stash apply
    git commit ...
    

    This creates a new branch, which will allow you to get your master up to date without conflicts, (checkout master again, then pull or fetch + merge). Then you can merge your branch back with (while still on master) git merge newwork. You can resolve the conflicts on master, while still retaining the work on newwork without any conflicts. This is a bit safer if you are worried about conflicts really screwing things up, but generally, conflicts are just part of the process, so don't worry too much about them.

    0 讨论(0)
  • 2020-12-13 14:48

    I have another solution:

    git reset --hard FETCH_HEAD
    

    It works in almost cases.

    0 讨论(0)
  • 2020-12-13 14:52

    First you should:

    git checkout -- public_html/sites/file
    git checkout -- public_html/sites/file1.txt
    git checkout -- public_html/sites/file2.txt
    

    Next step:

    git pull origin master
    
    0 讨论(0)
提交回复
热议问题