如何在 Git 中合并子目录?

是否可以只合并子目录从本地 Git 分支到远程 Git 分支的更改,还是“全部或全部”?

例如,我有:

branch-a
- content-1
- dir-1
- content-2

还有

branch-b
- content-1
- dir-1
- `content-2

我只想将 Branch-a dir-1的内容与 Branch-b dir-1的内容合并。

102461 次浏览

Just as an alternative to the SO question "How do you merge selective files with git-merge?", I just found this GitHub thread which could be more adapted for merging a whole subdirectory, based on git read-tree:

  • My repository => cookbooks
    My repository target directory => cookbooks/cassandra

  • Remote repository => infochimps
    Remote repository source I want merged into cookbooks/cassandra => infochimps/cookbooks/cassandra

Here are the commands I used to merge them

  • Add the repository and fetch it

    git remote add -f infochimps git://github.com/infochimps cluster_chef.git
    
  • Perform the merge

    git merge --allow-unrelated-histories -s ours --no-commit infochimps/master
    

(this performs a merge by using the 'ours' strategy (-s ours), which discards changes from the source branch.
This records the fact that infochimps/master has been merged, without actually modifying any file in the target branch)

  • Merge only infochimps/cookbooks/cassandra into cassandra

    git read-tree --prefix=cassandra/ -u infochimps/master:cookbooks/cassandra
    

This reads the tree for only the required source subdirectory i.e. cookbooks/cassandra, on the upstream branch of the source repository.

Note that the target subdirectory name should also be cookbooks/cassandra, or you would see:

fatal: Not a valid object name
  • Commit the change

    git commit -m 'merging in infochimps cassandra'
    

Addendum

It's bizarre,[edit me] — but the read-tree step can possibly fail like this:

error: Entry 'infochimps/cookbooks/cassandra/README' overlaps with 'cookbooks/cassandra/README'. Cannot bind.

... even when both files are identical. This might help:

git rm -r cassandra
git read-tree --prefix=cassandra/ -u infochimps/master:cookbooks/cassandra

But off course, verify manually that this does what you want.

I got this from a forum thread at Eclipse and it worked like a charm:

git checkout source-branch
git checkout target-branch <directories-or-files-you-do-**NOT**-want>
git commit
git checkout target-branch
git merge source-branch

Create a Git repository to contain both branch-a and branch-b:

git checkout branch-a
git diff branch-b dir-1 > a.diff
patch -R -p1 < a.diff

For my example, assume you have a branch 'source' and a branch 'destination' which both reflect upstream versions of themselves (or not, if local only) and are pulled to the latest code. Let's say I want the subdirectory in the repository called newFeature which only exists in the 'source' branch.

git checkout destination
git checkout source newFeature/
git commit -am "Merged the new feature from source to destination branch."
git pull --rebase
git push

It is significantly less convoluted than everything else I've seen and this worked perfectly for me, found here.

Note that this isn't a 'real merge', so you won't have the commit information about newFeature in the destination branch, just the modifications to the files in that subdirectory. But since you're presumably going to merge the entire branch back over later, or discard it, that might not be an issue.

Use git cherry-pick to select the commits you want and merge only these commits. The key trick here is to get these commits in an easy way (so that you don't have to figure them out by manually checking the Git log and entering them by hand). Here's how: use git log to print the commit's SHA-1 id, like this:

git log ^<commit-a> <commit-b> --pretty=format:"%h" --reverse -- <subdir>

'commit-a' is the commit immediately before the start point of the branch to merge, and 'commit-b' is the last commit on the branch to merge. '--reverse' prints these commits in reverse order for cherry-picking later.

Then do it like:

git cherry-pick $(git log ^<commit-a> <commit-b> --pretty=format:"%h" --reverse -- <subdir>)

It is two steps, simple and stable!

Given the OP's scenario where they have two branches, but want to merge only the history of dir-1 from branch-a into branch-b:

# Make sure you are in the branch with the changes you want
git checkout branch-a


# Split the desired folder into its own temporary branch
# This replays all commits, so it could take a while
git subtree split -P dir-1 -b temp-branch


# Enter the branch where you want to merge the desired changes into
git checkout branch-b


# Merge the changes from the temporary branch
git subtree merge -P dir-1 temp-branch


# Handle any conflicts
git mergetool


# Commit
git commit -am "Merged dir-1 changes from branch-a"


# Delete temp-branch
git branch -d temp-branch

Case 0: I didn't touch the files yet

git checkout origin/branch-with-the-code-you-want ./path/to/dir1

This gets you the folder as it is on the other branch as unstaged changes.

Case 1: Team has clean commits

If your team does clean commits, then digging around in the history for the commits could be fruitful. Use git cherry-pick COMMIT_HASH for those cases. You might want to git rebase -i HEAD~N where N is some number of commits to add them as pick COMMIT_HASH lines in the history if you need to.

Case 2: Clean commits are not important, but knowing the conflicts is

If you have all the commits all over the place then you likely won't need to keep the history, here's one approach that works for those cases. Note that this approach will not delete files or give you the history, but it will give you the conflicts between each file.

# Create an orphan branch with no history but your files
git checkout --orphan temp-branch


# Get the version of the files from the other branch
git checkout origin/branch-with-the-changes ./path/to/the/folder/you/want/to/merge
git commit -m "Commit that has all files like they are on your branch, except that one folder you want to merge"


# Merge the other file tree
git checkout your-branch
git merge temp-branch --allow-unrelated


# Clean up
git branch -D temp-branch

The easy workaround, merge and reset the undesired changes, keep what you want

  1. You have a repository with directory structre git/src/a, git/src/b, you only want to merge the directory b
  2. git merge origin/a-feature-branch
  3. backup the the b directory changes, for example mv b b-merged
  4. reset the branch by command git reset HEAD --hard
  5. override the b directory with b-merged, mv b b-origin; mv b-merged b;

This is how i would resolve it:

First find the last commit that both branches have in common, this is where the two branches start to diverge:

$ git merge-base branchA branchB
050dc022f3a65bdc78d97e2b1ac9b595a924c3f2

Then checkout a new branch from that commit:

git branch branchC 050dc022f3a65bdc78d97e2b1ac9b595a924c3f2

Then checkout the folder you want to merge from source branch into the new branch:

git checkout branchC
git checkout branchB ./path/to/the/folder/you/want/to/merge
git commit -m "update folder to merge from branchB"

Now branchC contains only changes from the folder you want to merge.

Finally merge those changes into target branch:

git checkout branchA
git merge branchC

Now you can handle any merge conflicts as you see fit.