Skip to content

Instantly share code, notes, and snippets.

@bonovoxly
Last active August 23, 2024 21:04
Show Gist options
  • 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 [email protected]:bonovoxly/source.git
git clone [email protected]: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
```
@scelfo
Copy link

scelfo commented Jul 12, 2024

Thanks for publishing this! I think there's one more typo:

git checkout filter-source should be git checkout -b filter-source

@gshel
Copy link

gshel commented Aug 23, 2024

Thank you for publishing this!

I ran into some gotchas:

  1. Install git-filter-repo
  2. If you need to move files into a different directory structure or rename them, make sure to do this in a separate, isolated commit -- don't include any other changes in that commit!
  3. To view the pretty git history made possible by your gist: git log --follow $FILEPATH

#3 will allow you to see the git history of the file after it has been renamed, so long as you follow #2

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