Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ryantang/52710a9f557956557211aac9ee90418b to your computer and use it in GitHub Desktop.
Save ryantang/52710a9f557956557211aac9ee90418b to your computer and use it in GitHub Desktop.
how to convert your repo to git-lfs and rewrite history
1. When trying to migrate a repo to Github
```
git clone --bare ssh://git@gitserver.example.com:2222/project-name/repo-name.git #clone a bare repo locally
curl --header "Authorization: token <GITHUB API TOKEN>" --data '{"name": "repo-name", "description": "Migrated from project-name from gitserver.example.com" https://api.github.com/orgs/org-name/repos #create github repo
git push --mirror git@github.com:org-name/repo-name.git
```
you'll see an error like this:
```
remote: error: File /test/assets/some_dataset.dat is 128.00 MB; ; this exceeds GitHub's file size limit of 100 MB
remote: error: File /setup/large_binary.tar.gz is 251.00 MB; this exceeds GitHub's file size limit of 100 MB
```
2. Convert the repository to a git-lfs repository. **This rewrites history, so be careful**. One way to do this is to use `git filter-branch`. I found two other tools that make it a lot easier [BFG Repo-Cleaner](https://rtyley.github.io/bfg-repo-cleaner/) and [git-lfs-java](https://github.com/bozaro/git-lfs-java). This write-up uses BFG.
a. Make a copy of your repository, locally (`cp -r repo-name.git repo-name-orig.git`)
b. Download bfg jar file
c. Run the bfg command to convert your repository
```bash
cd repo-name.git
java -jar ../../bfg-1.12.12.jar --convert-to-git-lfs '*{some_dataset.dat,large_binary.tar.gz},' --no-blob-protection
```
d. You should notice that your commit SHA changed and you have files in your `lfs/objects` directory.
e. Clean your repo, BFG instructs: `git reflog expire --expire=now --all && git gc --prune=now --aggressive`
3. Initialize git-lfs in your repository `git lfs install`
4. Work around the (git-lfs bug for unnamed remote urls)[https://github.com/github/git-lfs/issues/1069]: `git remote set-url --push git@github.com:org-name/repo-name.git`. Check the result with `git remote -v`
5. Git push `git push --mirror origin
@vsfedorenko
Copy link

It was helpful

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