Reputation: 69
I have a feature branch and a master branch and I want to merge the master branch to the feature branch. The problem is: the directory structures of the two branches are not the same.
The directory structure of the feature branch is like:
|── app1
│ ├── common
│ └── static
└── app2
The directory structure of the master branch:
|── app1
│── common
│── static
└── app2
Now in master branch I update folders 'common' and 'static'. Then, I'm going to merge master branch into feature branch to update the 'common' and 'static' folders under 'app1'. However, since the structures of the two branches are not the same, git will consider the two folders as new. How to do the merge to update the feature branch?
Upvotes: 2
Views: 2683
Reputation: 490068
The directory structure of the two branches is not particularly relevant, except as compared to the merge base commit.
Git does the merge by comparing the merge base commit to the two branch-tip commits. So your question is mis-framed: it really should be concerned with how the merge base commit compares to the two tip commits. Of course, if the two branch-tip commits have different directory layouts, at most one of them can match the directory layout of the merge base. But it's possible that neither matches that layout.
When Git performs the two diffs, from merge base to tip#1 and then from merge base to tip#2, it enables rename detection in both diffs. If that rename detection correctly finds all renamed files, Git will merge the files correctly. To whatever extent Git fails to correctly identify same-content-different-name files, you will have to merge the files manually.
You can assist Git somewhat in its rename detection using the -X find-renames=<n>
or -X rename-threshold=<n>
argument. This number n is the same "similarity index" argument you can pass to git diff
as -M
or --find-renames
. (The rename-threshold
name is the old name, changed relatively recently so that git merge
's option would match git diff
's.) Note that there is a limit on how many files Git will do rename detection for, as well. This limit defaults higher for git merge
(1000 now) than for git diff
(400 now). (These limits were smaller in older versions of Git, before 1.7.5, but all are configurable; see diff.renameLimit
and merge.renameLimit
in the git config
documentation. See also How do I merge changes in Git in files that I moved?)
Upvotes: 2