Sub topic 10
Locking Binary Files to Prevent Conflicting Edits
This chapter assumes you can track files with git lfs track, commit and push LFS files, and read .gitattributes. It covers Git LFS file locking, which stops two people from editing the same unmergeable file at once.
Why binary files need locks
Git merges text by comparing lines. A Photoshop file, a 3D model, or a compiled asset has no lines to compare. If two people edit design/logo.psd on separate branches, Git cannot combine the changes. One person must discard their work and redo it on top of the other's.
Locking avoids that waste. A lock is a record on the LFS server saying "this path is being edited by this user." Others can see the lock and wait.
Locks are advisory on the server side, but LFS adds a local safeguard: files marked lockable are read-only in your working tree until you lock them.
Marking files as lockable
Use the --lockable flag when tracking a pattern.
git lfs track --lockable "*.psd"
git add .gitattributes
git commit -m "Make PSD files lockable"This changes the line in .gitattributes:
*.psd filter=lfs diff=lfs merge=lfs -text lockableThe lockable attribute tells Git LFS to make matching files read-only after checkout. The read-only flag reminds you to take a lock before editing.
The lock workflow
git lfs lock design/logo.psd
git lfs locks
git lfs unlock design/logo.psdgit lfs lockasks the server to reserve the path. If someone else holds it, the command fails and names the owner. If it succeeds, the file becomes writable on your machine.- Edit the file, commit it, and push as usual.
git lfs unlockreleases the lock so others can edit.
git lfs locks lists every lock on the server, with the path, the owner, and a lock ID.
Useful options
| Command | Purpose |
|---|---|
| `git lfs locks --path design/logo.psd` | Check one file |
| `git lfs locks --verify` | Mark which locks are yours |
| `git lfs unlock --id 123` | Unlock by lock ID |
| `git lfs unlock --force design/logo.psd` | Release a lock held by someone else |
Forced unlocking needs write access to the repository. Use it for abandoned locks, for example when a colleague left without unlocking.
Common errors
- The file is read-only and you cannot save. You did not lock it. Run
git lfs lock <path>first. - Lock command fails with "not supported" or a 404. Your LFS server may not implement the locking API, or the remote URL is wrong. Check the host's documentation.
- Someone else's lock blocks your push. On servers that verify locks, pushing a change to a file locked by another user is rejected. Ask the owner to unlock, or coordinate with them.
- A lock is left behind after merging. Locks do not release themselves. Unlock the file once your work has been pushed.
- Edits made without a lock. If you change the permissions yourself and edit anyway, nothing stops you locally. Conflicts appear when you push or merge. Treat the read-only flag as a habit, not a guarantee.
Practical tips
- Lock only when you start editing, and unlock as soon as your commit is pushed.
- Run
git lfs locksbefore starting work to see what is taken. - Make only the file types that cannot be merged lockable. Text files that merge well do not need locks.
- Agree as a team on who may force-unlock.
Exercise
A teammate asks why design/logo.psd is read-only after they cloned the repository, and they cannot save changes. What caused it, and what do they run to fix it?
Show answer
The *.psd pattern is marked lockable in .gitattributes, so LFS makes matching files read-only until they are locked. They run git lfs lock design/logo.psd, edit and commit the file, push, and then run git lfs unlock design/logo.psd.
Exercise
You find a lock on art/hero.psd held by a colleague who left the company. Which command releases it, and what does it require?
Show answer
git lfs unlock --force art/hero.psd. It requires write access to the repository.