简体   繁体   English

创建GitHub存储库会创建Git存储库吗?

[英]Does creating a GitHub repository create a Git repository?

I have created many a repository on GitHub, but have not yet uploaded any of them to Git. 我在GitHub上创建了许多存储库,但尚未将其中任何一个上传到Git。

While downloading my repositories, on GitHub, using the green button in the top right-hand corner of the repo's file listing, I have noticed that you can download the repo via Git using a link, even though I have not uploaded the repo to Git. 在GitHub上使用仓库的文件列表右上角的绿色按钮下载我的存储库时,我注意到您可以使用链接通过Git下载仓库,即使我没有将仓库上传到Git。 。 I have noticed also that the links look like this: https://github.com/Dog-Face-Development/Bars.git , and point to a Bars.git (or whatever the repo's name is) file which I don't have in my file listing. 我也注意到链接看起来像这样: https://github.com/Dog-Face-Development/Bars.git : https://github.com/Dog-Face-Development/Bars.git ,并指向一个Bars.git (或任何回购名称)文件,我没有在我的文件列表中。

The question is: Since I am able to download my GitHub repo via Git even though I haven't uploaded the repo to Git, and since there seems to be a file with the .git extension, does that mean that my repo is automatically uploaded and added to Git without me doing anything, and then I can commit to the repo on Git using my computer and the command-line Git tools? 问题是:由于即使我没有将仓库上传到Git,我也可以通过Git下载我的GitHub仓库,并且因为似乎有一个扩展名为.git的文件,这是否意味着我的仓库已自动上传并在不做任何事情的情况下添加到Git,然后我可以使用计算机和命令行Git工具在Git上提交回购协议吗?

The question is: Since I am able to download my GitHub repo via Git even though I haven't uploaded the repo to Git, and since there seems to be a .git file, does that mean that my repo is automatically uploaded ... 问题是:由于即使我没有将仓库上传到Git,也可以通过Git下载我的GitHub仓库,而且由于似乎存在.git文件,这是否意味着我的仓库已自动上传...

(emphasis mine) (强调我的)

No. However, the answer to the question you asked in the subject line is "yes": creating a GitHub repository creates a Git repository. 否。但是,您在主题行中询问的问题的答案是“是”:创建GitHub存储库将创建Git存储库。

The tricky part here is that a GitHub repository is a Git repository. 这里最棘手的部分是GitHub存储库 Git存储库。 What GitHub adds (their "added value" as a service) is—well, more than one thing, so let's say includes rather than is —that the repository they create is stored on their computers, where it gets backed up, and is accessible from all over the web, and so on. 什么GitHub的增加(他们的“增值”服务)是孔,不止一两件事,让我们说包括而非 -即他们创造存储在他们的计算机上,在那里它被备份的存储库,并访问遍布整个网络等等。

They—GitHub—also add a bunch of communications layering, and things like one-button "pull requests", and forks, and so on. 它们(GitHub)还添加了一堆通信分层,以及一键式“拉取请求”和派生等。 But it all starts with them creating a Git repository. 但这一切都始于他们创建一个Git存储库。

The thing is, their Git repository is, at this point, totally independent of your Git repository, if you even have one yet! 问题是,此时,即使您还拥有Git存储库, 它们的 Git存储库也完全独立于您的 Git存储库!

The way two different (independent) Git repositories talk to each other is, at least normally, to use git fetch and/or git push . 两个不同的(独立的)Git存储库彼此交谈的方式至少通常是使用git fetch和/或git push These Git commands tell your Git to call up some other Git, usually via some http or https or ssh address. 这些Git命令告诉您的Git通常通过一些http或https或ssh地址来调用其他Git。 Once your Git is talking to that other Git via whatever this communications channel may be—I like to call it a telephone conversation, which might be meaningful for those folks who use their smartphones to make actual phone calls :-) —your Git and their Git exchange information as to who has which commits, and one Git (the sender) sends commits and other objects to the other Git (the receiver). 一旦您的Git通过该通信渠道可能与其他Git交谈(我喜欢将其称为电话交谈,这对于那些使用智能手机拨打实际电话的人可能是有意义的:-))-您的Git及其Git交换有关谁拥有哪些提交的信息,一个Git(发送方)将提交和其他对象发送给另一个Git(接收方)。

If you don't have a Git repository at all yet, you can use a four-step process: 如果还没有Git存储库,则可以使用四步过程:

  1. create a new, totally-empty repository; 创建一个新的完全空的存储库;
  2. add a remote (usually named origin ) that stores a URL like https://github.com/path/to/repo.git ; 添加一个远程 (通常命名为origin ),该远程存储类似https://github.com/path/to/repo.git的URL;
  3. run git fetch in this new empty repository to collect all the commits in the other Git that answers the Internet-phone at that https "phone number"; 在这个新的空存储库中运行git fetch ,以收集另一个 Git的所有提交,这些Git在该https “电话号码”处应答Internet电话; and last 最后
  4. run git checkout master to create a new branch named master in your new repository using the same commit found by the name origin/master that step 3 created in your repository, based on what their Git has in their master . 运行git checkout master创建一个新的分支叫master在新信息库使用相同的承诺将通过名称找到origin/master是第3步在你的仓库,基于什么他们 Git有他们的创造master

Or, you can run git clone , which does those steps for you. 或者,您可以运行git clone ,它会为您执行这些步骤。

Either way, now there are two Git repositories. 无论哪种方式,现在都有两个 Git存储库。

There's one other thing to watch out for, though. 不过,还有另一件事需要提防。

GitHub can create a totally-empty repository, or one with 1 commit GitHub可以创建一个完全为空的存储库,也可以创建一次提交的存储库

When you use the "create repository" button on the GitHub web interface, that does create a Git repository there. 当您在GitHub Web界面上使用“创建存储库”按钮时,会在此处创建一个Git存储库。 You now choose whether to create it with a README file and/or a .gitignore file and/or a LICENSE file, using clicky boxes and pulldown menu buttons. 现在,您可以使用clicky框和下拉菜单按钮选择是否使用README文件和/或.gitignore文件和/或LICENSE文件来创建它。

In order to create a repository with files, GitHub must actually start by creating a completely empty repository, then immediately add a single commit whose contents are the files you chose. 为了创建包含文件的存储库,GitHub实际上必须首先创建一个完全空的存储库,然后立即添加一个提交,其内容就是您选择的文件。

They claim: 他们声称:

This will let you immediately clone the repository to your computer. 这将使您立即将存储库克隆到计算机。 Skip this step if you're importing an existing repository. 如果要导入现有存储库,请跳过此步骤。

but that's not really true. 但这不是真的。 You can immediately clone the empty repository, too. 您也可以立即克隆空的存储库。 The problem is that an empty repository behaves rather oddly. 问题在于,空的存储库的行为很奇怪。

Now, if you use git init to make your own repositories, those are also empty. 现在,如果您使用git init来创建自己的存储库,那么这些存储库也是空的。 They behave rather oddly too, but you may not have noticed, because as soon as you make your first commit, they begin behaving normally ... and any oddness can be written off pretty easily. 它们的行为也很奇怪,但是您可能没有注意到,因为一旦您第一次提交,它们就会开始表现正常……并且任何奇数都可以很容易地被注销。 This is less so for the git clone command, which really wants to end with a git checkout step. 对于git clone命令来说要少得多,该命令实际上要以git checkout步骤结束。

Still, you can clone an empty repository. 不过,您可以克隆一个空的存储库。 The result is another empty repository. 结果是另一个空的存储库。 You can then create a first commit here, and use git push to send it back to the (still-empty) repository on GitHub. 然后,您可以在此处创建第一个提交,然后使用git push其发送回GitHub上的(仍然为空)存储库。 Once you create a commit in your clone, it's no longer empty and it starts behaving normally; 在克隆中创建提交后,提交不再为空,并且开始正常运行。 when you run git push , your Git calls up the Git on GitHub at the stored "phone number", sends them your commit(s), and has them create a master branch, or whatever branch you actually created and pushed, and now their repository also starts behaving normally. 当您运行git push ,您的Git在GitHub上以存储的“电话号码”调用Git,向他们发送您的提交,并让他们创建一个master分支,或者您实际创建并推送的任何分支,现在存储库也开始正常运行。

What if you have them make 1 commit, but you make your own first commit? 如果让他们进行1次提交但又进行了第一次提交怎么办?

If you think about the above, you will see that you could have GitHub create a repository and put one commit in it, and then you could git init your own repository and put your own first commit in it. 如果考虑以上内容,您会发现可以让GitHub创建存储库并在其中进行一次提交,然后可以git init自己的存储库并在其中进行首次提交。 Then you can git add origin https://github.com/... to set your Git up to call their Git up, and try running git fetch . 然后,您可以git add origin https://github.com/...来设置您的 Git来调用他们的 Git,然后尝试运行git fetch

This will work just fine. 这样就可以了。 Your Git will call up their Git and have their Git send you their commits, which will be their one single root commit with the readme, ignore, and/or license files. 您的Git将调用他们的Git,并让他们的Git向您发送其提交,这将是他们的一个根目录提交,其中包含自述文件,忽略文件和/或许可证文件。 Your Git will record that commit's hash ID under your Git's origin/master name. 您的Git会将记录的提交的哈希ID记录在您的Git的origin/master名称下。

Meanwhile, though, your own Git has your first commit recorded under your master name. 但是,与此同时,您自己的Git会将您的第一次提交记录在您的master名称下。 Your first commit has whatever you put in it—maybe different files than the up-to-three GitHub will create for you. 您的第一次提交包含任何内容-可能与最新的GitHub将为您创建的文件不同。

The curious things about this is that both of these commits are root commits . 奇怪的是, 这两个提交都是root提交 They have their one commit, which is unrelated to your one commit; 他们有一个提交,与您的一个提交无关。 and now you have their one commit, and your one commit. 现在您有一个提交,还有一个提交。

You can ask your Git to merge these two commits. 您可以要求您的Git 合并这两个提交。 Some versions of Git will do that without question. 某些版本的Git会毫无疑问地做到这一点。 Newer versions (2.9 or later) will object: Hey, your master and their origin/master seem unrelated! 较新的版本(2.9或更高版本)将反对: 嗨,您的master及其origin/master似乎无关! To get these newer Gits to merge these unrelated histories, you must add the --allow-unrelated-histories option. 要使这些更新的Git合并这些不相关的历史记录,必须添加--allow-unrelated-histories unrelated --allow-unrelated-histories选项。 For more about this, see VonC's answer to a related question . 有关此的更多信息,请参见VonC对相关问题的回答

GitHub is a Git repository hosting. GitHub是一个Git存储库托管。 You create the repository on GitHub, and then you can clone through Git and commit there as well. 您在GitHub上创建存储库,然后可以通过Git克隆并在其中提交。

As suggested, just follow the introduction guide . 根据建议,只需按照入门指南进行操作

Since I am able to download my GitHub repo via Git even though I haven't uploaded the repo to Git 由于即使我没有将仓库上传到Git,也可以通过Git下载我的GitHub库。

The repository initialized on GitHub, indirectly, by you, upon creation, is a bare repository . 创建后,由您间接在GitHub上初始化的存储库是一个裸存储库

... and since there seems to be a .git file ...而且似乎有一个.git文件

This is not a file. 不是文件。 The .git suffix (not a file extension) is just a naming convention for bare repositories. .git后缀(不是文件扩展名)只是裸存储库的命名约定。

... does that mean that my repo is automatically uploaded and added to Git without me doing anything, and then I can commit to the repo on Git using my computer and the command-line Git tools ...这是否意味着我的存储库无需任何操作即可自动上传并添加到Git,然后我可以使用计算机和命令行Git工具在Git上提交存储库

No. The remote, empty, bare repository, is only "uploaded" with your code upon your first git push command made from your local repo . 不会。只有在从本地存储库发出的第一个git push命令上,远程,空的,空的存储库才会“上传”代码。

Rule of thumb: Everything on Git happens locally , you will only ever change your "remote" GitHub repo whenever you do pushes. 经验法则: Git上的所有操作都在本地进行 ,只有在您进行推送时,您才可以更改“远程” GitHub存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM