The scenario:
- Import
repoB
into a sub-directory inrepoA
, whilerepoB
is imported- completely
- into a sub-directory in
repoA
- preserving history for the imported part only
- Optionally update imported code base from
repoB
- Optionally update
repoB
with amended code base from dedicated sub-directory inrepoA
This guide doesn't cover git submodules toolset.
Author recommends to employ git-subrepo
toolset.
See also Resources and Credits section below.
Install ingydotnet/git-subrepo@github / 2017-02-06
- Import from
repoB
:git subrepo clone repoB.git localSubdir4repoB -b master
- Update
repoA
fromrepoB
:git subrepo pull localSubdir4repoB
- Export back to
repoB
:git subrepo push localSubdir4repoB -b repoB-target-branch
Import and update commands squash imported commits.
It is recommended not to export to repoB/master
as repoB
owner may
want to review code before merging into master
.
Employ Pull Request mechanics to encourage updates get merged.
The actions above were tested on emulation project
Refer to README.md
files from the repos and
OleksiyRudenko/subrepo-host/README.md#summary@github
in particular for details.
Useful extras:
- Check
subrepoB
history:git log refs/subrepo/localSubdir4repoB/fetch
- Create branch with local
subrepoB
commits:git subrepo branch localSubdir4repoB
- Add
subrepoB
branch to current history as a single commit:git subrepo commit localSubdir4repoB
Refer to ingydotnet/git-subrepo@github / 2017-02-06 for further details.
The implemenation relies upon git-subtree
script available with git
since release 1.7.11.
For partial import see e.g.
- Merge a subdirectory of another repository with git / 2013-08-23
- git/git/contrib/subtree/git-subtree@github / 2009..2015
git remote add -f upstream-repoB repoB.git
git subtree add --prefix=localSubdir4repoB upstream-repoB repoB-branch
repoB-branch
would normally be master
as we may want working code.
Assuming upstream-repoB
is set.
git fetch upstream-repoB
git subtree pull --prefix=localSubdir4repoB upstream-repoB repoB-branch
repoB-branch
would normally be master
as we may want working code.
Assuming upstream-repoB
is set and repoA
updated from repoB
.
git subtree push --prefix=localSubdir4repoB/ upstream-repoB repoB-branch
repoB-branch
would normally differ from master
as
repoB
owner may want to review code before merging it to master
.
git subtree add --prefix=localSubdir4repoB upstream-repoB master --squash
- Merge a subdirectory of another repository with git / 2013-08-23
- Git Tools - Subtree Merging
- ingydotnet/git-subrepo@github / 2017-02-06
- Git subtree: the alternative to Git submodule / 2016-05-16
- How to import existing Git repository into another? / 2009..2017
- Using Git subtrees for repository separation / 2013-02-16
- About Git subtree merges
- git/git/contrib/subtree/git-subtree@github / 2009..2015
- The coolest merge EVER! by Linus Torvals / 2005-06-23