GitHub "fatal: remote origin already exists"
Asked Answered
M

26

650

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

I signed up at GitHub, issued a new SSH key, and created 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 [email protected]:ppreyer/first_app.git
fatal: remote origin already exists.

Just wondered if anybody else has run across this problem?

Marinamarinade answered 5/6, 2012 at 20:21 Comment(4)
If you yet created the git repository, look at the .git/config file to see if origin isn't yet declared.Mezzotint
I was facing the same problem and used the command "git remote rm origin" then use the command git remote add origin URL.Carltoncarly
@AkhzarNazir please add this as an answer. It worked for me.Orpha
I removed my 2 factor authentication and it worked for me.Slump
F
1222

Short version:

You should just update the existing remote:

git remote set-url origin [email protected]: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 [email protected]: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 already existing origin remote 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 [email protected]:ppreyer/first_app.git
Floatstone answered 5/6, 2012 at 20:28 Comment(6)
Why would origin exist in his new repository? He should fix the problem (ensuring for example origin is the correct alias to github) instead of creating a new alias he would have to remind.Mezzotint
Ok that worked but then when I go to the next step and enter git push -u origin master into the terminal I get the following error: ERROR: Repository not found. fatal: The remote end hung up unexpectedly What does that mean?Marinamarinade
What does git remote -v outputs? Also, can you try git push origin master?Floatstone
git remote rm origin did not quite work. It produced error: Could not remove config section 'remote.origin'Truelove
@zespri: use git remote set-url --add origin [email protected]/username/repo.gitMiscellany
@DenysSéguret he could have added e.g. a https with basic auth, but then decided to go through the SSH path, this would effectively create an "origin" even with failed auth.Dermatoplasty
R
491

This worked:

git remote rm origin
git remote add origin [email protected]:username/myapp.git
Renascent answered 30/10, 2014 at 11:30 Comment(3)
can anyone explain this? git remote rm originFounder
@KickButtowski rm is in reference to the remove command used in *nix. So this command tells git to "remove" the remote server details named "origin".Betelgeuse
No need to do this in 2 steps when you can do it in 1. See my answer.Floatstone
S
60

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!

Servia answered 3/11, 2014 at 7:53 Comment(2)
Why to remove origin ? Is there any way we can add anything instead of origin?Campstool
git remote rename [current name] [new name]Walther
A
41

You can see what remote repositories you are configured to connect to via

git remote -v

That will return a list in this format:

origin  [email protected]:github/git-reference.git (fetch)
origin  [email protected]:github/git-reference.git (push)

That might help you figure out what the original 'origin' pointed to.

If you want to keep the remote connection that you see with the -v, but still want to follow the Rails tutorial without having to remember 'github' (or some other name) for your tutorial's repo, you can rename your other repository with the command:

git remote rename [current name] [new name]

as in:

git remote rename origin oldrepo

You should then be able to resume your tutorial.

Angulation answered 28/8, 2013 at 20:26 Comment(0)
M
37

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.

Miscellany answered 3/4, 2014 at 1:33 Comment(4)
This worked for me. Thanks @eduncan911. The github instructions state to do a "git init" to create a new repository on the command line. This creates an origin remote with no URL using that PS interface.Shetrit
Ah yah, that's another gotcha and why you have to do this. +1 Maybe we should report this to POSH Git on github.Miscellany
How do i find the url when using gitlab?Darcie
@MennoGouw I think that's an off-topic question; but, usually it's on the Repo's dashboard/home. It's also in the settings for the project as well.Miscellany
L
33

In the special case that you are creating a new repository starting from an old repository that you used as template (Don't do this if this is not your case). Completely erase the git files of the old repository so you can start a new one:

rm -rf .git

And then restart a new git repository as usual:

git init
git add whatever.wvr ("git add --all" if you want to add all files)
git commit -m "first commit"
git remote add origin [email protected]:ppreyer/first_app.git
git push -u origin master
Lastminute answered 21/6, 2016 at 19:52 Comment(0)
P
24

update the origin if it exist already using this command

git remote set-url origin https://github.com/SriramUmapathy/ReduxLearning.git
Papoose answered 28/3, 2020 at 3:52 Comment(1)
Thank-you. You saved me a lot of time.Fernyak
F
15

If you need to check which remote repos you have connected with your local repos, theres a cmd:

git remote -v

Now if you want to remove the remote repo (say, origin) then what you can do is:

git remote rm origin
Figural answered 7/1, 2015 at 5:49 Comment(4)
This looks more like a comment than an answer. With a bit more rep, you will be able to post comments.Unconstitutional
@NathanTuggy No, it's a complete answer, and in my humble opinion, more direct than the most popular one on this thread.Lw
@daOnlyBG: I'm not sure what the answer looked like when I saw it, since that was before comments locked in grace periods (and it may therefore have been silently edited). But now, it's reasonably complete, I agree.Unconstitutional
@NathanTuggy Woah, didn't know there were silent edits once upon a time. That explains.Lw
D
13

I am facing issue in Bitbucket while push the code in remote

Follow below steps:

Step-1: Update the existing remote

git remote set-url origin https://[email protected]/pratik/demoapp.git

Step-2: You can use this command to force changes to the server with the local repository. Remote repo code will be replaced with your local repo code.

git push -f origin master

-f Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it.

Duston answered 26/1, 2022 at 8:31 Comment(0)
E
9

That error message indicates that you already have a remote in your git directory. If you are satisfied with that remote, your can push your code. If not or if you can't push just:

git remote remove origin
git remote add origin [email protected]:ppreyer/first_app.git

Voilà !

Economic answered 11/12, 2015 at 0:31 Comment(0)
P
9

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.

Pelkey answered 29/4, 2016 at 21:9 Comment(1)
great explanation in details! exactly what I was looking for. underrated answer imoSinclair
L
6

if you already add project for another storage, like you upload to github and then you upload to bitbucket then it shows this type of Error.

How to remove Error: delete git-hub file in your project and then repeat the following steps...

git init       
git remote add origin [email protected]:Yourname/firstdemotry.git  
git add -A  
git commit -m 'Message'  
git push -u origin master  
Ljoka answered 15/7, 2015 at 7:6 Comment(0)
S
6
  • $ git remote add origin [email protected]: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 [email protected]:abc/backend/abc.git (fetch) origin [email protected]:abc/backend/abc.git (push) origin1 [email protected]:abc/backend/abc.git (fetch) origin1 [email protected]: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 [email protected]: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 [email protected]:abc/backend/abc.git (fetch) destination [email protected]: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

Styrax answered 29/1, 2018 at 6:7 Comment(0)
P
5

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

git init
Pietro answered 8/3, 2017 at 9:35 Comment(0)
C
5

facing same error while add repository to git hun using git bash on windows

 git remote add origin https://github.com/axaysushir/netflix_page_clone.git

fatal: remote origin already exists.

fatal: remote origin already exists.

 ! [rejected]        master -> master (fetch first)

error: failed to push some refs to 'https://github.com/axaysushir/meditation_app_using_js.git'

Update repository by following command

$ git remote set-url origin https://github.com/axaysushir/netflix_page_clone.git

then add repository using git remote add github instead git remote add origin

$ git remote add github https://github.com/axaysushir/netflix_page_clone.git

And then write following command instead of git push origin master this will upload your repository to github

$ git push github master
Chemisorption answered 3/12, 2019 at 15:38 Comment(0)
S
4

Try this

  • cd existing_repo
  • git remote rename origin old-origin
Sonatina answered 27/9, 2018 at 16:31 Comment(1)
Bro thanks, this is the only thing that worked for me.Shrug
U
3

for using git you have to be

root

if not then use sudo

for removing origin :

git remote remove origin

for adding origin :

git remote add origin http://giturl

Unionism answered 4/2, 2016 at 8:35 Comment(0)
C
3

If you are getting an error like "error: remote origin already exists." then please try following command to remove already existing remote origin

git remote remove origin

then use your commmand

git remote add origin [email protected]:ppreyer/first_app.git

If you don't want to delete existing remote record then update it

git remote set-url <REMOTE-NAME> <NEW-URL>

then use your commmand

git remote add origin [email protected]:ppreyer/first_app.git

If you want to rename your existing project use following command

git remote rename <old-name> <new-name>

if you want to rename your origin remote to backup. You'd simply run:

git remote rename origin backup

Thank you!!

Cureall answered 24/12, 2022 at 18:33 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Archambault
Z
1

Please replace "add" in "git remote add origin [email protected]" with "set-url" in git remote set-url origin [email protected]

Zwick answered 21/4, 2022 at 13:18 Comment(0)
J
0

First check To see how many aliases you have and what are they, you can initiate this command git remote -v

Then see in which repository you are in then try git remote set-url --add [Then your repositpory link] git push -u origin master

Jazzman answered 30/3, 2018 at 15:16 Comment(0)
F
0

On bash at least, we can force the return value of the exit code of the command to be 0

You can remove the old remote and add it again

git remote remove $1 || true
git remote add $1 $2
Faze answered 14/3, 2019 at 12:25 Comment(0)
M
0

In case you want to do via GUI do the following:

  1. Ensure "hidden files" are visible in your project folder
  2. Go to .git directory
  3. Edit the url file in the config.txt file and save the file!
Matilda answered 26/9, 2019 at 20:52 Comment(0)
O
0

Do not pay attention to the error and write this:

git push -u origin main

Office answered 6/10, 2021 at 19:0 Comment(0)
N
0
git remote set-url origin https://username:[email protected]:ppreyer/first_app.git
git push -u origin main

You can generate personal accesss token using following steps on github:

  • Goto to settings
  • Click on Developer settings
  • Click on Generate new token
  • Generate your personal access token and paste it on the above command. :)
Ninette answered 23/9, 2022 at 16:21 Comment(0)
I
0

Very simple solution for this error :- error: remote origin already exists. Might be you hit this command

git remote add origin https://github.com/rahulgupta020/cardorganizer.git

To change this command add to set-url

git remote set-url origin https://github.com/rahulgupta020/cardorganizer.git

Irreformable answered 30/10, 2023 at 8:26 Comment(0)
G
-2

try this command it works for me.

rm -rf .git/

Gesture answered 8/6, 2022 at 4:35 Comment(1)
This will kill your repo. Do not do it.Den

© 2022 - 2024 — McMap. All rights reserved.