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
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?
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
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 set-url origin ...
on a newly init'd repo got me the message fatal: No such remote 'origin'. git remote add origin ...
worked.
set-url
When you run git remote add origin git@github.com:User/UserRepo.git, then a new remote created named origin.
When you run git remote set-url origin git@github.com: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 git@github.com:User/UserRepo.git
to add remote.
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
.
1. git remote add origin git@github.com: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 git@github.com: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 origin 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
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 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
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
If you don't have a git repo already initiate one with git init
git remote -v Check if any remote already exists If Yes then use git remote set-url origin git@github.com:User/UserRepo.git to change the origin If No then use git remote add origin git@github.com:User/UserRepo.git to set new origin for your repo. 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.
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:
A remote name, for example, origin 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:
An existing remote name. For example, origin or upstream are two common choices. 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 git@github.com:user/repo.git
git remote set-url myorigin https://github.com/user/repo.git
References from github: remote add, remote set-url
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 git@github.com:user-name/repository-name.git
How you push your first commit see below command :
git push -u origin master
Success story sharing
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?-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/…