15

I have a problem when i try to push my local commits, it has probably happened when Android Studio has crushed. Here is the error:

update_ref failed for ref 'refs/remotes/origin/master': cannot lock ref 'refs/remotes/origin/master': unable to resolve reference refs/remotes/origin/master: Invalid argument

$git stash says that HEAD is now at my latest commit.

I'm still a beginner in git and I have no idea how to fix it.. What can I do?

Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
Pawel
  • 163
  • 1
  • 1
  • 5

6 Answers6

40

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. :-)

torek
  • 448,244
  • 59
  • 642
  • 775
4

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
wick.ed
  • 473
  • 7
  • 15
3

Try running: git gc --prune=now and deleting .git/refs/remote/origin

mellifluous
  • 2,345
  • 2
  • 29
  • 45
  • 3
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Dec 16 '20 at 08:54
  • I recommend to rephrase from "Try ..." to "If the problem is ... you can find out by ... and fix by doing ..., because it works like ... and solves the problem because ....". – Yunnosch Dec 16 '20 at 08:55
  • 1
    It worked for me, just deleting the .git/refs/remote/origin. thasnks – Moisés Aguilar Jan 21 '21 at 05:03
2

I ran out of disk space and git couldn't write to the lock file as a result

usbdongle
  • 81
  • 6
1

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?

Shirish Kadam
  • 689
  • 1
  • 9
  • 21
limbo
  • 684
  • 9
  • 18
  • Error which i get shows up when i try to push. I'm on branch master. Yes, i'm sure i'm in my repository because i can see changes which i make in Android Studio, e.g . when i add a drawable, i can also see my latest commit in stash, here is the message: "Saved working directory and index state WIP on master: a5fa9f4 Floating Action Bar and night theme added HEAD is now at a5fa9f4 Floating Action Bar and night theme added" Any ideas? – Pawel Jul 14 '16 at 18:19
  • @Pawel so you use this exact command: git push origin master and it errors out? – limbo Jul 14 '16 at 22:12
0

My similar error message:

error: cannot update the ref 'refs/remotes/origin/alex-he/ITCAML-1691': unable to create directory for '.git/logs/refs/remotes/origin/alex-he/ITCAML-1691': No such file or directory

I had this problem is because I had a file instead named "alex-he" in both directories. But it should be a directory. So I just manually deleted the file in both directories and then the issue solve.

Jing He
  • 794
  • 1
  • 9
  • 17