[git] git - remote add origin vs remote set-url origin

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 [email protected]:User/UserRepo.git
git remote set-url origin [email protected]:User/UserRepo.git

At the end I perform push:

git push -u origin master

What happens when I call git remote set-url origin just after git init? Does git 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?

This question is related to git github

The answer is


below is used to a add a new remote:

git remote add origin [email protected]:User/UserRepo.git

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

git remote set-url origin [email protected]: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


if you have existing project and you would like to add remote repository url then you need to do following command

git init

if you would like to add readme.md file then you can create it and add it using below command.

git add README.md

make your first commit using below command

git commit -m "first commit"

Now you completed all local repository process, now how you add remote repository url ? check below command this is for ssh url, you can change it for https.

git remote add origin [email protected]:user-name/repository-name.git

How you push your first commit see below command :

git push -u origin master

Try this:

git init  
git remote add origin your_repo.git  
git remote -v  
git status  

You can not call remote set-url origin just after git init, Because the git remote set-url command will not create origin, but it changes an existing remote repository URL.

so the command git remote set-url will only work if you've either cloned the repository or manually added a remote called origin.

you can check remote with command git remote -v it will show remote url after name, or if this command gives error like fatal: Not a git repository (or any of the parent directories): .git then the repository not exists, so you have to add origin with command git remote add

1. git remote add

This command is used to add a new remote, you can use this command on the terminal, in the directory of your repository.

The git remote add command takes two arguments:

  1. A remote name, for example, origin
  2. A remote URL, for example, https://github.com/user/repo.git

For example:

git remote add origin https://github.com/user/repo.git

2.git remote set-url

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

The git remote set-url command takes two arguments:

  1. An existing remote name. For example, origin or upstream are two common choices.
  2. A new URL for the remote

For example you can change your remote's URL from SSH to HTTPS with the git remote set-url command.

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

you can verify that the remote URL has changed, with command git remote -v.

note: "origin" is a convention not part of the command. "origin" is the local name of the remote repository. you can use any name instead of "origin".

For example:

git remote add myorigin [email protected]:user/repo.git
git remote set-url myorigin https://github.com/user/repo.git

References from github: remote add, remote set-url


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!


This is very simple If you have already set a remote origin url then you use set-url command to change that, otherwise simply use add command

  1. git remote -v Check if any remote already exists
  2. If Yes then use git remote set-url origin [email protected]:User/UserRepo.git to change the origin
  3. If No then use git remote add origin [email protected]:User/UserRepo.git to set new origin for your repo.
  4. and finally use git push -u origin master to push your code to remote and add upstream (tracking) reference to your remote branch.

NOTE: If you use -u flag, its for upstream, it enables you to use simply git pull instead of git pull origin <branch-name> in upcoming operations.

Happy Coding ;)


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

1. git remote add origin [email protected]:User/UserRepo.git

  • This command is the second step in the command series after you initialize git into your current working repository using git init.
  • This command simply means "you are adding the location of your remote repository where you wish to push/pull your files to/from !!.."
  • Your remote repository could be anywhere on github, gitlab, bitbucket, etc.
  • Here origin is an alias/alternate name for your remote repository so that you don't have to type the entire path for remote every time and henceforth you are declaring that you will use this name(origin) to refer to your remote. This name could be anything.
  • To verify that the remote is set properly type : git remote -v

OR git remote get-url origin

2. git remote set-url origin [email protected]:User/UserRepo.git

This command means that if at any stage you wish to change the location of your repository(i.e if you made a mistake while adding the remote path using the git add command) the first time, you can easily go back & "reset(update) your current remote repository path" by using the above command.

3. git push -u remote master

This command simply pushes your files to the remote repository.Git has a concept of something known as a "branch", so by default everything is pushed to the master branch unless explicitly specified an alternate branch.

To know about the list of all branches you have in your repository type :git branch


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


  • When you run git remote add origin [email protected]:User/UserRepo.git, then a new remote created named origin.
  • When you run git remote set-url origin [email protected]:User/UserRepo.git,git searches for existing remote having name origin and change it's remote repository url. If git unable to find any remote having name origin, It raise an error fatal: No such remote 'origin'.

If you are going to create a new repository then use git remote add origin [email protected]:User/UserRepo.git to add remote.