Skip to content

Instantly share code, notes, and snippets.

@bonovoxly
Last active August 19, 2023 00:51
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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
```
@jdelder
Copy link

jdelder commented Jan 4, 2023

git remote add repo-source ../testing
Just a quick question ... what is ../testing ? Did you mean ../source since that is where the clone commands on the web page put the source repo? "git clone git@github.com:bonovoxly/source.git"

@bonovoxly
Copy link
Author

ah shoot. think i had a typo. bad translation there...

that should be

git remote add repo-source ../source

@bonovoxly
Copy link
Author

@jdelder I've edited the gist to be a bit more specific. Hopefully I didn't add NEW typos 😆

@jdelder
Copy link

jdelder commented Jan 4, 2023

Cool :) You might also want to change the blog entry since that is where I found it initially.
https://blog.billyc.io/how-to-copy-one-or-more-files-from-one-git-repo-to-another-and-keep-the-git-history/
I also used your tips plus the command
git filter-repo --to-subdirectory-filter <new_directory_name>
which I found somewhere else :)
to take a couple directories from one git repo - move them all to a new directory in the filter-source then merged that into another git repo including all the commit information.
P.S. Credit to the creators of the git filter-repo python script (https://github.com/newren/git-filter-repo)

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