Skip to content

Instantly share code, notes, and snippets.

@bonovoxly
Last active March 29, 2024 22:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bonovoxly/b4f7502f2b6cdb26a779dbfb73d4d134 to your computer and use it in GitHub Desktop.
Save bonovoxly/b4f7502f2b6cdb26a779dbfb73d4d134 to your computer and use it in GitHub Desktop.
git-filter-repo
## git-filter-repo
- Stage a working area. These are somewhat throwaway directories and local git repos.
- clone both source and target repos.
```
mkdir my-working-dir
cd my-working-dir
git clone git@github.com:bonovoxly/source.git
git clone git@github.com:bonovoxly/target.git
```
### In source repo
We checkout a branch that we will filter against. We specify the path(s) that we want.
```
cd ./source
git checkout filter-source
git filter-repo --path ansible/ansible.cfg --path ansible/file.yml --path ansible/another-file.yml --refs refs/heads/filter-source --force
```
## In target repo
```
cd ../target
git checkout -b filter-target
git remote add repo-source ../source
git fetch repo-source
git branch branch-source remotes/repo-source/filter-source
git merge branch-source --allow-unrelated-histories
```
@societymartingale
Copy link

This worked perfectly for me. I was able to migrate a large number of files from one repo to another and retain the git history.

@Gamecock
Copy link

Gamecock commented Feb 23, 2024

The nice thing is, if something goes wrong, you can just checkout main, delete the branch, start all over.

This did not work for me, when I checkout out main it was empty. I had to clone again. Great gist though.

It can really be simplified when pushing to a blank repository. After the filter
git remote add origin https://
git branch -M main
git push -u origin main

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