如何从Git LFS拉取所有文件并本地缓存以实现离线使用?
Hey there! Let's tackle your two Git LFS questions one by one—these are super common asks when working with large files in Git, so I’ve got you covered.
1. Pulling All Git LFS Files
Depending on whether you’re working with a new or existing repo, here are the straightforward commands to grab every LFS-tracked file:
- Cloning a new repo: Skip the two-step process and pull LFS files alongside the regular Git clone with:
This automatically fetches and checks out all LFS objects as part of the clone, so you don’t have to run extra commands afterward.git clone --lfs <repo-url> - For an existing repo: If you already have the repo cloned but haven’t pulled LFS files yet, run:
This combines two actions:git lfs pullgit lfs fetch(which downloads LFS objects to your local cache) andgit lfs checkout(which creates the actual file pointers in your working directory). - Fetching all historical LFS objects: If you need every version of LFS files from all branches, tags, and commits (not just your current checkout), use:
Follow this up withgit lfs fetch --allgit lfs checkoutto ensure all those cached objects are linked to your working directory.
2. Caching All LFS Files for Offline Usage
Great news—Git LFS is built with local caching in mind, so once you’ve pulled all LFS objects, you can work offline just like a regular Git repo. Here’s how to lock in that offline capability:
- First, ensure all LFS objects are cached locally: Run
git lfs fetch --allto grab every single LFS object from the remote. These get stored in your local LFS cache (default location:.git/lfs/objects). - Verify you have all objects: Check that no LFS files are marked as "missing" with:
Any file without agit lfs ls-files --all*next to it is missing from your cache—if you see those, re-rungit lfs fetch --allto grab them. - Prevent accidental cache pruning: By default, Git LFS might prune old or unused objects to save space. To keep all cached objects (critical for offline use), you can either:
- Avoid running
git lfs prune(this command deletes cached objects not referenced by any local commit) - Or set a config to disable automatic pruning entirely:
git config lfs.prune.enable false
- Avoid running
Once you’ve completed these steps, you can commit changes, switch branches, and work with your repo entirely offline. Git will automatically use the local LFS cache instead of reaching out to the remote. Just a quick heads-up: if you later switch to a branch with LFS files you never fetched, you’ll need to go online to grab those—but as long as you’ve fetched all objects first, offline work is smooth sailing.
内容的提问来源于stack exchange,提问作者Norman Percy




