Aerodynamika
Aerodynamika

Reputation: 8423

How to merge a branch from another repo and exclude some files?

Suppose I have a branch from another repo (yyy), which I want to merge into the current one (xxx).

But I want to exclude some files, like config.js, options.js and views/home.ejs, so that they stay as they were in my current repo xxx even if they have been changed in yyy.

How do I do that?

I figured out how I could merge the branch from another repo (below), but what should I add to keep these 3 files intact?

git remote add yyy [email protected]:yyy/yyy
git remote update
git checkout -b newbranch
git merge --allow-unrelated-histories yyy/master

Upvotes: 0

Views: 300

Answers (2)

matt
matt

Reputation: 535954

The advice in the other answer may work, but I would advise that you don't do it. These instructions will arguably create an "evil merge". This makes the true history impossible to trace. Never introduce external alterations into a merge commit. The merge commit should consist entirely of the results of the merge. (That could involve resolving a merge conflict, but then you must be even more careful to do nothing other than resolve the conflict.)

Instead, merge, then restore the overwritten individual files and make a new commit, to make explicitly clear what happened. (And use git restore, not git checkout.)

Upvotes: 1

glemco
glemco

Reputation: 36

You can merge and then restore the files from the previous commit (from your xxx repo), and commit again with the --amend option (edit the previous commit, which should be the merge).

#all stuff before
git merge --allow-unrelated-histories yyy/master
#solve all conflicts and be sure the merge is successful (appears in the log)
git log #find the commit hash before the merge, where your files where as you want
#if you cannot find it, try xxx/master, it should be the last upstream
git checkout <that hash> -- yourfile1 yourfile2 yourfile3
git commit --amend

Beware of spaces in the filenames, in case your files have spaces in their names, either quote them or escape them with a \ (if you use tab completion, this should happen automatically)

Upvotes: 0

Related Questions