Merge different git projects into one

Published on
1 mins read

Introduction

To merge a new local project into an old GitHub repository while preserving both commit histories, follow these steps:

Clone our old repository

git clone <old-repo-url>
cd <old-repo-folder>

Add our local project as a remote repository

git remote add new-local-project <path-to-local-project>

Replace <path-to-local-project> with the absolute path to our new local project folder ( e.g., /path/to/new/local/project/.git )

Fetch the new local project's commit history

git fetch new-local-project

Merge the histories

git merge --allow-unrelated-histories new-local-project/main

This command merges the new project's main branch into the old repository's main branch. The --allow-unrelated-histories flag is necessary because the histories of the two repositories are unrelated.

Resolve any merge conflicts

git add <resolved-files>
git commit
git push origin main

Conclusion

After pushing the commit, both the commit histories should appear in the old repository.

Happy reading!