开发者

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

开发者 https://www.devze.com 2023-01-27 22:10 出处:网络
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 remote master branch.

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 remote master branch.

After doing git-merge it shows the conflicts tha开发者_开发技巧t have occured.

Using git gui it shows that the local file is deleted, while the remote branch file has contents.

How do you stop these files from being conflicted? Is there a simple way using git gui?

Many thanks


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.


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


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


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.


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)


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

精彩评论

暂无评论...
验证码 换一张
取 消