Skip to content

Instantly share code, notes, and snippets.

@OleksiyRudenko
Last active October 30, 2018 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OleksiyRudenko/86d378b56fe690e47a066b8eeb4ac5b2 to your computer and use it in GitHub Desktop.
Save OleksiyRudenko/86d378b56fe690e47a066b8eeb4ac5b2 to your computer and use it in GitHub Desktop.
Repo import and export back

Repo import and export back

The scenario:

  1. Import repoB into a sub-directory in repoA, while repoB is imported
    • completely
    • into a sub-directory in repoA
    • preserving history for the imported part only
  2. Optionally update imported code base from repoB
  3. Optionally update repoB with amended code base from dedicated sub-directory in repoA

This guide doesn't cover git submodules toolset.

Author recommends to employ git-subrepo toolset.

See also Resources and Credits section below.

Action scripts

Using git-subrepo

Install ingydotnet/git-subrepo@github / 2017-02-06

  • Import from repoB:
    • git subrepo clone repoB.git localSubdir4repoB -b master
  • Update repoA from repoB:
    • 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.

Using git-subtree

The implemenation relies upon git-subtree script available with git since release 1.7.11.

For partial import see e.g.

Import from repoB

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.

Update repoA from repoB

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.

Export back to repoB

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.

Extras: one-way ticket (import)

git subtree add --prefix=localSubdir4repoB upstream-repoB master --squash

Resources and Credits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment