Skip to content

Instantly share code, notes, and snippets.

@corneil
Last active March 24, 2023 11:54
Show Gist options
  • Save corneil/13b0488aa6949f3d863b0189adfc07c4 to your computer and use it in GitHub Desktop.
Save corneil/13b0488aa6949f3d863b0189adfc07c4 to your computer and use it in GitHub Desktop.
Importing one git project into another.

If you want to import one project into another and the resulting project will be a subfolder into the parent the following steps will allow you preserve history and tags.

The tags from sub-project are prefixed as part of the process.

The scripts assume the 2 projects are on disk with the same parent folder.

Be careful on the merged project because some of the historical commits only reference the sub project and branching from the wrong sha can cause confusion.

BRANCH=main
REMOTE=sub-project
PREFIX=sub
cd sub-project
git checkout $BRANCH
# remove all files/folders that are not needed
git rm <files-not-needed> except for .git
git commit -m "Prepare for move to parent"
cd ../parent
git fetch --no-tags $REMOTE refs/heads/*:refs/heads/$PREFIX/* refs/tags/*:refs/tags/$PREFIX/*
git merge -s ours --no-commit $REMOTE/$BRANCH --allow-unrelated-histories
git read-tree --prefix=$REMOTE/ -u $REMOTE/$BRANCH
git commit -m "merged in $BRANCH from project $REMOTE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment