Sub topic 10
Locking binary files to prevent conflicting edits
This chapter assumes you can already track files with git lfs track, commit and push LFS objects, and read .gitattributes. It also assumes you know how two branches can edit the same file and how Git reports a merge conflict.
Why binary files need locks
Git resolves concurrent edits by merging text line by line. A Photoshop file, a 3D model, or a compiled game asset has no lines to merge. If Alice and Bob both change design/logo.psd on separate branches, Git can only report a conflict. Someone then has to pick one version and redo the other person's work by hand.
LFS file locking avoids the problem by moving the decision earlier. Before editing, you ask the LFS server for a lock on the file's path. While you hold it, other people can see who is working on the file, and their tools can refuse to let them edit it casually. When you finish, you release the lock.
Locking does not make binary files mergeable. It prevents the situation where a merge is needed.
The working model
Locking has three parts.
- Server-side lock records. The LFS server stores a list of locks. Each has an ID, a path, an owner, and a creation time. The list is shared by everyone who uses the repository.
- The
lockableattribute. This is a flag in.gitattributesthat marks which patterns are meant to be locked. It travels with the repository, so every clone sees it. - Read-only files in the working tree. For lockable files, Git LFS makes your local copy read-only when you check it out. Running
git lfs lockmakes your copy writable. Runninggit lfs unlockmakes it read-only again.
The read-only flag is the part that changes your habits. If your editor reports that it cannot save logo.psd, you have not locked the file yet.
Marking files as lockable
Use the --lockable flag when you track a pattern:
git lfs track --lockable "*.psd"
git add .gitattributes
git commit -m "Mark PSD files as lockable"
git push origin mainThis writes the same line that git lfs track normally writes, with one extra word at the end:
*.psd filter=lfs diff=lfs merge=lfs -text lockableIf the pattern is already tracked, running the command with --lockable updates the existing line. You can also edit .gitattributes by hand and append lockable.
Commit and push .gitattributes. Teammates only get read-only files after they pull this change and Git next writes those files to their working tree. If someone already has the files checked out, they may need to run git lfs checkout or check the files out again before the read-only flag appears.
You can check which patterns are lockable with git lfs track. Lockable patterns are listed with a [lockable] marker next to them.
The everyday locking cycle
A typical edit looks like this:
git pull
git lfs lock design/logo.psd
# edit and save the file in your design tool
git add design/logo.psd
git commit -m "Update logo colors"
git push origin main
git lfs unlock design/logo.psdSome details matter here.
- Pull first. A lock stops other people from starting new edits. It does not update your copy. If someone committed a new version before you took the lock, you would be editing an old file and creating the conflict you wanted to avoid.
- Push before you unlock. If you unlock first, another person can lock and start editing the old version while your change is still only on your machine.
- The path is relative to the repository root.
git lfs lock logo.psdfrom insidedesign/will normally work, because the client resolves paths, but spelling out the repository-relative path is clearer in scripts.
If the file is already locked, the command fails and names the owner:
Lock failed: design/logo.psd already locked by aliceTalk to Alice. Do not reach for --force yet.
Listing and finding locks
git lfs locks asks the server for all current locks:
git lfs locksEach line shows the path, the owner, and the lock ID. Useful options:
| Command | What it does |
|---|---|
| `git lfs locks --path design/logo.psd` | Shows the lock on one path, if any. |
| `git lfs locks --id 42` | Shows the lock with the given ID. |
| `git lfs locks --local` | Reads from a local cache and does not contact the server. |
| `git lfs locks --json` | Prints machine-readable output for scripts. |
Your own locks may be marked with a plus sign in some client versions, which helps you see what you should release. Check the output of your installed version if you rely on that marker.
Finding stale locks is a good habit at the end of a sprint or before a release. A lock held by someone who has left the team, or who forgot to release it, blocks everyone else's work on that file.
Releasing locks
Release your own lock by path or by ID:
git lfs unlock design/logo.psd
git lfs unlock --id 42If you made changes you have not pushed, git lfs unlock refuses and warns that the file has local modifications. That check protects you from releasing a lock while work is still uncommitted. To release anyway, for example after discarding your changes, add --force:
git lfs unlock --force design/logo.psd--force is also how you break someone else's lock. Servers usually require write access to the repository for this. Break another person's lock only after you have asked them, because their unsaved or unpushed work on that file will then conflict with whatever you do next.
Locks are advisory by default
A lock is a record on the server. It is not a security boundary. Several things can still go wrong.
- Local edits that ignore the read-only flag. Anyone can change file permissions with
chmod, or use a tool that overrides the flag, and edit a file they did not lock. - Clients without LFS locking. A teammate using plain Git, or an older LFS client, never asks the server about locks.
- Lockable not set. If the pattern is missing
lockable, files are not made read-only, and people forget to lock them.
Push-time checks make the system more reliable. When lock verification is enabled, the LFS client asks the server which files in the push are locked by other people, and it refuses the push if any are. Many servers and recent client versions turn this on or prompt you to enable it. You can enable it yourself for a remote:
git config lfs.https://example.com/team/assets.git/info/lfs.locksverify trueThe key is the remote's LFS URL followed by .locksverify. Replace the example URL with your own. With this on, a push that includes a file locked by someone else fails with an error, and the person pushing has a reason to stop and check.
Common errors and what they mean
The file is read-only and the editor cannot save. This is the intended behavior. Run git lfs lock <path> first. If the lock fails because someone else owns it, wait or ask them to release it.
Teammates' files are not read-only. Check that lockable is present on the pattern in the committed .gitattributes, that it was pushed, and that they pulled it. Then have them run git lfs checkout so the flag is applied to files already in their working tree.
git lfs lock says the file is not found or not tracked. The path must exist in the repository, and it should match a tracked pattern. Check the spelling and that you run the command inside the repository.
Push rejected because of a lock. Someone else holds a lock on a file in your push. Run git lfs locks to see who. Either wait, ask them to release, or remove the file's changes from your commits.
You cannot unlock because of local modifications. git lfs unlock found uncommitted changes. Commit and push them, or discard them, and run the command again. Use --force only when you are sure.
A lock stays on an old path after a rename. Locks belong to paths, not to file contents. If you rename a locked file, the lock stays attached to the old name. Release the lock before renaming, then lock the new path if you need to keep working.
Team conventions that make locking work
- Lock only what you will change. A habit of locking whole directories one file at a time slows everyone down.
- Lock, edit, commit, push, unlock. Treat it as one unit of work, and keep the time between lock and unlock short.
- Review locks regularly. Run
git lfs locksin standups or as a scheduled job and ask owners of old locks to release them. - Document who can force-unlock. Name the people who may break a lock, and the steps they should follow first.
- Mark every unmergeable type. Add
lockablefor formats such as.psd,.fbx, and.blend. Leave it off for text-based files that Git merges well.
Exercise
Your teammate Dana reports that she can edit art/hero.fbx and save it without running git lfs lock. Your repository has *.fbx filter=lfs diff=lfs merge=lfs -text in .gitattributes. What is missing, and what should Dana do after the fix is pushed?
Show answer
The pattern is not marked lockable, so LFS does not make matching files read-only. Run git lfs track --lockable "*.fbx", commit and push .gitattributes. Dana then pulls the change and runs git lfs checkout so her existing .fbx files become read-only. From then on she runs git lfs lock art/hero.fbx before editing.
Exercise
Put these steps in the safest order for editing a locked binary file: unlock, commit, lock, push, pull, edit.
Show answer
pull, lock, edit, commit, push, unlock. Pulling first makes sure you edit the latest version. Pushing before unlocking makes sure the next person starts from your version.