Member-only story

How to move some files in Git history to LFS with a single command

Thongchai Kolyutsakul
2 min readMar 22, 2022

I’m assuming you already know what Git LFS is and have it set up already.

Today I have updated GoogleCast SDK in my project to 4.7.0 beta which has support for Apple Silicon simulators. I created a pull request and noticed that there is some binary files of size almost 20MB. So I and the team think it’s a good idea to put it in Git LFS.

The problem is, that binary file is already tracked in git months ago. How can I update git’s reference to it throughout the history?

My first idea is I have to git lfs track to update .gitattribute for that file at the right commit, then manually re-add the commits in one big interactive rebase. That’s is… too much.

Fortunately, there is a quicker way using git lfs migrate import . The documentation is here.

Using `git lfs migrate`

Let’s say the file is at **/GoogleCast.framework/GoogleCast. The are 2 variants of it for different architectures.

The GoogleCast framework binary file
  • find the commit BEFORE you added the large file (e.g. 4c3488dafe)
  • run this command
git lfs migrate import --verbose \
--include="**/GoogleCast.framework/GoogleCast" \
--include-ref=HEAD \
--exclude-ref=4c3488dafe

This will rewrite git history 4c3488dafe to HEAD, converting specified files to pointers and add **/GoogleCast.framework/GoogleCast filter=lfs diff=lfs merge=lfs -text to .gitattributes in the commit the file is introduced.

Quick Tip: Don’t confuse the mode import with export! They are completely opposite operations. More on available modes options here.

Output:

migrate: Sorting commits: ..., done.
migrate: commit d5b2988f2e3d4fcc51dc9ffcf61646444bcec705: App/Features/GoogleCast/GoogleCastSDK-ios-no-bluetooth-4.7.0_dynamic_beta.xcframework/ios-arm64/GoogleCast.framework/GoogleCast
migrate: commit d5b2988f2e3d4fcc51dc9ffcf61646444bcec705: App/Features/GoogleCast/GoogleCastSDK-ios-no-bluetooth-4.7.0_dynamic_beta.xcframework/ios-arm64_x86_64-simulator/GoogleCast.framework/GoogleCast
migrate: Rewriting commits: 100% (13/13), done.
chore/m1-support-no-lfs 6ce8dc46c3c81b3973416f06648986622ca35b29 -> f826ed0aa3428a49e819ea29fde5f4cda22c3b96
migrate: Updating refs: ..., done.
migrate: checkout: ..., done.
  • pushing to remote branch should show “Uploading LFS objects”
Uploading LFS objects: 100% (2/2), 28 MB | 0 B/s, done.
Enumerating objects: 757, done.
Counting objects: 100% (757/757), done.
Delta compression using up to 12 threads
Compressing objects: 100% (532/532), done.
Writing objects: 100% (702/702), 10.05 MiB | 7.05 MiB/s, done.
Total 702 (delta 199), reused 578 (delta 160), pack-reused 0

That’s it. Enjoy!

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Thongchai Kolyutsakul
Thongchai Kolyutsakul

Written by Thongchai Kolyutsakul

iOS developer since 2009. Working @Viki in Singapore. I write about iOS development, Swift language, and general software engineers tips.

No responses yet

Write a response