Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set separate remote for pushing and pulling only subfolder

Tags:

git

Let's say I have two repos, repo A and repo B, which contains a folder with code similar to the code in repo A (doesn't really matter how this actually has happened, but OK, let's assume I've just copied contents from A).

Now I want following:

  • To work just as I got used to in repo A
  • To set and additional remote repo pointing to repo B, but pushing and pulling only this subfolder.
  • To push, when it needed to this repo B.

So, basically, I'm looking for the cheapest way to have something like reversed sparsed check out.

like image 635
shabunc Avatar asked Mar 23 '23 13:03

shabunc


2 Answers

It seems exactly the kind of problem git-subtree1 tries to solve.

cd repoB
git subtree pull -P folder {repoA URL} {repoA branch}
... edit test commit... 
git subtree push -P folder {repoA URL} {repoA branch}
like image 155
milton Avatar answered Mar 25 '23 02:03

milton


You can add a ref for and so publish anything in a repo. So, to maintain a separate branch that tracks the main branch's subfolder, add this to your commit ritual:

# in your commit ritual (no need to be mainbranch-specific, 
# this will detect changes and add the tracking commits only as necessary),
if [ `git rev-parse mainbranch:path/to/subfolder` \
      != `git rev-parse subfolderbranch:path/to/subfolder` ]; then 
    git commit-tree -p subfolderbranch -p mainbranch \
              mainbranch:path/to/subfolder <<<"tracking mainbranch subfolder" \
    | xargs git update-ref -m"tracking mainbranch subfolder" refs/heads/subfolderbranch 
fi
like image 43
jthill Avatar answered Mar 25 '23 04:03

jthill