How do I merge a sub directory in Git?

前端 未结 6 908
太阳男子
太阳男子 2020-11-27 11:08

Is it possible to merge only the changes for a sub-directory from a local Git branch to a remote Git branch or is it "all or nothing"?

For example, I have:<

6条回答
  •  天命终不由人
    2020-11-27 11:47

    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
    

提交回复
热议问题