[git] Remote origin already exists on 'git push' to a new repository

I have my project on GitHub at some location, [email protected]:myname/oldrep.git.

Now I want to push all my code to a new repository at some other location, [email protected]:newname/newrep.git.

I used the command:

git remote add origin [email protected]:myname/oldrep.git

but I am receiving this:

fatal: remote origin already exists.

This question is related to git github git-push git-remote

The answer is


If you have mistakenly named the local name as "origin", you may remove it with the following:

git remote rm origin

The previous solutions seem to ignore origin, and they only suggest to use another name. When you just want to use git push origin, keep reading.

The problem appears because a wrong order of Git configuration is followed. You might have already added a 'git origin' to your .git configuration.

You can change the remote origin in your Git configuration with the following line:

git remote set-url origin [email protected]:username/projectname.git

This command sets a new URL for the Git repository you want to push to. Important is to fill in your own username and projectname


git remote rm origin
git remote add origin [email protected]:username/myapp.git

First, write this line of code for removed remote:

$ git remote rm origin

and then write this line:

$ git remote add origin https://github.com/khadim321/React-Form.git

It's working properly.


This can also happen when you forget to make a first commit.


if you want to create a new repository with the same project inside the github and the previous Remote is not allowing you to do that in that case First Delete That Repository on github then you simply need to delete the .git folder C:\Users\Shiva\AndroidStudioProjects\yourprojectname\.git delete that folder,(make sure you click on hidden file because this folder is hidden )

Also click on the minus(Remove button) from the android studio Setting->VersionControl click here for removing the Version control from android And then you will be able to create new Repository.


You should change the name of the remote repository to something else.

git remote add origin [email protected]:myname/oldrep.git

to

git remote add neworigin [email protected]:myname/oldrep.git

I think this should work.

Yes, these are for repository init and adding a new remote. Just with a change of name.


I got the same issue, and here is how I fixed it after doing some research:

  1. Download GitHub for Windows or use something similar, which includes a shell
  2. Open the Git Shell from task menu. This will open a power shell including Git commands.
  3. In the shell, switch to your old repository, e.g. cd C:\path\to\old\repository
  4. Show status of the old repository

  5. Now remove the remote repository from local repository by using

    git remote rm origin
    
  6. Check again with step 4. It should show origin only, instead of the fetch and push path.

  7. Now that your old remote repository is disconnected, you can add the new remote repository. Use the following to connect to your new repository.

Note: In case you are using Bitbucket, you would create a project on Bitbucket first. After creation, Bitbucket will display all required Git commands to push your repository to remote, which look similar to the next code snippet. However, this works for other repositories, too.

cd /path/to/my/repo # If haven't done yet
git remote add mynewrepo https://[email protected]/team-or-user-name/myproject.git
git push -u mynewrepo master # To push changes for the first time

That's it.


I had the same problem when I first set up using Bitbucket.

My problem was that I needed to change the word origin for something self-defined. I used the name of the application. So:

git remote add AppName https://[email protected]/somewhere/something.git

You need to check the origin and add if not exists.

if ! git config remote.origin.url >/dev/null; then
    git remote add origin [email protected]:john/doe.git
fi

Create file check.sh, paste the script update your git repository URL and run ./check.sh.


  1. git remote rm origin

  2. git remote -v It will not display any repository name

  3. git remote add origin [email protected]:username/myapp.git

  4. git push origin master It will start the process and creating the new branch. You can see your work is pushed to github.


Step:1

git remote rm origin

Step:2

git remote add origin enter_your_repository_url

Example:

git remote add origin https://github.com/my_username/repository_name.git

I had the same issue but I found the solution to it. Basically "origin" is another name from where your project was cloned. Now the error

fatal: remote origin already exists.

LITERALLY means origin already exists. And hence to solve this issue, our goal should be to remove it. For this purpose:

git remote rm origin

Now add it again

git remote add origin https://github.com/__enter your username here__/__your repositoryname.git__

This did fix my issue.


I just faced this issue myself and I just removed it by removing the origin. the origin is removed by this command

git remove rm origin

try implementing this command.


You don't have to remove your existing "origin" remote, just use a name other than "origin" for your remote add, e.g.

git remote add github [email protected]:myname/oldrep.git


METHOD1->

Since origin already exist remove it.

git remote rm origin
git remote add origin https://github.com/USERNAME/REPOSITORY.git

METHOD2->

One can also change existing remote repository URL by ->git remote set-url

If you're updating to use HTTPS

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

If you're updating to use SSH

git remote set-url origin [email protected]:USERNAME/REPOSITORY.git

If trying to update a remote that doesn't exist you will receive a error. So be careful of that.

METHOD3->

Use the git remote rename command to rename an existing remote. An existing remote name, for example, origin.

git remote rename origin startpoint
# Change remote name from 'origin' to 'startpoint'

To verify remote's new name->

git remote -v

If new to Git try this tutorial->

TRY GIT TUTORIAL


You can simply edit your configuration file in a text editor.

In the ~/.gitconfig you need to put in something like the following:

[user]
        name  = Uzumaki Naruto
        email = [email protected]

[github]
        user = myname
        token = ff44ff8da195fee471eed6543b53f1ff

In the oldrep/.git/config file (in the configuration file of your repository):

[remote "github"]
        url = [email protected]:myname/oldrep.git
        push  = +refs/heads/*:refs/heads/*
        push  = +refs/tags/*:refs/tags/*

If there is a remote section in your repository's configuration file, and the URL matches, you need only to add push configuration. If you use a public URL for fetching, you can put in the URL for pushing as 'pushurl' (warning: this requires the just-released Git version 1.6.4).


You could also change the repository name you wish to push to in the REPOHOME/.git/config file

(where REPOHOME is the path to your local clone of the repository).


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to git-push

Fix GitLab error: "you are not allowed to push code to protected branches on this project"? ! [rejected] master -> master (fetch first) Git - What is the difference between push.default "matching" and "simple" error: src refspec master does not match any Issue pushing new code in Github Can't push to GitHub because of large file which I already deleted Changing the Git remote 'push to' default What does '--set-upstream' do? Git push hangs when pushing to Github? fatal: 'origin' does not appear to be a git repository

Examples related to git-remote

Why does Git tell me "No such remote 'origin'" when I try to push to origin? Git - What is the difference between push.default "matching" and "simple" error: src refspec master does not match any How to connect to a remote Git repository? Changing the Git remote 'push to' default What does '--set-upstream' do? Git: How to remove remote origin from Git repo Git push error: "origin does not appear to be a git repository" How to add a local repo and treat it as a remote repo Git branching: master vs. origin/master vs. remotes/origin/master