git merge "deleted by us"

42.9k Views Asked by At

I am doing a big merge. A lot of files have been removed from the repo in my branch, and in doing the merge I want to keep this change for all of those files. There are also some files that will need explicit merging and I'm intending to use git mergetool to merge them.

I wish to keep the "deleted by us" change (ie. the files should remain deleted) for all deleted files. Other merge conflicts I want to resolve myself.
Is there a way I can tell git to keep the deleted files deleted?

6

There are 6 best solutions below

1
On

I'll leave the question - because I'm sure there is a good answer - this is what I have done in the mean time:

  1. Do git status to see the list of deleted files (1082) and the number that had merge conflicts (3)
  2. In a text editor manually edit the 3 files that had merge conflicts, and then do git add on them
  3. Create a text file with a line with the letter "d" on each line and nothing else for each of the (1082) files that had been deleted (d.txt)
  4. run git mergetool < d.txt

Not elegant, but faster than pressing the letter "d" and enter 1082 times

0
On

You can resolve this by keeping the edited files by adding them back, and committing them once more:

git add .

or

git add -A

Then commit

git commit

If you want to resolve the confict by removing the files, you have to run git rm instead of git add.

See: Resolving a merge conflict from the command line

1
On

Here is a partial solution:

  1. Resolve all non deleted merge conflicts by hand, which you have to do anyway

  2. Type git diff --name-only --diff-filter=U to get a list of all remaining files in conflict. These files must be the ones you want deleted. Save the list of removed files as filesToRemove.txt

  3. Then do cat filesToRemove.txt | xargs git rm to remove all the files.

0
On

First, initiate the merge to get into a conflicted state.

Then try this:

git status | grep 'deleted by us' | sed 's/deleted by us: //' | xargs git rm

If you are curious as to how it works, you can build it up command-wise beforehand:

git status
git status | grep 'deleted by us'
git status | grep 'deleted by us' | sed 's/deleted by us: //'

See also the man pages for git status, grep, sed, and xargs.

0
On

I also observed

abc.txt: needs merge

when attempting

git rm abc.txt

after cherry-picking and seeing the file in "deleted by us" status.

I ended up with a resolved delete by doing the following:

git add abc.txt
rm abc.txt
git add abc.txt

this adds abc.txt to the staging area (essentially recreating the "deleted by us" file. (which resolves the conflict status)

then deletes the files from the file system

then adds to the stating area the fact that the file is now gone.

there are probably shell tricks to run these 3 commands on your set of 1000+ files without much headache.

there are probably better ways to handle this, but since git rm abc.txt did not work as we expected for our situation(s) i thought i would share an alternative set of commands that seem to have worked without using the mergetool.

3
On

One line fix:

git diff --name-only --diff-filter=U | xargs git rm

Explanation:

Resolve the conflicting modified files first and add them to the staging area, then use the diff-filter to delete remaining unmerged files. Here the documentation (man git-diff):

   --diff-filter=[(A|C|D|M|R|T|U|X|B)...[*]]

Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, ...) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected.

Also, these upper-case letters can be downcased to exclude. E.g. --diff-filter=ad excludes added and deleted paths.

Note that not all diffs can feature all types. For instance, diffs from the index to the working tree can never have Added entries (because the set of paths included in the diff is limited by what is in the index). Similarly, copied and renamed entries cannot appear if detection for those types is disabled.