Git error when pushing - update_ref failed
Your refs/remotes/origin
directory—this is a directory within in your local repository—has some sort of problem. (It's not clear to me what exactly went wrong, but your guess that it happened when Android Studio crashed seems likely to me as well.)
To fix it, you can probably use the sequence of commands below. Note that I don't recommend it in general—your Git service, whether that's Android Studio or ordinary command-line Git, is not supposed to crash and leave you with a broken repository, so you should never have to do this, it's just an emergency repair, something like putting duct tape all over your car1 after a tree fell on it, just enough to get you to a proper repair / replacement vehicle later:
$ cd $(git rev-parse --show-toplevel) # if necessary
$ rm -rf .git/refs/remotes/origin # remove all origin/*
$ mkdir .git/refs/remotes/origin # create empty origin/
$ git fetch origin # repopulate origin/*
In any case it's likely that your git push
has actually succeeded at this point, since what is failing is the update to your origin/master
remote-tracking branch.
Your Git has just finished talking to another Git on origin
, giving them (the users of origin
) updates for their master
, and origin
has accepted those updates, and your Git is now recording the fact that, the last time it talked with origin
, their master
was set to some particular commit hash—the one you just pushed.
(Remember that your origin/*
remote-tracking branches are just recording, for you, what your Git saw that their Git has for its branches. Your Git updates these when you run git fetch
, where your Git talks to their Git and gets all of its branches. Your git also updates some—not all—on git push
, when your Git talks to their Git and asks them to set one or more branches, to the hashes your Git hands them.)
1Except that as a Git mechanic, instead of just duct tape, baling wire, and chewing gum, my recommended parts are actually proper body panels, belts and hoses, and clamps. :-)
To push the commits to your online repo you need to do this: $ git push origin branch_name
. To know your branch name just do a $ git status
and it will show up(in git bash it is colored in blue). Your issue could arise from different sources. Basically, git is telling you that it cannot understand what you passed as your arguments. Are you sure you are in your repository?
Apart from the steps mentioned in accepted answer, I also had to use the garbage collector command below to remove the dangling commit blobs.
git gc --prune="0 days"
You can check the dangling blobs by running
git fsck
Try running: git gc --prune=now
and deleting .git/refs/remote/origin