Have you ever been confused by how Git and GitHub work? Don’t fret — you are not alone. Git and GitHub can be tricky sometimes, but by the end of this post you will have a good grasp of the two.
您是否对Git和GitHub的工作方式感到困惑? 不要烦恼-您并不孤单。 Git和GitHub有时可能很棘手,但是到本文结尾,您将对这两者有很好的了解。
At first, it may be tempting to believe Git and GitHub are the same thing. But in reality they are not. Indeed, it is possible to use Git without GitHub! And ultimately, the two exist for different purposes.
起初,可能很容易相信Git和GitHub是同一回事。 但实际上并非如此。 确实,可以在没有GitHub的情况下使用Git! 最终,两者存在是出于不同的目的。
This post will begin by taking a good look at the purposes of Git and GitHub. Afterward, we will learn about the main differences between these two vital technologies.
这篇文章将首先介绍Git和GitHub的用途。 之后,我们将了解这两种关键技术之间的主要区别。
Without any further ado, let’s get it started with Git.
事不宜迟,让我们从Git开始。
什么是Git? (What is Git?)
Git is a Distributed Version Control System (DVCS) used to save different versions of a file (or set of files) so that any version is retrievable at will.
Git是一个分布式版本控制系统(DVCS),用于保存文件(或文件集)的不同版本,以便可以任意检索任何版本。
Git also makes it easy to record and compare different file versions. This means that the details about what changed, who changed what, or who initiated an issue are reviewable anytime.
Git还可以轻松记录和比较不同的文件版本。 这意味着有关更改内容,更改内容或引发问题的详细信息随时可以查看。
But if Git is a Distributed Version Control System, what exactly do those terms mean?
但是,如果Git是分布式版本控制系统,那么这些术语到底意味着什么?
“分布式”是什么意思? (What does “distributed” mean?)
The term “distributed” means that whenever you instruct Git to share a project’s directory, Git does not only share the latest file version. Instead, it distributes every version it has recorded for that project.
术语“分布式”意味着,只要您指示Git共享项目目录,Git不仅共享最新的文件版本。 相反,它将分发该项目已记录的每个版本。
This "distributed" system is in sharp contrast to other version control systems. They only share whatever single version a user has explicitly checked out from the central/local database.
这种“分布式”系统与其他版本控制系统形成鲜明对比。 它们仅共享用户从中央/本地数据库明确签出的任何单一版本。
Okay, so “distributed” means distribute all — not just selected few — versions of a project’s files that Git has recorded. But what exactly is a version control system?
好的,所以“分布式”意味着分发Git记录的项目文件的所有版本,而不仅仅是选定的少数版本。 但是,什么是版本控制系统呢?
什么是版本控制系统? (What is a Version Control System?)
A Version Control System (VCS) refers to the method used to save a file's versions for future reference.
版本控制系统(VCS)是指用于保存备查文件的版本的方法 。
Intuitively, many people already version control their projects by renaming different versions of the same file in various ways like blogScript.js
, blogScript_v2.js
, blogScript_v3.js
, blogScript_final.js
, blogScript_definite_final.js
, and so on. But this approach is error-prone and ineffective for team projects.
直观地,许多人已经通过以各种方式重命名同一文件的不同版本来对项目进行版本控制,例如blogScript.js
, blogScript_v2.js
, blogScript_v3.js
, blogScript_final.js
, blogScript_definite_final.js
等等。 但是这种方法容易出错,并且对团队项目无效。
Also, tracking what changed, who changed it, and why it was changed is a tedious endeavor with this traditional approach. This illuminates the importance of a reliable and collaborative version control system like Git.
此外,使用这种传统方法来跟踪更改的内容,更改的人以及更改的原因是一项繁琐的工作。 这说明了像Git这样的可靠且协作的版本控制系统的重要性。
However, to get the best of Git, it is essential to understand how Git handles your files.
但是,要充分利用Git,必须了解Git如何处理文件。
Git中的文件状态 (Files states in Git)
In Git, there are three primary states (conditions) in which a file can be: modified state, staged state, or committed state.
在Git中,文件可以处于三种主要状态(条件): 修改状态 , 暂存状态或提交状态 。
修改状态 (Modified state)
A file in the modified state is a revised — but uncommitted (unrecorded) — file.
处于修改状态的文件是已修改但未提交(未记录)的文件。
In other words, files in the modified state are files you have modified but have not explicitly instructed Git to monitor.
换句话说,处于修改状态的文件是您已修改但尚未明确指示Git进行监视的文件。
阶段状态 (Staged state)
Files in the staged state are modified files that have been selected — in their current state (version) — and are being prepared to be saved (committed) into the .git
repository during the next commit snapshot.
处于暂存状态的文件是已选择的修改文件-处于其当前状态(版本),并且准备在下一次提交快照期间保存(提交)到.git
存储库中。
Once a file gets staged, it implies that you have explicitly authorized Git to monitor that file’s version.
暂存文件后,表明您已明确授权Git监视该文件的版本。
承诺状态 (Committed state)
Files in the committed state are files successfully stored into the .git
repository.
处于提交状态的文件是成功存储到.git
存储库中的文件。
Thus, a committed file is a file in which you have recorded its staged version into the Git directory (folder).
因此,提交的文件是您已将其暂存版本记录到Git目录(文件夹)中的文件。
Note: The state of a file determines the location where Git will place it.
注意:文件的状态决定了Git放置文件的位置。
文件位置 (File locations)
There are three key places versions of a file may reside while version controlling with Git: the working directory, the staging area, or the Git directory.
在使用Git进行版本控制时,文件的版本可能驻留三个关键位置: 工作目录 , 暂存区或Git目录 。
工作目录 (Working directory)
The working directory is a local folder for a project's files. This means that any folder created anywhere on a system is a working directory.
工作目录是项目文件的本地文件夹。 这意味着在系统上任何位置创建的任何文件夹都是工作目录。
Note:
注意:
- Files in the modified state reside in the working directory. 处于修改状态的文件位于工作目录中。
The working directory is different from the
.git
directory. That is, you create a working directory while Git creates a.git
directory.工作目录不同于
.git
目录。 也就是说,您创建一个工作目录,而Git创建一个.git
目录。Check out this comparison article for more differences between the two repositories.
请查看此比较文章,以了解两个存储库之间的更多区别。
暂存区 (Staging area)
The staging area — technically called “index” in Git parlance — is a file, usually located in the .git
directory, that stores information about files next-in-line to be committed into the .git
directory.
暂存区(在Git术语中技术上称为“索引”)是一个文件,通常位于.git
目录中,该文件存储有关下一行要提交到.git
目录中的文件的信息。
Note:
注意:
- Files in the staged state reside in the staging area. 处于暂存状态的文件位于暂存区中。
Git目录 (Git directory)
The .git
directory is the folder (also called “repository”) that Git creates inside the working directory you have instructed it to track.
.git
目录是Git在您指示其跟踪的工作目录内创建的文件夹(也称为“存储库”)。
Also, the .git
folder is where Git stores the object databases and metadata of the file(s) you have instructed it to monitor.
另外, .git
文件夹是Git存储目标数据库和已指示其监视的文件的元数据的位置。
Note:
注意:
The
.git
directory is the life of Git — it is the item copied when you clone a repository from another computer (or from an online platform like GitHub)..git
目录是Git的生命-它是您从另一台计算机(或从GitHub等在线平台)克隆存储库时复制的项目。- Files in the committed state reside in the Git directory. 处于提交状态的文件位于Git目录中。
基本的Git工作流程 (The basic Git workflow)
Working with the Git Version Control System looks something like this:
使用Git版本控制系统看起来像这样:
Modify files in the working directory.
修改工作目录中的文件。
Note that any file you alter becomes a file in the
请注意,您更改的任何文件都将成为
modified state.
修改状态 。
Selectively stage the files you want to commit to the
.git
directory.有选择地将要提交到
.git
目录的文件登台。Note that any file you stage (add) into the staging area becomes a file in the
请注意,您暂存(添加)到暂存区域中的任何文件都会成为
staged state.
上演状态 。
Also, be aware that staged files are not yet in the
另外,请注意,暂存文件尚未位于
.git
database..git
数据库。Staging means information about the staged file gets included in a file (called "index") in the
暂存是指有关暂存文件的信息包含在文件中的文件(称为“索引”)中。
.git
repository..git
存储库。Commit the file(s) you have staged into the
.git
directory. That is, permanently store a snapshot of the staged file(s) into the.git
database.将已暂存的文件提交到
.git
目录。 也就是说,将暂存文件的快照永久存储到.git
数据库中。Note that any file version you commit to the
请注意,您提交给
.git
directory becomes a file in the committed state..git
目录成为处于提交状态的文件。
到目前为止的要点 (The gist thus far)
The long and short of all the discussion thus far is that Git is a brilliant version control system for competent versioning, management, and distribution of files. Check out this simple guide to learn how to use Git efficiently.
到目前为止,所有讨论的长短是Git是一个出色的版本控制系统,用于主管文件的版本控制,管理和分发。 查看此简单指南,以了解如何有效使用Git。
But, hang on a sec, if Git helps to effectively manage and distribute different versions of a project's file, what is GitHub's purpose?
但是,稍等一下,如果Git帮助有效地管理和分发项目文件的不同版本,那么GitHub的目的是什么?
GitHub揭秘 (GitHub Demystified)
GitHub is a web-based platform where users can host Git repositories. It helps you facilitate easy sharing and collaboration on projects with anyone at any time.
GitHub是一个基于Web的平台,用户可以在其中托管Git存储库。 它可帮助您随时与任何人轻松进行项目共享和协作。
GitHub also encourages broader participation in open-source projects by providing a secure way to edit files in another user's repository.
GitHub还通过提供一种安全的方式来编辑另一个用户存储库中的文件,从而鼓励更广泛地参与开源项目。
To host (or share) a Git repository on GitHub, follow the steps below:
要在GitHub上托管(或共享)Git存储库,请执行以下步骤:
步骤1:注册GitHub帐户 (Step 1: Signup for a GitHub account)
The first step to begin hosting on GitHub is to create a personal account. Visit the official registration page to sign up.
开始在GitHub上托管的第一步是创建一个个人帐户。 访问官方注册页面进行注册。
步骤2:在GitHub中创建一个远程存储库 (Step 2: Create a remote repository in GitHub)
After signing up for an account, create a home (a repository) in GitHub for the Git repository you want to share.
注册帐户后, 在GitHub中为要共享的Git存储库创建一个主目录(存储库) 。
步骤3:将项目的Git目录连接到远程存储库 (Step 3: Connect the project’s Git directory to the remote repository)
Once you've created a remote repository for your project, link the project’s .git
directory — located locally on your system — with the remote repository on GitHub.
为项目创建远程存储库后,将项目的.git
目录(位于系统本地)链接到GitHub上的远程存储库。
To connect to the remote repository, go inside the root directory of the project you want to share via your local terminal, and run:
要连接到远程存储库, 请进入要通过本地终端共享的项目的根目录 ,然后运行:
git remote add origin https://github.com/yourusername/yourreponame.git
Note:
注意:
Replace
yourusername
in the code above with your GitHub username.将以上代码中的
yourusername
替换为您的GitHub用户名。Likewise, replace
同样,更换
yourreponame
with the name of the remote repository you want to connect to.yourreponame
和要连接的远程存储库的名称。The command above implies that git should add the specified URL to the local project as a remote reference with which the local
.git
directory can interact.上面的命令暗示git应该将指定的URL 添加到本地项目中,作为本地
.git
目录可以与之交互的远程引用。The
origin
option in the command above is the default name (a short name) Git gives to the server hosting your remote repository.上面命令中的
origin
选项是Git给托管您的远程存储库的服务器的默认名称(简称)。That is, instead of the server's URL, Git uses the short name
也就是说,Git使用短名称代替服务器的URL。
origin
.origin
。It is not compulsory to stick with the server’s default name. If you prefer another name rather than
origin
, simply substitute theorigin
name in thegit remote add
command above with any name you prefer.不必强制使用服务器的默认名称。 如果您更喜欢使用其他名称而不是
origin
,只需git remote add
上面的git remote add
命令中的origin
名称替换为您喜欢的任何名称。Always remember that a server’s short name (for example,
origin
) is nothing special! It only exists — locally — to help you easily reference the server’s URL. So feel to change it to a short name you can easily reference.永远记住,服务器的简称(例如
origin
)没什么特别的! 它仅在本地存在,以帮助您轻松引用服务器的URL。 因此,可以将其更改为可以轻松引用的简称。To rename any existing remote URL, use the
git remote rename
command like so:要重命名任何现有的远程URL,请使用
git remote rename
命令,如下所示:
git remote rename theCurrentURLName yourNewURLName
Whenever you clone (download) any remote repo, Git automatically names that repo’s URL
origin
. However, you can specify a different name with thegit clone -o yourPreferredName
command.每当克隆(下载)任何远程存储库时,Git都会自动命名该存储库的URL
origin
。 但是,您可以使用git clone -o yourPreferredName
命令指定其他名称。To see the exact URL stored for nicknames like
origin
, rungit remote -v
command.要查看为昵称(如
origin
存储的确切URL,请运行git remote -v
命令。
步骤4:确认连接 (Step 4: Confirm the connection)
Once you’ve connected your Git directory to the remote repository, check whether the connection was successful by running git remote -v
on the command line.
将Git目录连接到远程存储库后,通过在命令行上运行git remote -v
来检查连接是否成功。
Afterward, check the output to confirm that the displayed URL is the same as the remote URL you intend to connect to.
然后,检查输出以确认显示的URL与您打算连接的远程URL相同。
Note:
注意:
See the “Connecting with SSH” article if you wish to connect using the SSH URL instead of the HTTPS URL.
However, if you are not sure of the remote URL to use, check out the “Which remote URL should I use?” article.
但是,如果您不确定要使用的远程URL,请查看“ 我应该使用哪个远程URL? ”文章。
Do you wish to change your remote URL? Changing a remote's URL is an excellent guide.
您要更改远程URL吗? 更改遥控器的URL是一个很好的指南。
步骤5:将本地Git存储库推送到远程存储库 (Step 5: Push a local Git repo to the remote repo)
After successfully connecting your local directory to the remote repository, you can then begin to push (upload) your local project upstream.
成功将本地目录连接到远程存储库后,您可以开始向上游推送(上传)本地项目。
Whenever you are ready to share your project elsewhere, on any remote repo, simply instruct Git to push all your commits, branches, and files in your local .git
directory to the remote repository.
每当您准备好在其他地方,在任何远程仓库上共享项目时,只需指示Git将本地.git
目录中的所有提交,分支和文件推送到远程存储库即可。
The code syntax used to upload (push) a local Git directory to a remote repository is git push -u remoteName branchName
.
用于将本地Git目录上载(推送)到远程存储库的代码语法为git push -u remoteName branchName
。
That is, to push your local .git
directory, and assuming the remote URL’s short name is “origin”, run:
也就是说,要推送本地.git
目录,并假设远程URL的简称为“ origin”,请运行:
git push -u origin master
Note:
注意:
The command above implies that git should push your local master branch to the remote master branch located at the URL named origin.
上面的命令暗示git应该将您的本地master分支推送到位于名为origin的URL上的远程master分支。
Technically, you can substitute the
origin
option with the remote repository’s URL. Remember, theorigin
option is only a nickname of the URL you’ve registered into your local.git
directory.从技术上讲,您可以用远程存储库的URL替换
origin
选项。 请记住,origin
选项只是您在本地.git
目录中注册的URL的昵称。The
-u
flag (upstream/tracking reference flag) automatically links the.git
directory's local branch with the remote branch. This allows you to usegit pull
without any arguments.-u
标志(上游/跟踪参考标志)自动将.git
目录的本地分支与远程分支链接。 这允许您使用不带任何参数的git pull
。
第6步:确认上传 (Step 6: Confirm the upload)
Lastly, go back to your GitHub repository page to confirm that Git has successfully pushed your local Git directory to the remote repository.
最后,返回到GitHub存储库页面,以确认Git已成功将本地Git目录推送到远程存储库。
Note:
注意:
- You may need to refresh the remote repository's page for the changes to reflect. 您可能需要刷新远程存储库的页面以反映更改。
- GitHub also has a free optional facility to convert your remote repository into a functional website. Let see “how” below. GitHub还具有免费的可选功能,可将您的远程存储库转换为功能正常的网站。 让我们在下面看到“如何”。
使用GitHub页面发布您的网站 (Publish your website with GitHub pages)
After pushing your project to your remote repository, you can easily publish it on the web like so:
将项目推送到远程存储库后,您可以轻松地将其发布到Web上,如下所示:
Make sure that the name of the main HTML file of your project is
index.html
.确保项目的主HTML文件的名称为
index.html
。On GitHub’s website platform, go into the repository of the project you want to publish and click the repository’s settings tab.
在GitHub的网站平台上,进入要发布的项目的存储库,然后单击存储库的设置选项卡 。
Scroll down to the GitHub Pages section and change the Source branch from none to master.
向下滚动至GitHub Pages部分,并将Source分支从none更改为master 。
Afterward, a notification saying, “Your site is published at https://your-username.github.io/your-github-repo-name/” will display.
之后,将显示一条通知,指出“您的网站发布在https://your-username.github.io/your-github-repo-name/ ”。
- Now you can view — and publicize — your project at the specified URL. 现在,您可以在指定的URL上查看并公开您的项目。
This section has merely scratched the surface of publishing your project with GitHub. To learn more about GitHub pages, check out this “Working with GitHub Pages” documentation.
本节仅介绍了使用GitHub发布项目的内容。 要了解有关GitHub页面的更多信息,请查看此“ 使用GitHub页面 ”文档。
简而言之 (In short)
GitHub is an online platform for hosting (or sharing) Git repositories. It helps you create an avenue to collaborate easily on projects with anyone, at any place, at any time.
GitHub是一个用于托管(或共享)Git存储库的在线平台。 它可以帮助您创建一种途径,可以随时随地与任何人轻松地就项目进行协作。
还是有疑问吗? (Still in doubt?)
Are you still perplexed about the fine line between Git and GitHub? Don’t worry — I have you covered. Below are five key differences between Git and GitHub.
您是否仍然对Git和GitHub之间的界限感到困惑? 不用担心-我已经覆盖了您。 以下是Git与GitHub之间的五个主要区别。
差异1:Git与GitHub —主要功能 (Difference 1: Git vs. GitHub — Primary function)
Git is a distributed version control system that records different versions of a file (or set of files). It lets users access, compare, update, and distribute any of the recorded version(s) at any time.
Git是一个分布式版本控制系统,它记录文件(或文件集)的不同版本。 它使用户可以随时访问,比较,更新和分发任何记录的版本。
However, GitHub is mainly a hosting platform for hosting Git repositories online. It lets users keep their remote repository private or open for collaborative endeavors.
但是, GitHub主要是用于在线托管Git存储库的托管平台。 它使用户可以将其远程存储库保持私有状态或开放以进行协作。
差异2:Git与GitHub —操作平台 (Difference 2: Git vs. GitHub — Operation platform)
Users install and operate Git on their local machines. This means that most of Git's operations are achievable without the internet.
用户在其本地计算机上安装和操作Git。 这意味着无需互联网即可实现Git的大部分操作。
GitHub, however, is a web-based service that operates solely online. This means that you need the internet to do anything on GitHub.
但是,GitHub是一种基于Web的服务,仅在线运行。 这意味着您需要互联网才能在GitHub上执行任何操作。
差异3:Git与GitHub —发明家 (Difference 3: Git vs. GitHub — Inventors)
Linus Torvalds began the development of Git in April, 2005.
Linus Torvalds于2005年4月开始开发Git。
Chris Wanstrath, P. J. Hyett, Tom Preston-Werner, and Scott Chacon founded GitHub.com in February, 2008.
Chris Wanstrath,PJ Hyett,Tom Preston-Werner和Scott Chacon于2008年2月成立了GitHub.com。
差异4:Git与GitHub —维护者 (Difference 4: Git vs. GitHub — Maintainers)
In July 2005, Linus Torvalds turned over Git's maintenance to Junio C. Hamano — who has been the chief maintainer since then.
2005年7月,Linus Torvalds将Git的维护工作移交给了Junio C. Hamano,后者从那时起一直担任首席维护者。
And Microsoft acquired GitHub in October, 2018.
微软于2018年10月收购了GitHub。
差异5:Git与GitHub —竞争对手 (Difference 5: Git vs. GitHub — Competitors)
Popular alternatives to Git are Mercurial, Team Foundation Version Control (TFVC), Perforce Helix Core, Apache Subversion, and IBM Rational ClearCase.
Git的流行替代品是Mercurial,Team Foundation版本控制(TFVC),Perforce Helix Core,Apache Subversion和IBM Rational ClearCase。
GitHub’s closest competitors are GitLab, Bitbucket, SourceForge, Cloud Source Repositories, and AWS CodeCommit.
GitHub上最接近的竞争对手是GitLab,Bitbucket,SourceForge,Cloud Source Repositories和AWS CodeCommit。
总而言之 (All in all)
Git and GitHub are two different entities that help you manage and host files. In other words, Git serves to control file versions while GitHub is a platform for hosting Git repositories.
Git和GitHub是两个不同的实体,可帮助您管理和托管文件。 换句话说,Git用于控制文件版本,而GitHub是托管Git存储库的平台。
有用的资源 (Useful resource)
翻译自: https://www.freecodecamp.org/news/git-and-github-overview/