Git Combine Two Repositories

Aus MattWiki
Version vom 10. November 2024, 16:46 Uhr von Matt (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „This article describes how to combine two Git repositories in a new repository while keeping histories of both of the repositories intact. == Prepare the New Repository == Create and initialize a new repository where you will combine both repositories.<syntaxhighlight lang="bash"> # Create a new directory and initialize a Git repository mkdir combined-repo cd combined-repo git init </syntaxhighlight> == Add First Repository (Repo 1) as a Remote and Pull…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

This article describes how to combine two Git repositories in a new repository while keeping histories of both of the repositories intact.

Prepare the New Repository

Create and initialize a new repository where you will combine both repositories.

# Create a new directory and initialize a Git repository
mkdir combined-repo
cd combined-repo
git init

Add First Repository (Repo 1) as a Remote and Pull

Add the first repository as a remote, then fetch and pull its `master` branch into the new repository. This will keep Repo 1’s history intact.

# Add Repo 1 as a remote
git remote add repo1 <URL_to_Repo_1>
git fetch repo1

# Create a local branch from Repo 1's master
git checkout -b master repo1/master

Create a Subdirectory for Repo 2 (Optional)

To avoid file conflicts, consider placing the contents of Repo 2 into a subdirectory within the new repository. This keeps both repositories' files separate and makes the combined history clearer.

# Create a directory for Repo 2's files
mkdir repo2-files

Add the Second Repository (Repo 2) as a Remote and Fetch

Add Repo 2 as another remote, then fetch its master branch.

# Add Repo 2 as a remote
git remote add repo2 <URL_to_Repo_2>
git fetch repo2

Check Out Repo 2’s Master Branch into the Subdirectory

To keep Repo 2's history intact but merge it into the current repository, use Git’s --allow-unrelated-histories flag and move its files into the subdirectory.

# Create a temporary branch for Repo 2
git checkout -b repo2-temp repo2/master

# Move all Repo 2's files into the subdirectory
mkdir -p repo2-files
git mv * repo2-files/  # Adjust if any hidden files like .gitignore need to be moved manually
                       # Hint: * does not work, git move dir-by-dir, file-by-file

# Commit the file move to preserve Repo 2’s structure in the new repository
git commit -m "Moved Repo 2 files to repo2-files subdirectory"

Hint: Timeline in VSCode becomes only after this commit. Or the next one.

Merge Repo 2’s History into the New Repository’s Master Branch

Switch back to the master branch and merge in the repo2-temp branch, keeping all history intact.

# Switch back to master
git checkout master

# Merge Repo 2's temp branch into master, allowing unrelated histories
git merge repo2-temp --allow-unrelated-histories

git commit -m "repositories combined in new master branch"

Clean Up Remotes and Temporary Branches

After the merge is complete, remove the temporary branch and any unnecessary remotes.

# Remove the remotes if they’re no longer needed
git remote remove repo1
git remote remove repo2

# Delete the temporary branch
git branch -d repo2-temp

Verify and Push to Your New Repository

Finally, inspect the history and verify that both repositories’ histories are intact. If everything looks good, push the combined repository to your new remote.

# Add the new remote (e.g., GitHub, GitLab)
git remote add origin <URL_to_New_Repo>

# Push the combined master branch
git push -u origin master