什么是“git远程添加..."?和“git push origin master"? [英] What is "git remote add ..." and "git push origin master"?

查看:36
本文介绍了什么是“git远程添加..."?和“git push origin master"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多时候,Git 和 Ruby on Rails 看起来很神奇……例如在Ruby on Rails 3 教程书的第一章,讲的是Git:>

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

git remote add origin git@github.com:peter/first_app.git
git push origin master

它几乎说它只是有效";无需过多地谈论它们是什么,然后开始谈论分支.网上查了一下,git remote add就是添加一个短名称",比如origin,也可以是任意名称,就像一个URL 的别名.

And it pretty much says "it just works" without saying too much about what they are and start talking about branching. Searching on the Internet shows that git remote add is to add a "short name", such as origin, and it can be any name as well, which is like an alias to a URL.

origin 是远程存储库指向的通常路径(在 http://git-scm.com/book/en/Git-Basics-Working-with-Remotes 在添加远程存储库"下).

And origin is the usual path of where the remote repository points to (in http://git-scm.com/book/en/Git-Basics-Working-with-Remotes under "Adding Remote Repositories").

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

So why is the URL not git://git@github.com/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 in git@github.com seems to be a user account on the Git server?

另外,为什么使用git push origin master需要这么冗长?不能默认是origin和master吗?我发现第一次需要origin master,但是经过一个小的编辑和提交,然后git push就足够了(不需要origin master).有知道情况的人能详细说说吗?

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 master is needed, but after a small edit and commit, then git push is all it needs (no need origin master). Can somebody who knows what is going on give some details?

有时感觉就像很多魔法一样没有解释……有时使用它的人非常自信,当被问到为什么时,无法解释,并用类似事情就是这样"这样的话来回应.有时非常实际和务实.实用一点也不错,但可能不实用到不知道发生了什么的地步.

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.

推荐答案

Git 就像 Unix.它是用户友好的,但它对朋友很挑剔.它与 shell 管道一样强大且用户友好.

Git is like Unix. It is user-friendly, but it is picky about its friends. It's about as powerful and as user-friendly as a shell pipeline.

话虽如此,一旦您理解了它的范式和概念,它就会具有我对 Unix 命令行工具所期望的 Zenlike 清晰度.您应该考虑花一些时间阅读在线提供的众多优秀 Git 教程之一.Pro Git 一书是一个不错的起点.

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.

回答你的第一个问题.

  1. 什么是git remote add ...?

您可能知道,Git 是一个分布式版本控制系统.大多数操作都是在本地完成的.为了与外界交流,Git 使用了所谓的遥控器".这些存储库不同于本地磁盘上的存储库,您可以将更改推送到其中(以便其他人可以看到它们)或拉取(以便您可以获取其他变化).命令 git remote add origin git@github.com:peter/first_app.git 创建一个名为 origin 的新远程,位于 git@github.com:peter/first_app.git.执行此操作后,在推送命令中,您可以推送到 origin 而不是输入整个 URL.

As you probably know, Git is a distributed version control system. Most operations are done locally. To communicate with the outside world, Git uses what are called "remotes". These are repositories other than the one on your local disk which you can push your changes into (so that other people can see them) or pull from (so that you can get others changes). The command git remote add origin git@github.com:peter/first_app.git creates a new remote called origin located at git@github.com:peter/first_app.git. Once you do this, in your push commands, you can push to origin instead of typing out the whole URL.

什么是git push origin master?

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

This is a command that says "push the commits in the local branch named master to 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.

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

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 Git server.

现在是冗长.您输入的命令是通用命令.可以告诉 Git 类似这里名为 master 的分支是名为 foo 的远程分支的本地镜像 bar";.在 Git 中,这意味着 master tracks bar/foo.当您第一次克隆时,您将获得一个名为 master 的分支和一个名为 origin 的远程(您克隆的地方),其中本地主设置用于跟踪主起源.

Now the verbosity. The command you've typed is the general one. It's possible to tell Git something like "the branch called master over here is local mirror of the branch called foo on the remote called bar". In Git speak, this means that master tracks bar/foo. When you clone for the first time, you will get a branch called master and a remote called origin (where you cloned from) with the local master set to track the master on origin.

设置完成后,您只需说 git push 即可.如果您需要,可以使用更长的命令(例如,git push 可能推送到官方公共存储库,git push review master 可用于推送到单独的远程您的团队用来审查代码).您可以使用 git branch 命令的 --set-upstream 选项将您的分支设置为跟踪分支.

Once this is set up, you can simply say git push and it'll do it. The longer command is available in case you need it (e.g., git push might push to the official public repository and git push review master can 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-upstream option of the git branch command.

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

I've felt that Git (unlike most other applications 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 Git users, 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 push origin master"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆