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 :User/UserRepo.git git remote set-url origin :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?
10 Answers
below is used to a add a new remote:
git remote add origin :User/UserRepo.git below is used to change the url of an existing remote repository:
git remote set-url origin :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 7Below 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!
2- When you run
git remote add origin :User/UserRepo.git, then a new remote created namedorigin. - When you run
git remote set-url origin :User/UserRepo.git,git searches for existing remote having nameoriginand change it's remote repository url. If git unable to find any remote having nameorigin, It raise an errorfatal: No such remote 'origin'.
If you are going to create a new repository then use git remote add origin :User/UserRepo.git to add remote.
1. git remote add origin :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
originis 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 :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
addis a new remote name that did not exist prior to that command. - The remote name that comes after
set-urlshould 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 => **ADD** If you don't already have upstream git remote set-url upstream # => **UPDATE** url for upstream This is very simple If you have already set a remote origin url then you use
set-urlcommand to change that, otherwise simply useaddcommand
If you don't have a git repo already initiate one with git init
git remote -vCheck if any remote already exists- If Yes then use
git remote set-url origin :User/UserRepo.gitto change the origin - If No then use
git remote add origin :User/UserRepo.gitto set new origin for your repo. - and finally use
git push -u origin masterto 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:
For example:
git remote add origin 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,
originorupstreamare 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 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 :user/repo.git git remote set-url myorigin 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 :user-name/repository-name.git How you push your first commit see below command :
git push -u origin master