Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reference broken #3838

Closed
herio5154 opened this issue Jan 21, 2018 · 22 comments
Closed

reference broken #3838

herio5154 opened this issue Jan 21, 2018 · 22 comments
Assignees

Comments

@herio5154
Copy link

error: cannot lock ref 'refs/remotes/origin/master': unable to resolve reference 'refs/remotes/origin/master': reference broken
From https://github.com/herio5154/stargoon
! [new branch] master -> origin/master (unable to update local ref)

@shiftkey
Copy link
Member

shiftkey commented Jan 22, 2018

@herio5154 thanks for the feedback! It looks like something with the underlying Git repository has broken, leaving it in an invalid state.

I think these steps should get you back to a happy place, but take a backup of your repository before running any of these commands, just in case.

I'll explain the intent behind each step as we go along. This assumes using Git Bash, but if you are using a different shell let me know which and I'll tweak it.

rm .git/refs/remotes/origin/master

Git stores refs and other Git data on disk under the .git folder, and it looks like the value of whatever origin/master entry Git was tracking is now invalid. This will remove the file to hide it from Git locally.

git fetch

This tells Git to go and fetch whatever it can find from the remote.

git branch --set-upstream-to=origin/master

This ensures your local master is tracking whatever is currently on origin/master, which should put Git back into a happy place.

@shiftkey shiftkey self-assigned this Jan 22, 2018
@shiftkey
Copy link
Member

Closing this out due to inactivity. @herio5154 let us know if there's anything else we can do to address the problem!

@ThatTonybo
Copy link

This actually worked for me too, thanks! ❤️

@lock lock bot locked as resolved and limited conversation to collaborators Aug 17, 2018
@desktop desktop unlocked this conversation Sep 27, 2018
@mikaelovi
Copy link

This worked for me too

@cfandel
Copy link

cfandel commented Feb 15, 2019

Hello,
I'm having the same problem in GitHub Desktop, but I'm fairly new to using it and don't have Git installed, only GitHub Desktop. Could you explain how to approach solving the problem assuming a little bit less pre-existing knowledge & without having Git installed? I'm working on Windows 10 and have the latest version of GitHub Desktop.
Thank you!

@steveward
Copy link
Member

steveward commented Feb 15, 2019

@cfandel are you seeing the same exact error output? If you can share the specific error message I can verify that the solution provided will work for you.

Could you explain how to approach solving the problem assuming a little bit less pre-existing knowledge & without having Git installed?

Unfortunately this problem is not resolvable within GitHub Desktop since it requires running a few specific Git commands. Since you do not have Git installed, you will need to download it from here:

https://git-scm.com/download/win

Once you install Git you can launch it directly from GitHub Desktop and follow the steps @shiftkey provided. I would recommend making a backup of the repository, and let me know if you run into any specific issues.

The other workaround would be to clone a fresh copy of the repository, which will not have the broken references. If your local repository and remote repository are up-to-date with each other then that would be the easiest solution.

@cfandel
Copy link

cfandel commented Feb 15, 2019

Thanks for the quick response!
The files on my computer (these are called the local files yes?) are up to date, but the files on GitHub are NOT up to date (because I made changes locally and then wasn't able to push them).
In GitHub Desktop, the push button has changed to 'Publish branch' and I get the following error:

error: cannot lock ref 'refs/remotes/origin/master': unable to resolve reference 'refs/remotes/origin/master': reference broken From https://github.com/cfandel/gottesacker ! [new branch] master -> origin/master (unable to update local ref)

I downloaded Git and tried the steps above, but get this result:

$ rm.git/refs/remotes/origin/master bash: rm.git/refs/remotes/origin/master: No such file or directory

When you say clone a fresh copy of the repository, does that mean taking the copy on my computer and replacing everything currently on GitHub with the files I have, or vice-versa? If it means preserving the version on my computer, that's a fine solution, but otherwise it will result in losing a bunch of work.
Also, in the long term, if this issue happens again, it would be best to know how to repair it without having to clone a fresh copy no?

**Note: I wasn't able to see where to launch Git from GitHub Desktop? So I tried this in Git Bash launched from the Windows Search Bar.

@steveward
Copy link
Member

@cfandel it looks like you may have missed a space between the rm (remove) command and the file path:

rm .git/refs/remotes/origin/master

**Note: I wasn't able to see where to launch Git from GitHub Desktop? So I tried this in Git Bash launched from the Windows Search Bar.

You can go to the menu and select File > Open in Command Prompt. That will launch CMD -- if you would prefer to use Git Bash you can change this setting by going to File > Options > Advanced and choosing Git Bash from the Shell dropdown.

If it means preserving the version on my computer, that's a fine solution, but otherwise it will result in losing a bunch of work.

In that case we will want to get things working with the local repository, since a fresh clone will not have your local changes.

@steveward
Copy link
Member

@cfandel it may actually be that you were not in the correct directory when you ran the command -- if you open the repository in the command line from within GitHub Desktop it will drop you right into the correct directory. If you open Git Bash separately it will launch into your User directory, and you will need to use cd /path/to/repository to change to the correct directory.

@cfandel
Copy link

cfandel commented Feb 18, 2019

Thanks so much for the help!
I opened Command Prompt from GitHub Desktop (though it's under the Repository menu not the File menu), and tried again. If I understand correctly, all Git commands run from Command Prompt have to be preceded by the word "git"? And if they are run from Git Bash, the command is exactly the same but without the preceding "git", but I need to make sure to be in the correct directory first?

Unfortunately I'm still not having any success with the steps above.
Here are the commands I tried and the results (note that I am now in the correct directory I think):

C:\Users\Chloe\Documents\GitHub\gottesacker>rm .git/refs/remotes/origin/master
'rm' is not recognized as an internal or external command, operable program or batch file.
This means I need to tell Command Prompt that I want to use Git commands right?
So I tried adding a preceding "git":
C:\Users\Chloe\Documents\GitHub\gottesacker>git rm .git/refs/remotes/origin/master
And get this result:
fatal: pathspec '.git/refs/remotes/origin/master' did not match any files

If I try skipping to the next step I get this:
C:\Users\Chloe\Documents\GitHub\gottesacker>git fetch error: cannot lock ref 'refs/remotes/origin/master': unable to resolve reference 'refs/remotes/origin/master': reference broken From https://github.com/cfandel/gottesacker ! [new branch] master -> origin/master (unable to update local ref)

***EDIT:
I tried this exact same process, but by opening Git Bash from within GitHub Desktop, and it seems to have worked! However, I'm still not at all clear about why - what is the difference between doing it in Command Prompt and doing it in Git Bash?

@shiftkey
Copy link
Member

@cfandel I'm glad you were able to workaround the issue!

'rm' is not recognized as an internal or external command, operable program or batch file.
This means I need to tell Command Prompt that I want to use Git commands right?

rm is a built-in program that's part of most Unix shells. The equivalent command in Command Prompt is del but they accept different parameters, so be mindful of the shell you are using to run commands.

However, I'm still not at all clear about why - what is the difference between doing it in Command Prompt and doing it in Git Bash?

Git Bash has an rm included in it's environment, so the command works fine there. git rm is a Git-specific command to remove files from within a Git repository, and isn't a replacement for rm.

@cfandel
Copy link

cfandel commented Feb 19, 2019

Very helpful explanation - thank you!

@cfandel
Copy link

cfandel commented May 15, 2019

Hello again,

I have been having this issue about once a month, and each time repairing it by following the steps described above, which has been working fine.
However, the most recent time this problem occurred, I wasn't able to repair it.
The original error message when I try to commit changes is:

"Commit failed - exit code 128 received, with output: 'fatal: cannot lock ref 'HEAD': unable to resolve reference 'refs/heads/master': reference broken"

In Git Bash in the current repository, I tried:
rm .git/refs/remotes/origin/master git fetch git branch --set-upstream--to=origin/master

and get the following error:
fatal: Failed to resolve HEAD as a valid ref.

Screenshot below:
image

Could you recommend what the best solution would be?
Thanks!

@SNB1726
Copy link

SNB1726 commented May 28, 2020

$ git commit -m "Commit-1"
fatal: cannot lock ref 'HEAD': unable to resolve reference 'refs/heads/master': reference broken
Please Help

@face-hh
Copy link

face-hh commented Aug 30, 2020

image
help pls

@tierninho
Copy link
Contributor

@face-hh Please file a new issue here if you still need assistance. Thank you.

@Dtech-Dbug
Copy link

Same. The issue still persists, even after following the steps @shiftkey mentioned.
Can I get some assistance?
Screenshot (66)

@Dtech-Dbug
Copy link

The rest of the two steps returned errors too.

I can not commit, even from VSCode.
I am not sure what caused the error suddenly, as it was working fine, just a few moments ago too.

@Dtech-Dbug
Copy link

I could finally work around this weird issue. Came with a tradeoff.
With the issue, I could not even initialize a git repo.

Apparently, by deleting the .git folder from the workspace solved a lot of errors.
The instant reflection was, I could initialize a git repo , just after I deleted the git folder.
i.e , .git/config

Afterwards, I could commit directly from the Github desktop, as well. Just that the repository name changed locally, and had to publish a new repo(a new name , because already existing name, gave errors) to the remote.

Hope this helps.

As you can see, previously the dir name was React-Ecomm-App, it changed to MERN-Stack-App , also another tradeoff was, I lost all the staged changes commit...
Still not sure what caused the whole issue in the first place, any explanation in laymen would be highly appreciated! Thank You ❤

Screenshot (67)

@steveward
Copy link
Member

@Dtech-Dbug it sounds like there may have been some corruption in your repository that was causing this issue (which is why a fresh repository got things working). In the future you can get some further diagnostic information by running the command git fsck inside of your repository from the command line.

@Dtech-Dbug
Copy link

@steveward Thanks for your response, Steve. ❤
I have had encountered this issue, a lot of times ever since the last post here.
I will try following what you suggested, the next time I encounter the same.

Meanwhile, here is another way that could be helpful :

  • Delete the directory from the desktop client.
  • Clone the original repo from Github to a fresh new folder and continue where you last left off[The last commit to the origin]

@ivan-iljin
Copy link

ivan-iljin commented Feb 14, 2022

Corruptions, from my experience, mostly occur when the writing process gets stopped while trying to write.
I get this kind of errors when I hard-kill my machines. About thrice a year, when I'm tired and forget to close all the editors and tools. If You also work in virtual machines, try not killing them while editors and tools are open.

If you don't, then it might be some physical source of unexpected shut down. @Dtech-Dbug @cfandel I wonder how often do you guys experience power outages. Or maybe the machines get shut down by somebody forcefully?

Nobody deserves to have such a struggle so often, it's inhumane working conditions.

Try resolving this by adding a reserve power supply. Maybe a centralized server for remote working stations, located somewhere with more stable electricity, would solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests