Move Git LFS tracked files under regular Git
I have just recently run into this problem where assets were accidentally added to git-lfs on one branch that shouldn't have been. My solution was:
git lfs untrack '<file-type>'
git rm --cached '<file-type>'
git add '<file-type>'
git commit -m "restore '<file-type>' to git from lfs"
The result is a rewrite of the git-lfs oid sha256 pointers with the standard file contents.
(Edit 2019-03): The accepted answer was changed to provide an easy solution for simpler cases. See also the edits in the answer by VonC for alternate solutions in case you have a more complex case on hand.
As of Git 2.16 (released Jan 17th, 2018), you can do this easily with the --renormalize
flag of git add
:
git lfs untrack '<pattern>'
git add --renormalize .
git commit -m 'Restore file contents that were previously in LFS'
From Git's documentation:
--renormalize: Apply the "clean" process freshly to all tracked files to forcibly add them again to the index. This is useful after changing
core.autocrlf
configuration or thetext
attribute in order to correct files added with wrong CRLF/LF line endings. This option implies-u
.
The key part here is "all tracked files". Normally, filters are only run when a Git operation changes a file in the work tree. Changing the LFS whitelist in .gitattributes
isn't a Git operation, and so the index ends up in an inconsistent state after you run git lfs untrack
. Running git add --renormalize .
tells Git to re-run filters on every file in the repository, which ensures that all files which should be in LFS are—and that all files which shouldn't be aren't.