git - remote add origin vs remote set-url origin

git add remote branch
git remove remote
git remote origin
git remote add origin fatal: remote origin already exists
git remote add upstream
no such remote 'origin
git change remote branch
git add remote not found

I create a new repository:

git init
echo "# MESSAGE" >> README.md
git add README.md
git commit -m "first commit"

Then I want to push my commit to the empty remote repository created on github so I have to set remote.

What is difference between using following commands ? :

git remote add origin git@github.com:User/UserRepo.git
git remote set-url origin git@github.com:User/UserRepo.git

At the end I perform push:

git push -u origin master

Edit1:

What happens when I call remote set-url origin just after git init ? Does remote set-url origin create origin ? If origin already exists after git init there is no difference between using those commands in my scenario, right ?

below is used to a add a new remote:

git remote add origin git@github.com:User/UserRepo.git

below is used to change the url of an existing remote repository:

git remote set-url origin git@github.com:User/UserRepo.git

below will push your code to the master branch of the remote repository defined with origin and -u let you point your current local branch to the remote master branch:

git push -u origin master

Documentation

git - remote add origin vs remote set-url origin -git add , The git remote set-url command changes an existing remote repository URL. git remote set-url origin https://github.com/USERNAME/REPOSITORY.git You can also change the remote's URL by editing the .git/config file with a text editor. However, it is recommended to use the git command. Changing a Git remote's URL is as simple as running: git remote set-url <remote-name> <remote-url>. If you hit a problem or have feedback, leave a comment below.

Changing a remote's URL, git remote [-v | --verbose] git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags​] git remote get-url [--push] [--all] <name> git remote set-url [--push] <name> For example, if the default branch for origin is set to master , then origin may be  The above will only work if you've either cloned the repository or manually added a remote called origin. If "git remote -v" doesn't show you any remotes you can simply add a remote using: git remote add origin https: //u sername@stash /scm/ PROJECT /repo.git. The name of the remote doesn't have to be "origin" and can be any name that makes it

Below will reinitialize your local repo; also clearing remote repos (ie origin):

git init

Then below, will create 'origin' if it doesn't exist:

git remote add origin [repo-url]

Else, you can use the set-url subcommand to edit an existing remote:

git remote set-url origin [repo-url]

Also, you can check existing remotes with

git remote -v

Hope this helps!

git-remote Documentation, A remote URL, which you can find on the Source sub-tab of your Git repo. For example: #set a new remote git remote add my_awesome_new_remote_repo git​@  To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at.

git remote add => ADDS a new remote.

git remote set-url => UPDATES existing remote.


  1. The remote name that comes after add is a new remote name that did not exist prior to that command.
  2. The remote name that comes after set-url should already exist as a remote name to your repository.

git remote add myupstream someurl => myupstream remote name did not exist now creating it with this command.

git remote set-url upstream someurl => upstream remote name already exist i'm just changing it's url.


git remote add myupstream https://github.com/nodejs/node => **ADD** If you don't already have upstream
git remote set-url upstream https://github.com/nodejs/node # => **UPDATE** url for upstream

How to Add a New Remote to your Git Repo, If you created the repository locally, you can add a new remote. The remote git remote set-url origin git@gitserver.com:user/repo_name.git. Pushing to Your Remotes. When you have your project at a point that you want to share, you have to push it upstream. The command for this is simple: git push <remote> <branch> . If you want to push your master branch to your origin server (again, cloning generally sets up both of those names for you automatically),

To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at.

The git remote set-url command changes an existing remote repository URL.

So basicly, remote add is to add a new one, remote set-url is to update an existing one

How to Change a Git Remote's URL, git remote add origin git@github.com:scottwrobinson/camo.git <repo-url-for-​fetching> $ git remote set-url --push <remote-name> <repo-url-for-pushing>. git remote add => ADDS a new remote. git remote set-url => UPDATES existing remote. The remote name that comes after add is a new remote name that did not exist prior to that command. The remote name that comes after set-url should already exist as a remote name to your repository.

Git: Add New Remote to a Repo, While attempting to push a local file into a repo in my GitHub I. please find the most recent attempt through the command line and not through VS Code git remote add origin <url> set a remote called origin with some url  In theory, you could remove the `origin` remote and then re-add it, but instead of doing that in two steps, you can actually knock it out in just one with the `set-url` command that I showed you at the top of this article. In one step, this command will update the URL of a repository for the given name you pass in.

unable to push first attempt from local machine to, git remote origin $ git remote -v origin git@github.com:github/git-reference.git (​fetch) origin You do that by running git remote add [alias] [url] . Internally, the git remote set-url command calls git config remote , but has the added benefit of  To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A remote name, for example, “origin” A remote URL, which you can find on the Source sub-tab of your Git repo. git remote add origin git@git.assembla.com :portfolio/space.space

remote, 11.6.1 HTTPS vs SSH · 11.6.2 git2r – or some other tool – can't find SSH keys on Windows · 11.6.3 Other git remote add happygit https://github.com/jennybc/​happy-git-with-r.git and clone” of a repo and your personal copy is set up as the origin remote. git remote set-url can be used to change the URL for a remote.

Comments
  • if I clone from A to my local and then use "git remote set-url B". will it delete the repository in A? I am trying to clone a repository from AWS code commit to GitLab
  • usually when I'm forking a new repo, I make a mistake and set the origin to the upstream. End up needing to correct it using the 2nd command git remote set-url origin git@github.com:User/UserRepo.git
  • -u let you point your current local branch to the remote master branch I don't get why I'd want to do such. I mean let's say I pulled from master, created a new featureBranch...committed my changes and then pushed my changes to origin/featureBranch and then I merged/pull that feature into my master. <— at this moment am I not done with my featureBranch? Why would I need it to point to remote master branch? Shouldn't I checkout to local/master and then pull from latest origin?
  • The answer is very good, except the explanation of -u flag, which is, in my opinion, misleading. For explanation of -u flag, I would recommend to have a look at this thread stackoverflow.com/questions/18867824/…
  • Please see Edit1
  • git init doesn't add any origin. Only git repository will be initialize. If you clone any existing repository then it has a remote origin. Recommendation is use git add, after git init not set-url.
  • @Ram It is obvious not to call set-url after git init as it not make sense. set-url is to change and add is to add new remote.
  • git remote set-url origin ... on a newly init'd repo got me the message fatal: No such remote 'origin'. git remote add origin ... worked.
  • @RobbVandaveer Thanks for the catch! I updated the answer for the correct use of subcommand set-url