git: ! [remote rejected] error

2022-09-08

 | 

~2 min read

 | 

303 words

Recently, I was doing something I’ve done numerous times: pushing a new branch up to Github. This time, however, I hit a wall. Or, more accurately, I hit an error:

 ! [remote rejected]       my/branch/name -> my/branch/name (cannot lock ref 'refs/heads/my/branch/name': 'refs/heads/sw/exchange/6/chime/oauth' exists; cannot create 'refs/heads/my/branch/name')
error: failed to push some refs to 'github.com:org/repo-name'

Surprise, surprise, I’m not the first person to have hit this problem. In fact, there’s a great series of suggestions in this Stack Overflow conversation.

In the majority of cases, it sounds like the combination of the following commands will serve you well:

% git gc --prune=now
% git remote prune origin

The latter, while sounding scary, does not affect your actual remote as far as I can tell.

Unfortunately, these answers did not work for me (nor did simply resetting my upstream remote).

Why? What made my situation special? Astute readers may have noticed that my contrived example above used slashes / in the git branch name. This is the pattern I previously recommended. In the previous post, I also noted that there were some limitations with slashes. Like that while some tooling made it look like these were directories, they’re not.

This was the problem. It turns out that I had previously pushed a branch my/branch up to Github. When I renamed it to my/branch/name that was me violating my own rule. I tried to go “deeper” in a directory structure, but only ended up creating a conflict.

Once I’d figured out the problem (thanks to this article), I was able to rename my branch to a non-conflicting name. In this case, I went with my/branch-name instead of my/branch/name.

Once the branch was renamed, the issue evaporated and I was left to go on my merry way. Which is what I’ll do now. Cheers!


Related Posts
  • Git: Tracking Upstream


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!