mens0r
mens0r

Reputation: 25

How do I merge only existing files

I have a repository A with 30 files and added second repository B as a remote. This repository has 120 files, but the 30 files from repo A are also included here.

How do I merge B into A but only the existing (30) files ?

I tried to merge the whole branch from Repo B, but then I get the 30 files modified and the other 90 as new files.

I use Sourcetree client, maybe there's a functionality for this case.

Upvotes: 0

Views: 220

Answers (1)

Mark Adelsberger
Mark Adelsberger

Reputation: 45689

It sounds like either these repos have some common history, or you've already determined how to deal with that, so that your 30 files are merging correctly - and so you just need to keep the other 90 files from being added to the target branch...

There is a caveat here[1], but one way to do it:

git checkout repo_a_branch
git merge --no-commit repo_b_branch

The tentative merge result is now staged but not yet committed, so you can modify it. If the 90 files are easily identified by a path or glob that distinguishes them from the 30 files, you could

git rm path-or-glob-of-unwanted-files

and then commit.

If the files are all mixed up in a way that would make that difficult, you could do something like this

git status --porcelain |grep ^A |cut -d ' ' -f3 |xargs git rm -f

Once the unwanted files are removed, you can commit to complete the merge.


[1] If you have

     x -- x -- x <--(repo_b_branch)
...
     x -- x -- x <--(repo_a_branch)

and you find some solution to create

        x -- x -- x <--(repo_b_branch)
...                \
     x -- x -- x -- M <--(repo_a_branch)

where M still has only the 30 files, then M will be seem as deleting the 90 "other" files. So if in the future you try to merge from repo_a_branch into repo_b, you'll be dealing with those files getting taken out of the merge.

More generally, what you have here is sometimes called an "evil merge", in that it "hides" changes relative to the default merge result. Especially in cases where the default merge would've completed successfully, this can cause trouble down the road if you run certain commands that assume default merges.

This applies to the solution above, and also to any other solution that produces this commit graph while keeping only the 30 files in repo a.

Upvotes: 2

Related Questions