0

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 git@github.com:yyy/yyy
git remote update
git checkout -b newbranch
git merge --allow-unrelated-histories yyy/master
Aerodynamika
  • 7,883
  • 16
  • 78
  • 137

2 Answers2

1

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • If you ever want to push your changes back, I would suggest you don't get yourself in the situation to start with and don't add changes to your own repo that you would not want to push back. I know that is easier said than done sometimes, but it is worth considering if you should be saving such changes. – johnfo Jul 21 '21 at 08:04
  • @matt could you please explain how I can do that sequence of steps: merge, restore, and make a new commit? I'm not so proficient in Git, so would be great to better understand what I need to do to avoid any errors. Thanks! – Aerodynamika Jul 21 '21 at 09:54
  • Also, is this maybe just better to consider one repo as an original another one as a fork? In that case, does the procedure change? – Aerodynamika Jul 21 '21 at 11:33
0

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)

glemco
  • 36
  • 4