Github “fatal: remote origin already exists”

2019-01-03 04:05发布

I am trying to follow along Michael Hartl's Rails tutorial but I've run across an error.

I signed up on Github and issued a new SSH key and made a new repository. But when I enter the next line into the terminal I get the following error:

Parkers-MacBook-Pro:.ssh ppreyer$ git remote add origin git@github.com:ppreyer/first_app.git
fatal: remote origin already exists.

Just wondered if anybody else has run across this problem?

15条回答
干净又极端
2楼-- · 2019-01-03 04:22
  • $ git remote add origin git@gitlab.com:abc/backend/abc.git

    In this command origin is not part of command it is just name of your remote repository. You can use any name you want.

    • First You can check that what it contains using below command

    $ git remote -v

    It will gives you result like this origin git@gitlab.com:abc/backend/abc.git (fetch) origin git@gitlab.com:abc/backend/abc.git (push) origin1 git@gitlab.com:abc/backend/abc.git (fetch) origin1 git@gitlab.com:abc/backend/abc.git (push)

    if it contains your remote repository path then you can directly push to that without adding origin again

    • If it is not contaning your remote repository path

    Then you can add new origin with different name and use that to push like $ git remote add origin101 git@gitlab.com:abc/backend/abc.git

    Or you can rename existing origin name add your origin

    git remote rename origin destination

    fire below command again

    $ git remote -v

    destination git@gitlab.com:abc/backend/abc.git (fetch) destination git@gitlab.com:abc/backend/abc.git (push)

    It will change your existing repos name so you can use that origin name

    Or you can just remove your existing origin and add your origin

    git remote rm destination

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-03 04:25

First do a:

git remote rm origin

then

git remote add origin https://github.com/your_user/your_app.git

and voila! Worked for me!

查看更多
倾城 Initia
4楼-- · 2019-01-03 04:28

The concept of remote is simply the URL of your remote repository.

The origin is an alias pointing to that URL. So instead of writing the whole URL every single time we want to push something to our repository, we just use this alias and run:

git push -u origin master

Telling to git to push our code from our local master branch to the remote origin repository.

Whenever we clone a repository, git creates this alias for us by default. Also whenever we create a new repository, we just create it our self.

Whatever the case it is, we can always change this name to anything we like, running this:

git remote rename [current-name] [new-name]

Since it is stored on the client side of the git application (on our machine) changing it will not affect anything in our development process, neither at our remote repository. Remember, it is only a name pointing to an address.

The only thing that changes here by renaming the alias, is that we have to declare this new name every time we push something to our repository.

git push -u my-remote-alias master

Obviously a single name can not point to two different addresses. That's why you get this error message. There is already an alias named origin at your local machine. To see how many aliases you have and what are they, you can initiate this command:

git remote -v

This will show you all the aliases you have plus the corresponding URLs.

You can remove them as well if you like running this:

git remote rm my-remote-alias

So in brief:

  • find out what do you have already,
  • remove or rename them,
  • add your new aliases.

Happy coding.

查看更多
疯言疯语
5楼-- · 2019-01-03 04:29

TL;DR you should just update the existing remote:

$ git remote set-url origin git@github.com:ppreyer/first_app.git

Long version:

As the error message indicates, there is already a remote configured with the same name. So you can either add the new remote with a different name or update the existing one if you don't need it:

To add a new remote, called for example github instead of origin (which obviously already exists in your system), do the following:

$ git remote add github git@github.com:ppreyer/first_app.git

Remember though, everywhere in the tutorial you see "origin" you should replace it with "github". For example $ git push origin master should now be $ git push github master.

However, if you want to see what that origin which already exists is, you can do a $ git remote -v. If you think this is there by some error, you can update it like so:

$ git remote set-url origin git@github.com:ppreyer/first_app.git
查看更多
做自己的国王
6楼-- · 2019-01-03 04:29

It can also happen if you run the command in directory without git initialized. If that's the case run first:

git init
查看更多
虎瘦雄心在
7楼-- · 2019-01-03 04:30

For those of you running into the ever so common error "fatal: remote origin already exists.", or when trying to remove origin and you get "error: could not remove config section remote.origin", what you need to do is to set the origin manually.

Window's POSH~Git for Windows PowerShell (and GitHub for Windows' app) has a problem with this.

I ran into this, like I do so often, again when setting up my octopress. So, here's how I got it working.

First, check your remotes:

C:\gd\code\octopress [source +2 ~3 -0 !]> git remote -v
octopress       https://github.com/imathis/octopress.git (fetch)
octopress       https://github.com/imathis/octopress.git (push)
origin

You'll first note that my origin has no url. Any attempt to remove it, rename it, etc all fails.

So, change the url manually:

git remote set-url --add origin https://github.com/eduncan911/eduncan911.github.io.git

Then you can confirm it worked by running git remote -v again:

C:\gd\code\octopress [source +2 ~3 -0 !]> git remote -v
octopress       https://github.com/imathis/octopress.git (fetch)
octopress       https://github.com/imathis/octopress.git (push)
origin  https://github.com/eduncan911/eduncan911.github.io.git (fetch)
origin  https://github.com/eduncan911/eduncan911.github.io.git (push)

This has fixed dozens of git repos I've had issues with, GitHub, BitBucket GitLab, etc.

查看更多
登录 后发表回答