How to Move Files from Git LFS Back to Standard Git Management
How to Move Files from Git LFS Back to Standard Git Management
When using Git LFS (Large File Storage), we can store large files such as images, videos, etc., in LFS to save space in the Git repository. However, sometimes we might want to move these files back to standard Git storage and stop using LFS. This article will show how to restore files from Git LFS to regular Git and remove them from LFS.
Background
Suppose we have the following rule in place:
**/img/* filter=lfs diff=lfs merge=lfs -text
Currently, some images match this rule and are stored in Git LFS. Now, we want to move these files back to standard Git and stop using LFS to manage them.
Steps
1. Modify the .gitattributes
File
First, we need to modify the .gitattributes
file in the project.
Find and either delete or comment out the following rule:
**/img/* filter=lfs diff=lfs merge=lfs -text
Once you remove this rule, Git will no longer apply LFS management to the images in the img
folder.
2. Remove Files from LFS Tracking
Next, we need to stop LFS from tracking these files. Run the following command:
git lfs untrack "**/img/*"
This command tells Git LFS to stop tracking the image files in the img
folder.
3. Manually Move Files Back to Standard Git
Since LFS stores pointers to the actual files, we need to manually move the files back to standard Git storage. Here's how:
1. Check the Status of LFS Files
We can check which files are currently being tracked by LFS using the following command:
git lfs ls-files
Ensure you see the files you want to remove.
2. Backup Files (Optional)
To avoid any issues, you can back up the image files to a temporary folder.
3. Remove LFS Pointer Files
We can remove the LFS pointer for the files using the git rm --cached
command:
git rm --cached path/to/img/file.jpg
This doesn't delete the actual files, but it removes the LFS pointer from the Git staging area.
4. Re-add Files to Standard Git
Next, re-add these files to standard Git:
git add path/to/img/file.jpg
5. Commit the Changes
Commit the changes:
git commit -m "Remove files from LFS and store in standard Git"
4. Clean Up Old Files in LFS
After moving the files out of LFS, they might still occupy space in LFS storage. We can clean up unused LFS files with the following command:
git lfs prune
This will remove any LFS objects that are no longer being tracked, freeing up storage.
5. Push to the Remote Repository
Finally, push your changes to the remote repository:
git push origin main
Make sure to replace main
with the branch you are working on if it's different.
Conclusion
By following these steps, we have successfully moved files from Git LFS back to standard Git management. Here’s a quick summary:
- Modify the
.gitattributes
file to remove the LFS management rule. - Use
git lfs untrack
to stop LFS from tracking the files. - Remove the LFS pointer and re-add the files to standard Git.
- Commit and push the changes.
- Clean up old LFS files using
git lfs prune
.
With these actions, your Git repository will no longer use LFS to manage these files, and they will be stored under standard Git version control.