git · Git
The message
error: cannot lock ref 'refs/remotes/origin/main': is at 8a3f21c but expected 1c2d3e4
What it means
As git went to write a new value into a remote-tracking ref, it found a value other than the one it had just read, so it refused to overwrite. It commonly happens when your editor fetches in the background while you fetch too, or when someone force-pushed and your remote-tracking refs are out of step. Running the fetch again usually just works. git remote prune origin tidies away remote-tracking refs whose branches no longer exist upstream; it deletes only the copies inside your own repository and never touches a branch on the server.
The fix
git remote prune origin- Printed by
- git
- Git
- 29
Almost every git error is a refusal that means "doing that from this state would lose something"; fatal: means git stopped without changing anything, and the hint: lines under the first line usually carry the actual remedy.
Reading an error message
- Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
- If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
- Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
- The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
- Before pasting a fix, check what it throws away. Some of these cannot be undone.
Common questions
Q. What does “error: cannot lock ref 'refs/remotes/origin/main': is at 8a3f21c but expected 1c2d3e4” mean?
As git went to write a new value into a remote-tracking ref, it found a value other than the one it had just read, so it refused to overwrite. It commonly happens when your editor fetches in the background while you fetch too, or when someone force-pushed and your remote-tracking refs are out of step. Running the fetch again usually just works. git remote prune origin tidies away remote-tracking refs whose branches no longer exist upstream; it deletes only the copies inside your own repository and never touches a branch on the server.
Q. How do I fix it?
git remote prune origin — before running it, check the explanation above for what this command discards.
Q. Which tool prints this?
git. It sits under Git, and the message runs to 11 words.