git - 远程添加源与远程 set-url 源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42830557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:52:55  来源:igfitidea点击:

git - remote add origin vs remote set-url origin

gitgithub

提问by Irbis

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.

然后我想将我的提交推送到在 github 上创建的空远程存储库,所以我必须设置远程。

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

Edit1:

编辑1:

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 ?

当我在 git init 之后调用 remote set-url origin 时会发生什么?远程 set-url origin 是否创建 origin ?如果在 git init 之后 origin 已经存在,那么在我的场景中使用这些命令没有区别,对吧?

回答by Shubham Khatri

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:

下面用于更改现有远程存储库的 url:

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 originand -ulet you point your current local branch to the remote master branch:

下面会将您的代码推送到定义的远程存储库的 master 分支,origin-u让您将当前本地分支指向远程 master 分支:

git push -u origin master

Documentation

文档

回答by Ram

  • 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 originand 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'.
  • 当您运行时git remote add origin [email protected]:User/UserRepo.git,会创建一个名为origin.
  • 当您运行时git remote set-url origin [email protected]:User/UserRepo.git,git 搜索具有名称的现有远程origin并更改它的远程存储库 url。如果 git 找不到任何具有 name 的遥控器,origin则会引发错误fatal: No such remote 'origin'

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

如果您要创建一个新的存储库,请使用git remote add origin [email protected]:User/UserRepo.git添加远程。

回答by vpibano

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

下面将重新初始化您的本地存储库;还清除远程仓库(即来源):

git init

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

然后在下面,如果它不存在,将创建'origin':

git remote add origin [repo-url]

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

否则,您可以使用set-url子命令来编辑现有的遥控器:

git remote set-url origin [repo-url]

Also, you can check existing remotes with

此外,您可以检查现有的遥控器

git remote -v

Hope this helps!

希望这可以帮助!

回答by Tomer Ben David

git remote add=> ADDSa new remote.

git remote add=> ADDS新的远程。

git remote set-url=> UPDATESexisting remote.

git remote set-url=>更新现有的遥控器。



  1. The remote name that comes after addis a new remote name that did not exist prior to that command.
  2. The remote name that comes after set-urlshould already exist as a remote name to your repository.
  1. 后面add的远程名称是在该命令之前不存在的新远程名称。
  2. 后面的远程名称set-url应该已经作为存储库的远程名称存在。


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

git remote add myupstream someurl=> myupstream 远程名称不存在,现在使用此命令创建它。

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

git remote set-url upstream someurl=> 上游远程名称已经存在,我只是在更改它的 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

回答by Murf

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

要添加新的遥控器,请git remote add在终端上使用命令,在您的存储库所在的目录中。

The git remote set-urlcommand changes an existing remote repository URL.

git remote set-url命令更改现有的远程存储库 URL。

So basicly, remote addis to add a new one, remote set-urlis to update an existing one

所以基本上,remote add就是添加一个新的,remote set-url就是更新一个现有的

回答by Tahir77667

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

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 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

    ORgit remote get-url origin

  • 此命令是使用 .git 将 git 初始化到当前工作存储库后的命令系列中的第二步git init
  • 此命令仅表示“您正在添加远程存储库的位置,您希望将文件推入/拉出的位置!!...”
  • 你的远程仓库可以在 github、gitlab、bitbucket 等的任何地方。
  • origin是远程存储库的别名/备用名称,这样您就不必每次都键入 remote 的完整路径,此后您声明将使用此名称(来源)来引用您的遥控器。这个名字可以是任何名字。
  • 要验证遥控器是否设置正确,请键入: git remote -v

    或者git remote get-url origin

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

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

This command means that if accidently you happen to push to a wrong repository the first time, you can "reset your remote repository path"by using the above command.

这个命令意味着如果你不小心第一次推送到错误的存储库,你可以使用上面的命令“重置你的远程存储库路径”

3. git push -u remote master

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 masterbranch unless explicitly specified an alternate branch.

此命令只是将您的文件推送到远程存储库。Git 有一个称为“分支”的概念,因此默认情况下,除非明确指定备用分支,否则所有内容都会推送到分支。

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

要了解存储库中所有分支的列表,请键入:git branch

回答by Tal Hakmon

Try this:

尝试这个:

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

回答by Haritsinh Gohil

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

您不能remote set-url origin在 之后立即调用git init,因为该git remote set-url命令不会创建 origin,但它会更改现有的远程存储库 URL

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

因此该命令git remote set-url仅在您克隆存储库或手动添加名为 origin 的远程时才有效。

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

您可以使用命令检查远程,git remote -v它会在名称后显示远程 url,或者如果此命令给出错误,fatal: Not a git repository (or any of the parent directories): .git则存储库不存在,因此您必须使用命令添加原点git remote add

1. 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:

git remote add 命令有两个参数:

  1. A remote name, for example, origin
  2. A remote URL, for example, https://github.com/user/repo.git
  1. 一个远程名称,例如,起源
  2. 远程URL,例如,https://github.com/user/repo.git

For example:

例如:

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

2.git remote set-url

2.git remote set-url

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

git remote set-url 命令更改现有的远程存储库 URL。

The git remote set-url command takes two arguments:

git remote set-url 命令有两个参数:

  1. An existing remote name. For example, originor upstreamare two common choices.
  2. A new URL for the remote
  1. 一个现有的远程名。例如,originupstream是两个常见的选择。
  2. 一个远程新的URL

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

例如,您可以使用以下git remote set-url命令将远程的 URL 从 SSH 更改为 HTTPS 。

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.

您可以使用命令验证远程 URL 是否已更改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".

注意:“origin”是一个约定,不是命令的一部分。“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

来自 github 的参考:remote add, remote set-url

回答by Sumit Jaiswal

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

如果您有现有项目,并且想添加远程存储库 url,则需要执行以下命令

git init

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

如果您想添加 readme.md 文件,那么您可以创建它并使用以下命令添加它。

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.

现在您完成了所有本地存储库过程,现在如何添加远程存储库 url ?检查下面的命令,这是用于 ssh url,您可以将其更改为 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