Search code examples
git

How to force git merge to only add one folder


I have branchA and branchB with the different folders in each branch. How do I merge branchB into branchA and keep the folder from both branches?

 ...----O----O----O----O----O----O----O----O=branchA
        |_folder1      |                  /|_folder1
        |_folder2      |                 / |_folder2
                       |                /  |_folder3
                       |               / 
                       `----O--------O=branchB
                            |_folder3

Solution

  • Disclaimer: as j6t rightfully noted in comments above, this is not the intended way to use branches, since each one should represent some state of the entire project, not a part of it. The following is just a workaround to achieve what's asked, but you probably should reconsider your branching strategy.

    That being said, you can bring your folder3's changes in your branchA branch without merging thanks to git restore (for older versions, see git checkout):

    # be sure to be on your branchA, with a clean working tree, then
    git restore --source=branchB -- folder3/
    git add folder3
    git commit "Your message"