git merge in wrong folder

471 views Asked by At

I have mainProject on git, with a shared library. This shared library is also in subProject.

my workflow

I start my project by cloning the project and create a branch :

git clone https://github.com/user/mainProject.git main-project
git checkout -b fix

I worked on my mainProject, and added a awesomeClass.php in the folder lib/ (the root of the shared library)

The new file structure:

/index.php 
/page1.php
/lib/form/contact.php
/lib/awesomeClass.php*

*=new

after this I add and commit my changes.

git add lib/awesomeClass.php
git commit -m "added new awesome class that will blow your mind"

Add the remote location of the shared library, fetch it and connect remote to local branch

git remote add sharedlib https://github.com/user/sharedlib.git
git fetch sharedlib
git checkout -b sharedlib sharedlib/master

this switches my branch to sharedlib where I try to merge my changes to, to get the same changes of the shared library to my subProject.

 git merge --squash -s subtree --no-commit fix

This is where it goes wrong, The file structure was(seen from sharedlib branch perspective):

/lib/form/contact.php
/lib/awesomeClass.php

after merge :

/lib/form/index.php*
/lib/form/page1.php*
/lib/form/lib/form/contact.php*
/lib/form/lib/awesomeClass.php*

/lib/form/contact.php
/lib/awesomeClass.php

*=new

Can anyone explain how I can merge only the shared library and not the full mainProject into a wrong folder? Thanks!

1

There are 1 answers

2
johnb003 On

There is some complex workflow you're following regarding the subtree merge strategy but this is out-dated and it's much simpler these days.

What you're looking for is the contrib command git-subtree, which comes complete with a split feature to extract the changes from the main project specifically relevant to that sub-directory so that you can merge them back upstream to the subproject.