Skip to content

Instantly share code, notes, and snippets.

@OmeGak
Last active October 27, 2021 18:02
Show Gist options
  • Save OmeGak/5481323 to your computer and use it in GitHub Desktop.
Save OmeGak/5481323 to your computer and use it in GitHub Desktop.
Fix for cleaning a repository when: "error: Couldn't set refs/heads/branchName"
# Solves:
# error: Couldn't set refs/heads/branchName
# To X:xxx.git
# ! [remote rejected] branchName -> branchName (failed to write)
# error: failed to push some refs to 'X:xxx.git'
git fsck --unreachable
git reflog expire --expire=0 --all
git repack -a -d -l
git prune
git gc --aggressive
@looeee
Copy link

looeee commented Jun 18, 2018

Just a warning to anyone else that finds this - the double dashes have been converted to long single dashes above. It should be:

git fsck --unreachable
git reflog expire --expire=0 --all
git repack -a -d -l
git prune
git gc --aggressive

In any case, this didn't work for me. The problem seemed to be that the reference to HEAD in my remote was wrong.

I updated

REMOTE: .git/refs/heads/master

to be the same as

LOCAL: .git/refs/heads/master

and everything worked again. I'm using Google Drive to store repos.

EDIT: but the next time I made a commit and tried to push it, I'm back to the same error

@h-tendy
Copy link

h-tendy commented Sep 27, 2018

I had the same problem. I quit Google Drive and did a 'git push' and it worked. So there is some workaround at least.

@Derkness
Copy link

Derkness commented Sep 27, 2021

I find myself having to do this on every commit if its been more than like, 5 minutes. Is there a permanent fix? ( i am not suing google drive or anything). Furthermore, I find this corrupts my history and makes lal my commits look like they were at once.

@santiago-salas-v
Copy link

santiago-salas-v commented Oct 27, 2021

I have been having this issue a lot, with remote stored in a network drive. Apparently local refs have drifted from remote. The tip from the ohshitgit guide is useful (https://ohshitgit.com/), in all cases it has solved my problem, as long as it is ok to reset local:

git reset --hard origin/branchName

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment