{"thread":{"id":"51700","subject":"Bug: `git fetch --prune origin master:refs/foo` incorrectly prunes refs/foo","startedAt":"2019-08-21T00:50:43Z","lastAt":"2019-08-21T00:50:43Z","messageCount":1,"participants":["Lily Ballard"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"380873","messageId":"3927bdd8-5641-4ade-aea5-e843c3bbf612@www.fastmail.com","threadId":"51700","inReplyTo":null,"subject":"Bug: `git fetch --prune origin master:refs/foo` incorrectly prunes refs/foo","fromName":"Lily Ballard","fromEmail":"lily@sb.org","sentAt":"2019-08-21T00:50:39Z","receivedAt":"2019-08-21T00:50:43Z","isPatch":false,"sender":{"key":"lily@sb.org","avatar":null},"body":"When using the --prune flag (or fetch.prune=true), a `git fetch` command that uses a refspec to update a ref that doesn’t exist remotely will behave incorrectly if the ref already exists locally. For example:\n\n> git fetch --prune master:refs/foo\n\nIf refs/foo doesn’t exist, this command will create it with the contents of origin’s master. If refs/foo does exist, it will be pruned immediately. If the `git fetch` doesn’t actually fetch anything (because the commits exist locally) then it will exit with success, despite the ref not existing locally. If it does fetch commits, it will fail with a “cannot lock ref” error.\n\nHere’s the flow for fetching twice:\n\n> git fetch --prune origin master:refs/foo\nFrom <url>\n * [new branch]            master     -> refs/foo\n> git fetch --prune origin master:refs/foo\nFrom <url>\n - [deleted]               (none)     -> refs/foo\n> git rev-parse --verify refs/foo\nfatal: Needed a single revision\n\nAnd here’s what I get if the ref already exists and the fetch actually downloads commits (in this case I’m using --force but that’s not necessary for the bug):\n\n> git fetch --prune --force origin pull/7267/head:refs/foo\nFrom <url>\n - [deleted]               (none)     -> refs/foo\nerror: cannot lock ref 'refs/foo': unable to resolve reference 'refs/foo'\n ! e090924c71...ca5f06da05 refs/pull/7267/head -> refs/foo  (unable to update local ref)\n\nThis was reproduced using git version 2.22.0.\n\n-Lily Ballard\n"}]}