Search code examples
gitmergebranchgit-mergegit-branch

Merge from trunk to branch but only files in the branch


I have branched off trunk to a feature branch. In order to keep my branch up to date, I would like to frequently merge back from trunk (always up to date), but I don't like everything in trunk getting merged into my branch.

Is it possible to somehow merge from trunk only the files I have in the branch?

E.g, before merge:

Trunk
  File 1
  File 2
  File 3
  File 4
Branch
  File 2
  File 3

After merge, I have the following, which I don't want. I want to only merge File 2 and File 3 from trunk.

Branch
  File 1
  File 2
  File 3
  File 4

Solution

  • git merge --no-commit --no-ff trunk
    git diff --name-only -z --diff-filter=A @ | xargs -0 git rm -f
    git commit