git - merge conflict when local is deleted but file exists in remote

前端 未结 6 770
被撕碎了的回忆
被撕碎了的回忆 2020-11-28 04:58

I am very new to git and wondered how I should go about a merge where in the local repo I have deleted several files on the master branch but these files exist within the re

相关标签:
6条回答
  • 2020-11-28 05:14

    As stated in this useful answer to a related question, you can use git mergetool to start a dialog where you can select if you want to take the modified or the deleted version of the file(s).

    0 讨论(0)
  • 2020-11-28 05:26

    You should resolve the conflicts as you see fit. If the file really is supposed to be removed, and you will be publishing that change to origin, remove it again:

    git rm path/to/file
    

    If the file should in fact be tracked still, add it (the version in the work tree will be the version from origin):

    git add path/to/file
    

    After doing either of those to resolve the conflict, commit the merge.

    0 讨论(0)
  • 2020-11-28 05:29

    In Git GUI, you select the conflicted file and then right-click on the main text area where the conflicted text is shown.

    In the context menu that appears, you can choose to go with "Remote" or go with "Local". So if a file is remotely deleted, you can choose "Remote" to propagate the delete locally, and vice versa.

    Took me a month to figure it out...it would be nice if Git GUI actually had documentation...

    0 讨论(0)
  • 2020-11-28 05:32

    In EGit I also found problems. My solution was:

    • Used the Git Staging view.
    • Double clicked on each files shown on unstaged changes to open comparator
    • Click on the "Copy all from left to right" icon
    • Save file (it will disappear from the unstaged list)
    0 讨论(0)
  • 2020-11-28 05:33

    The best-rated answer focuses on the way to resolve the conflict.

    Before that, you probably want to know what the remote changed in the locally removed files.

    To do that, you can see the changes with:

    git diff --base
    

    From https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--1--base

    Compare the working tree with the "base" version [...]. The index contains these stages only for unmerged entries i.e. while resolving conflicts.

    0 讨论(0)
  • 2020-11-28 05:37

    As an added tip in addition to the accepted answer, in a "deleted by us", if you would like to see the changes that were made to the deleted file so that you may apply those changes elsewhere you can use:

    git diff ...origin/master -- path/to/file
    

    If it is a "deleted by them" scenario, and you would like to see the changes so you can apply them elsewhere, you can use:

    git diff origin/master... -- path/to/file
    
    0 讨论(0)
提交回复
热议问题