Skip to content

Instantly share code, notes, and snippets.

@smetana
Last active January 6, 2021 15:52
Show Gist options
  • Save smetana/f57b06303ddd999fbae3b42a947c754c to your computer and use it in GitHub Desktop.
Save smetana/f57b06303ddd999fbae3b42a947c754c to your computer and use it in GitHub Desktop.
Add git repo as folder to another repo preserving history

Before

repo1
|--.git
|--.gitignore
|--repo1_files

repo2
|--.git
|--.gitignore
|--repo2_files

We will move content and history of repo1 to lib directory of repo2

After

repo2
|--.git
|--.gitignore
|-- lib
    |--.gitignore
    |--repo1_files
|--repo2_files

Tools needed

https://github.com/newren/git-filter-repo

pip3 install git-filter-repo
# or any other method

Magic

## Make fresh clone of repo1 and remove remotes
git clone https://github.com/you/your-repo.git /home/user/repo1
cd /home/user/repo1
git remote remove origin

# Move content of the repo1 root directory to subfolder
git filter-repo --to-subdirectory-filter lib

# Add local modified repo1 as remote to target repo2, copy history, and rebase
cd /home/user/repo1
git remote add tmp /home/user/repo1
git fetch tmp
git checkout tmp/master
git checkout -b feature/some-lib
git rebase -i master
git remote remove tmp
git gc
git prune
``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment