136

I've a repository moodle on my Github account which I forked from the official repository.

I then cloned it on my local machine. It worked fine. I created several branches (under the master branch). I made several commits and it worked fine.

I don't know how I'm getting the following error when I do : git push origin master

fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

How do I resolve the error without effecting my repository on Github?

I'm using Ubuntu 12.10

The contents of my .git/config after doing cat $(git rev-parse --show-toplevel)/.git/config gives:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[branch "master"]
[branch "MOODLE_23_STABLE"]
[branch "MOODLE_24_STABLE"]
[remote "upstream"]
    url = git://git.moodle.org/moodle.git
    fetch = +refs/heads/*:refs/remotes/upstream/*
xan
  • 4,640
  • 13
  • 50
  • 83

12 Answers12

156

$HOME/.gitconfig is your global config for git.
There are three levels of config files.

 cat $(git rev-parse --show-toplevel)/.git/config

(mentioned by bereal) is your local config, local to the repo you have cloned.

you can also type from within your repo:

git remote -v

And see if there is any remote named 'origin' listed in it.

If not, if that remote (which is created by default when cloning a repo) is missing, you can add it again:

git remote add origin url/to/your/fork

The OP mentions:

Doing git remote -v gives:

upstream git://git.moodle.org/moodle.git (fetch) 
upstream git://git.moodle.org/moodle.git (push)

So 'origin' is missing: the reference to your fork.
See "What is the difference between origin and upstream in github"

enter image description here

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Doing `git remote -v` gives: `upstream git://git.moodle.org/moodle.git (fetch)` `upstream git://git.moodle.org/moodle.git (push)` – xan Mar 26 '13 at 13:14
  • 1
    @xan 'upstream" is the repo you have forked. You are missing origin, which is your fork on github. See http://stackoverflow.com/a/15632224/6309 and http://stackoverflow.com/a/9257901/6309 – VonC Mar 26 '13 at 13:15
  • How do I change my fork to master? – Wolfpack'08 Jan 25 '20 at 10:16
  • @Wolfpack'08 "fork" means a repository (with multiple branch). Change to a branch is a local operation which involves git switch: https://stackoverflow.com/a/57066202/6309. In your own words, what does "changing a fork to master" means to you? Changing your local branch in your local cloned repository (of your remote GitHub fork)? – VonC Jan 25 '20 at 10:53
  • 1
    If you own the remote repo ghy, simply push to it: `git push -u ghy master`. – VonC Jan 25 '20 at 11:23
  • Thanks. I figured out various mistakes I'd made and how I was picturing git incorrectly. I made some naming errors, was avoiding clone, and did some other dumb stuff. – Wolfpack'08 Jan 25 '20 at 12:34
30

I faced the same problem when I renamed my repository on GitHub. I tried to push at which point I got the error

fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

I had to change the URL using

git remote set-url origin ssh://git@github.com/username/newRepoName.git

After this all commands started working fine. You can check the change by using

git remote -v

In my case after successfull change it showed correct renamed repo in URL

[aniket@alok Android]$ git remote -v
origin  ssh://git@github.com/aniket91/TicTacToe.git (fetch)
origin  ssh://git@github.com/aniket91/TicTacToe.git (push)
javabrett
  • 7,020
  • 4
  • 51
  • 73
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • "fatal: no such remote 'origin'" – MwBakker Jan 02 '21 at 19:38
  • @MwBakker first you have to add origin with "git remote add origin ssh://git@github.com/username/newRepoName.git" and then you can check it with "git remote -v". – buck80 Jan 23 '22 at 08:02
18

It is possible the other branch you try to pull from is out of synch; so before adding and removing remote try to (if you are trying to pull from master)

git pull origin master

for me that simple call solved those error messages:

  • fatal: 'master' does not appear to be a git repository
  • fatal: Could not read from remote repository.
sivi
  • 10,654
  • 2
  • 52
  • 51
6

Try to create remote origin first, maybe is missing because you change name of the remote repo

git remote add origin URL_TO_YOUR_REPO

zdravko zdravkin
  • 2,090
  • 19
  • 21
6

I had the same issue and solved it by checking

git remote -v

git init the repo URL

git remote add origin the repo URL

git push -f origin master
Vega
  • 27,856
  • 27
  • 95
  • 103
Lucy karimi
  • 61
  • 1
  • 2
2

This does not answer your question, but I faced a similar error message but due to a different reason. Allow me to make my post for the sake of information collection.

I have a git repo on a network drive. Let's call this network drive RAID. I cloned this repo on my local machine (LOCAL) and on my number crunching cluster (CRUNCHER). For convenience I mounted the user directory of my account on CRUNCHER on my local machine. So, I can manipulate files on CRUNCHER without the need to do the work in an SSH terminal.

Today, I was modifying files in the repo on CRUNCHER via my local machine. At some point I decided to commit the files, so a did a commit. Adding the modified files and doing the commit worked as I expected, but when I called git push I got an error message similar to the one posted in the question.

The reason was, that I called push from within the repo on CRUNCHER on LOCAL. So, all paths in the config file were plain wrong.

When I realized my fault, I logged onto CRUNCHER via Terminal and was able to push the commit.

Feel free to comment if my explanation can't be understood, or you find my post superfluous.

Dohn Joe
  • 313
  • 4
  • 15
2

100% works.

First of first make sure that there is no hidden git folder inside your project root and delete it if there is one. Then open a command shell and execute the following commands:

git init -b main

git add .   //This is very important and it adds the files in the local repository and stages them for commit.

git commit -m "First commit"

git remote add origin  <REMOTE_URL>  //REMOTE_URL: the repository address in github.com 

git remote -v  //Verifies the new remote URL

git push origin main

and that's it. You can also read more about this in GitHub documentation

Ryan Didevar
  • 512
  • 4
  • 14
2

My issue was related to the way I cloned the repo. Github gave a colon before the user name, for example:

git@github.companyname.com:usernmame/reponame.git

The command git remote -v showed this:

origin git@github.companyname.com:usernmame/reponame.git (fetch)
origin git@github.companyname.com:usernmame/reponame.git (push)

Everything matched what Github was telling me, so I was confused why I was getting following error when I tried to push a new branch upstream:

fatal: 'reponame' does not appear to be a git repository

Then I realized it was because my origin was pointing to the wrong upstream because of the colon in the URL

git remote set-url origin ssh://git@github.companyname.com/username/reponame.git

Now I can push the new branch.

Update

I have found that after doing this I was no longer able to push any changes. I had to reset the origin back to the URL with the colon in order pushes to work.

git remote set-url origin git@github.companyname.com:usernmame/reponame.git

So I would say that this is a work around and not a proper solution.

Fred
  • 1,054
  • 1
  • 12
  • 32
1

I had the same error on git pull origin branchname when setting the remote origin as path fs and not ssh in .git/config:

fatal: '/path/to/repo.git' does not appear to be a git repository 
fatal: The remote end hung up unexpectedly

It was like so (this only works for users on same server of git that have access to git):

url = file:///path/to/repo.git/

Fixed it like so (this works on all users that have access to git user (ssh authorizes_keys or password)):

url = git@domain.com:path/to/repo.git

the reason I had it as a directory path was because the git files are on the same server.

Waqleh
  • 9,741
  • 8
  • 65
  • 103
0

I had the same error and solved it by editing .git/config in my repo to include:

[branch "master"]
    remote = origin
    merge = refs/heads/master

at the end of the file.

Ciryon
  • 2,637
  • 3
  • 31
  • 33
0

For me it was a RAM issue in the WSL. I had to restart WSL to make it work again. In the command prompt:

wsl --shutdown
Alin Pop
  • 317
  • 2
  • 8
0

in my case I had a typo when running 'git remote add origin ...'

Hamid Mohamadi
  • 141
  • 2
  • 4