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

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

问题描述

通常,Git和Rails看起来很神奇......例如在第一章Rails 3 Tutorial book ,它讨论了Git:

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

它几乎可以说 他们并没有多说什么,并开始谈论分支。在网上搜索显示 git remote add 是添加一个短名称,比如 origin ,并且它也可以是任何名称,就像URL的别名一样。并且 origin 是远程回购指向的常用路径。 (在 http://git-scm.com/book/en / Git-Basics-Working-with-Remotes )添加远程仓库)

那为什么这个URL不是 git: //git@github.com/peter/first_app.git 但在其他语法中 - 它是什么语法?为什么必须以 .git 结尾?我在最后尝试不使用 .git ,它也可以工作。如果不是 .git ,还有什么可以的? git@github.com 中的 git 似乎是git服务器上的用户帐户?



另外,它为什么需要如此详细地使用 git push origin master ?不能默认是起源和主人吗?我发现第一次需要 origin master ,但是在一个小小的编辑和提交之后,然后 git push 是它所需要的(不需要 origin master )。有人知道发生了什么会给出一些细节吗?



有时候感觉就像很多魔术一样,没有解释......有时使用它的人是如此自信,当被问及为什么时,无法解释它,并用就是这样的方式回应。有时非常务实和务实。这是不是很实际,但可能不切实际,不知道发生了什么。

解决方案

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

这就是说,一旦你理解了它的范例和概念,它就具有与UNIX命令行工具相同的清晰度。你应该考虑抽出一些时间阅读在线提供的许多优秀的git教程之一。 Pro Git书是开始的好地方。

回答你的第一个问题。


  1. 什么是 git remote add ...



    您可能知道, git 是一个分布式版本控制系统。大多数操作都在本地完成。为了与外部世界进行交流, git 使用了所谓的远程。这些存储库不是您本地磁盘上的存储库,您可以将 push 更改为(以便其他人可以看到它们)或 pull from(这样你就可以获得其他变更)。命令 git remote add origin git@github.com:peter / first_app.git 创建一个名为 origin 的新远程在 git@github.com:peter / first_app.git 。一旦你这样做,在你的push命令中,你可以推送到 origin ,而不是输入整个URL。

  2. p>这是一个命令,它将将名为 master 的本地分支中的提交推送到名为 origin 的远程 。执行此操作后,所有与原始同步的内容都将发送到远程存储库,其他人员将能够在此处看到它们。 )的意思。远程存储库URL可以有多种类型( file:// https:// 等)。 Git只是依靠传输提供的认证机制来处理权限和内容。这意味着对于 file:// URL,它将是UNIX文件权限等。 git:// 方案是要求git使用自己的内部传输协议,该协议针对发送git变更集进行了优化。至于确切的URL,这是因为github建立它的 git 服务器的方式。



    现在的详细程度。你输入的命令是一般的命令。可以告诉git这里有一个名为 master 的分支,这里是分支的本地镜像,名为 foo 远程调用 bar 。在git中,这意味着 master track bar / foo 。当你第一次克隆的时候,你会得到一个名为 master 和一个名为 origin 的远程分支(你克隆的地方from)与本地主设备一起追踪原点上的主设备。一旦设置完成,你可以简单地说 git push ,它就可以做到。如果您需要它,可以使用更长的命令(例如, git push 可能会推到官方公共仓库, git push review master 可以用来推送到您的团队用来查看代码的单独的远程设备)。您可以使用 git分支命令的 - set-upstream 选项将分支设置为跟踪分支。



    我从内到外都能更好地理解git(与大多数其他应用程序不同)。一旦您了解了数据库中存储和维护数据的方式,命令和它们的功能就变得清晰起来。我同意你的看法,许多 git 用户中有一些精英主义,但我也发现,曾经有UNIX用户在使用它,并且过去他们学习系统是值得的。 。祝你好运!


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

    git remote add origin git@github.com: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 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. And origin is the usual path of where the remote repo points to. (in http://git-scm.com/book/en/Git-Basics-Working-with-Remotes under "Adding Remote Repositories")

    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?

    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 is like UNIX. User friendly but picky about its friends. It's about as powerful and as user friendly as a shell pipeline.

    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.

    To answer your first question.

    1. What is git remote add ...

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

    2. What is git push origin master

      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.

    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.

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

    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 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 remote add ...”和“git push origin master”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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