什么是“git remote add ...”和“git push origin master”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5617211/
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-10 10:35:44  来源:igfitidea点击:

What is "git remote add ..." and "git push origin master"?

gitgithub

提问by nonopolarity

Quite often, Git and Rails looks like magic... such as in the first chapter of Rails 3 Tutorial book, it talks about Git:

很多时候,Git 和 Rails 看起来很神奇……比如在Rails 3 教程书第一章中,它谈到了 Git:

git remote add origin [email protected]:peter/first_app.git
git push origin master

and it pretty much says "it just works" without saying too much about what they are and start talking about branching. Searching on the net shows that git remote addis to add a "short name", such as origin, and it can be any name as well, which is like an alias to a URL. And originis the usual path of where the remote repo points to. (in http://git-scm.com/book/en/Git-Basics-Working-with-Remotesunder "Adding Remote Repositories")

它几乎说“它只是有效”而没有过多地说明它们是什么并开始谈论分支。网上查了一下,git remote add是加了一个“短名称”,比如origin,也可以是任意名称,相当于一个网址的别名。并且origin是远程仓库指向的通常路径。(在“添加远程存储库”下的http://git-scm.com/book/en/Git-Basics-Working-with-Remotes 中

So why is the URL not git://[email protected]/peter/first_app.gitbut in the other syntax -- what syntax is it? Why must it end with .git? I tried not using .gitat the end and it works too. If not .git, what else can it be? The gitin [email protected]seems to be a user account on the git server?

那么为什么 URL 不是, git://[email protected]/peter/first_app.git而是采用另一种语法——它是什么语法?为什么必须以 结尾.git?最后我尝试不使用.git它,它也有效。如果不是.git,还能是什么?在git[email protected]似乎是git的服务器上的用户帐户?

Also, why does it need to be so verbose to use git push origin master? Can't the default be origin and master? I found that the first time, the origin masteris needed, but after a small edit and commit, then git pushis all it needs (no need origin master). Can somebody who knows what is going on give some details?

另外,为什么使用 需要如此冗长git push origin master?不能默认是origin和master吗?我发现第一次origin master是需要的,但是经过一个小的编辑和提交后,就git push只需要它了(不需要origin master)。知道发生了什么的人可以提供一些细节吗?

Sometimes it feels like a lot of magic without explanation... and sometimes the person using it is so confident and when asked why, can't explain it, and respond with something like "that's the way it is". Sometimes very practical and pragmatic. It is not bad to be practical, but probably not practical to the point to not know what is going on.

有时感觉就像没有解释的很多魔法……有时使用它的人非常自信,当被问到为什么时,无法解释它,并回答“就是这样”。有时非常实际和务实。实用并不坏,但可能不实用到不知道发生了什么的地步。

回答by Noufal Ibrahim

gitis like UNIX. User friendly but picky about its friends. It's about as powerful and as user friendly as a shell pipeline.

git就像 UNIX。用户友好但对朋友很挑剔。它与 shell 管道一样强大且用户友好。

That being said, once you understand its paradigms and concepts, it has the same zenlike clarity that I've come to expect from UNIX command line tools. You should consider taking some time off to read one of the many good git tutorials available online. The Pro Git book is a good place to start.

话虽如此,一旦您理解了它的范式和概念,它就会像我期望从 UNIX 命令行工具中获得的一样清晰。您应该考虑花一些时间阅读在线提供的众多优秀 git 教程之一。Pro Git 书是一个很好的起点。

To answer your first question.

回答你的第一个问题。

  1. What is git remote add ...

    As you probably know, gitis a distributed version control system. Most operations are done locally. To communicate with the outside world, gituses what are called remotes. These are repositories other than the one on your local disk which you can pushyour changes into (so that other people can see them) or pullfrom (so that you can get others changes). The command git remote add origin [email protected]:peter/first_app.gitcreates a new remote called originlocated at [email protected]:peter/first_app.git. Once you do this, in your push commands, you can push to origininstead of typing out the whole URL.

  2. What is git push origin master

    This is a command that says "push the commits in the local branch named masterto the remote named origin". Once this is executed, all the stuff that you last synchronised with origin will be sent to the remote repository and other people will be able to see them there.

  1. 什么是 git remote add ...

    您可能知道,git是一个分布式版本控制系统。大多数操作在本地完成。为了与外界交流,git使用所谓的remotes. 这些是您本地磁盘上的存储库以外的存储库,您可以push在其中进行更改(以便其他人可以看到它们)或pull从中(以便您可以获得其他人的更改)。该命令git remote add origin [email protected]:peter/first_app.git创建一个名为origin位于的新遥控器[email protected]:peter/first_app.git。执行此操作后,在推送命令中,您可以推送到origin而不是输入整个 URL。

  2. 什么是 git push origin master

    这是一条命令,表示“将本地分支中的提交推master送到名为远程的分支origin”。执行此操作后,您上次与 origin 同步的所有内容都将发送到远程存储库,其他人将能够在那里看到它们。

Now about transports (i.e. what git://) means. Remote repository URLs can be of many types (file://, https://etc.). Git simply relies on the authentication mechanism provided by the transport to take care of permissions and stuff. This means that for file://URLs, it will be UNIX file permissions, etc. The git://scheme is asking git to use its own internal transport protocol, which is optimised for sending git changesets around. As for the exact URL, it's the way it is because of the way github has set up its gitserver.

现在关于运输(即什么git://)的意思。远程仓库的URL可以是多种类型(的file://https://等等)。Git 只是依赖传输提供的身份验证机制来处理权限和其他内容。这意味着对于file://URL,它将是 UNIX 文件权限等。该git://方案要求 git 使用其自己的内部传输协议,该协议针对发送 git 变更集进行了优化。至于确切的 URL,这是因为 github 设置其git服务器的方式。

Now the verbosity. The command you've typed is the general one. It's possible to tell git something like "the branch called masterover here is local mirror of the branch called fooon the remote called bar". In git speak, this means that mastertracksbar/foo. When you clone for the first time, you will get a branch called masterand a remote called origin(where you cloned from) with the local master set to track the master on origin. Once this is set up, you can simply say git pushand it'll do it. The longer command is available in case you need it (e.g. git pushmight push to the official public repo and git push review mastercan be used to push to a separate remote which your team uses to review code). You can set your branch to be a tracking branch using the --set-upstreamoption of the git branchcommand.

现在的冗长。您输入的命令是通用命令。可以告诉 git 类似“master这里调用的分支foo是远程调用的分支的本地镜像bar”。在 git 中,这意味着master跟踪bar/foo. 当你第一次克隆时,你会得到一个被调用的分支master和一个远程调用origin(你从那里克隆的),本地主设置为跟踪源上的主。设置完成后,您可以简单地说git push,它就会做到。如果您需要,可以使用更长的命令(例如,git push可能会推送到官方公共存储库,git push review master并可用于推送到您的团队用来代码的单独远程)。您可以使用以下命令将分支设置为跟踪分支--set-upstreamgit branch命令的选项。

I've felt that git (unlike most other apps I've used) is better understood from the inside out. Once you understand how data is stored and maintained inside the repository, the commands and what they do become crystal clear. I do agree with you that there's some elitism amongst many gitusers but I also found that with UNIX users once upon a time, and it was worth ploughing past them to learn the system. Good luck!

我觉得 git(与我使用过的大多数其他应用程序不同)更好地由内而外地理解。一旦您了解了如何在存储库中存储和维护数据,命令及其功能就会变得非常清晰。我确实同意您的观点,即在许多git用户中存在一些精英主义,但我也发现曾经有 UNIX 用户存在这种情况,值得深入了解他们以学习该系统。祝你好运!

回答by Mark Longair

Update: note that the currently accepted answer perpetuates a common misunderstandingabout the behaviour of git push, which hasn't been corrected despite a comment pointing it out.

更新:请注意,当前接受的答案延续了对 的行为的常见误解git push,尽管有评论指出,但尚未纠正。

Your summary of what remotes are - like a nickname for the URL of a repository - is correct.

你对什么是远程的总结——比如存储库 URL 的昵称——是正确的。

So why does the URL not git://[email protected]/peter/first_app.git but in the other syntax -- what syntax is it? Why must it end with .git? I tried not using .git at the end and it works too. If not .git, what else can it be? The git at the beginner seems to be a user account on the git server?

那么为什么 URL 不是 git://[email protected]/peter/first_app.git 而是另一种语法——它是什么语法?为什么必须以 .git 结尾?我尝试在最后不使用 .git 并且它也有效。如果不是 .git,还能是什么?初学者的git好像是git服务器上的用户账号?

The two URLs that you've mentioned indicate that two different transport protocols should be used. The one beginning with git://is for the git protocol, which is usually only used for read-only access to repositories. The other one, [email protected]:peter/first_app.git, is one of the different ways of specifying access to a repository over SSH - this is the "scp-style syntax" described in the documentation. That the username in the scp-style syntax is gitis because of the way that GitHub deals with identifying users - essentially that username is ignored, and the user is identified based on the SSH key-pair that they used to authenticate.

您提到的两个 URL 表示应该使用两种不同的传输协议。以 开头的git://是 git 协议,它通常只用于对存储库的只读访问。另一种[email protected]:peter/first_app.git是指定通过 SSH 访问存储库的不同方式之一 - 这是文档中描述的“scp 样式语法” 。scp 样式语法中的用户名git是因为 GitHub 处理识别用户的方式 - 本质上该用户名被忽略,并且用户是根据他们用来进行身份验证的 SSH 密钥对来识别的。

As for the verbosity of git push origin master, you've noticed that after the first push, you can then just do git push. This is because of a series of difficult-to-remember-but-generally-helpful defaults :)

至于 的冗长git push origin master,您已经注意到,在第一次推送之后,您就可以执行git push. 这是因为一系列难以记住但通常有用的默认值:)

  • If no remote is specified, the remote configured for the current branch (in remote.master.urlin your case) is used. If that's not set up, then originis used.
  • If there's no "refspec" (e.g. master, master:my-experiment, etc.) specified, then git defaults to pushing every local branch that has the same name as a branch on the remote. If you just have a branch called masterin common between your repository and the remote one, that'll be the same as pushing your masterto the remote master.
  • 如果未指定远程,则使用为当前分支(remote.master.url在您的情况下)配置的远程。如果没有设置,则origin使用。
  • 如果没有指定“refspec”(例如master,master:my-experiment等),则 git 默认推送与远程分支同名的每个本地分支。如果您master的存储库和远程存储库之间只有一个共同调用的分支,则与将您的存储库推master送到远程master.

Personally, since I tend to have many topic branches (and often several remotes) I always use the form:

就我个人而言,由于我倾向于有许多主题分支(通常还有几个遥控器),我总是使用以下形式:

git push origin master

... to avoid accidentally pushing other branches.

...以避免意外推动其他分支。



In reply to your comments on one of the other answers, it sounds to me as if arelearning about git in a top-down way very effectively - you've discovered that the defaults work, and your question is asking about why ;) To be more serious, git canbe used essentially as simply as SVN, but knowing a bit about remotes and branches means you can use it much more flexibily and this can really change the way you work for the better. Your remark about a semester course makes me think of something Scott Chacon said in a podcast interview - students are taught about all kinds of basic tools in computer science and software engineering, but very rarely version control. Distributed version control systems such as git and Mercurial are now so important, and so flexible, that it would be worth teaching courses on them to give people a good grounding.

在回复您对其他答案之一的评论时,在我看来,似乎正在以自上而下的方式非常有效地学习 git - 您发现默认设置有效,而您的问题是询问原因;)更严重一点,git可以本质上与 SVN 一样简单地使用,但是对远程和分支有所了解意味着您可以更灵活地使用它,这确实可以更好地改变您的工作方式。你对一学期课程的评论让我想起了 Scott Chacon 在播客采访中所说的话——学生们学习了计算机科学和软件工程中的各种基本工具,但很少有版本控制。诸如 git 和 Mercurial 之类的分布式版本控制系统现在非常重要且非常灵活,因此值得教授有关它们的课程,以便为人们打下良好的基础。

My view is that with git, this learning curve is absolutely worth it - working with lots of topic branches, merging them easily, and pushing and pulling them about between different repositories is fantastically useful once you become confident with the system. It's just unfortunate that:

我的观点是,有了git,这条学习曲线绝对值得 - 处理大量主题分支,轻松合并它们,并在您对系统充满信心后在不同存储库之间推动和拉动它们非常有用。只是不幸的是:

  • The primary documentation for git is so hard to parse for newcomers. (Although I'd argue that if you Google for almost any git question, helpful tutorial material (or Stack Overflow answers :)) come up nowadays.)
  • There are a few odd behaviours in git that are hard to change now because many scripts may rely on them, but are confusing to people.
  • git 的主要文档对于新手来说很难解析。(尽管我认为,如果您在 Google 上搜索几乎所有 git 问题,现在都会出现有用的教程材料(或 Stack Overflow 答案:))。)
  • git 中有一些奇怪的行为现在很难改变,因为许多脚本可能依赖于它们,但让人们感到困惑。

回答by Mr. Suryaa Jha

Have a look at the syntax for adding a remote repo.

查看添加远程仓库的语法。

git remote add origin <url_of_remote repository>

Example:

例子:

git remote add origin [email protected]:peter/first_app.git

Let us dissect the command :

让我们剖析命令:

git remotethis is used to manage your Central servers for hosting your git repositories.

git remote这用于管理您的中央服务器以托管您的 git 存储库。

May be you are using Githubfor your central repository stuff. I will give you a example and explain the git remote add origincommand

可能你正在使用Github作为你的中央存储库的东西。给大家举个例子,解释一下git remote add origin命令

Suppose I am working with GitHuband BitBucketfor the central servers for the git repositories and have created repositories on both the websites for my first-appproject.

假设我正在使用GitHubBitBucket作为 git 存储库的中央服务器,并且已经在这两个网站上为我的第一个应用程序项目创建了存储库。

Now if I want to push my changes to both these git servers then I will need to tell git how to reach these central repositories. So I will have to add these,

现在,如果我想将我的更改推送到这两个 git 服务器,那么我需要告诉 git 如何访问这些中央存储库。所以我将不得不添加这些,

For GitHub

对于 GitHub

git remote add gh_origin https://github.com/user/first-app-git.git

And For BitBucket

对于 BitBucket

git remote add bb_origin https://[email protected]/user/first-app-git.git

I have used two variables ( as far it is easy for me to call them variables ) gh_origin( gh FOR GITHUB ) and bb_origin( bb for BITBUCKET ) just to explain you we can call origin anything we want.

我使用了两个变量(到目前为止我很容易称它们为变量)gh_origin(gh FOR GITHUB)和bb_origin(bb for BITBUCKET)只是为了向您解释我们可以将 origin 称为我们想要的任何东西。

Now after making some changes I will have to send(push) all these changes to central repositories so that other users can see these changes. So I call

现在,在进行一些更改后,我必须将所有这些更改发送(推送)到中央存储库,以便其他用户可以看到这些更改。所以我打电话

Pushing to GitHub

推送到 GitHub

git push gh_origin master

Pushing to BitBucket

推送到 BitBucket

git push bb_origin master

gh_originis holding value of https://github.com/user/first-app-git.gitand bb_originis holding value of https://[email protected]/user/first-app-git.git

gh_origin被保持的值https://github.com/user/first-app-git.gitbb_origin被保持的值的https://[email protected]/user/first-app-git.git

This two variables are making my life easier

这两个变量让我的生活更轻松

as whenever I need to send my code changes I need to use this words instead of remembering or typing the URL for the same.

就像每当我需要发送我的代码更改时,我需要使用这个词而不是记住或输入相同的 URL。

Most of the times you wont see anything except than originas most of the times you will deal with only one central repository like Github or BitBucket for example.

大多数时候,除了origin之外,你不会看到任何东西,因为大多数时候你只会处理一个中央存储库,例如 Github 或 BitBucket。

回答by anshul

  1. The .gitat the end of the repository name is just a convention. Typically, on git servers repositories are kept in directories named project.git. The git client and protocol honours this convention by testing for project.gitwhen only projectis specified.

  2. git://[email protected]/peter/first_app.gitis not a valid git url. git repositories can be identified and accessed via various url schemes specified here. [email protected]:peter/first_app.gitis the sshurl mentioned on that page.

  3. gitis flexible. It allows you to track your local branch against almost any branch of any repository. While master(your local default branch) tracking origin/master(the remote default branch) is a popular situation, it is not universal. Many a times you may not want to do that. This is why the first git pushis so verbose. It tells git what to do with the local masterbranch when you do a git pullor a git push.

  4. The default for git pushand git pullis to work with the current branch's remote. This is a better default than origin master. The way git push determines this is explained here.

  1. .git在库名的末尾仅仅是一个惯例。通常,在 git 服务器上,存储库保存在名为project.git. git 客户端和协议通过测试project.gitproject指定何时来遵守此约定。

  2. git://[email protected]/peter/first_app.git不是有效的 git url。可以通过此处指定的各种 url 方案来识别和访问 git 存储库。 [email protected]:peter/first_app.gitssh那个页面上提到的网址。

  3. git是灵活的。它允许您针对任何存储库的几乎任何分支跟踪本地分支。虽然master(您的本地默认分支)跟踪origin/master(远程默认分支)是一种流行的情况,但它并不普遍。很多时候你可能不想这样做。这就是为什么第一个git push如此冗长。master当您执行 agit pull或 a时,它会告诉 git 如何处理本地分支git push

  4. 默认的git pushgit pull是与当前分支的遥控工作。这是比 origin master 更好的默认设置。git push 确定这一点的方式在这里解释。

gitis fairly elegant and comprehensible but there is a learning curve to walk through.

git相当优雅和易于理解,但有一个学习曲线要​​走。

回答by saikumarputta

Git remote add origin:

Git远程添加原点:

It centralises your source code to the other projects.It is developed based on Linux, complete open source and make your code useful to the other git users.we call it as reference

它将你的源代码集中到其他项目中。它是基于Linux开发的,完全开源,让你的代码对其他git用户有用。我们称之为参考

Pushes your code into git repository using remote url of the git hub.

使用 git hub 的远程 url 将您的代码推送到 git 存储库中。