git 使用简介_Git简介:它是什么,以及如何使用它

git 使用简介

Git is an Open Source Distributed Version Control System. Now that’s a lot of words to define Git.

Git是一个开源的分布式版本控制系统 。 现在定义Git的话很多。

Let me break it down and explain the wording:

让我分解一下并解释其措辞:

  • Control System: This basically means that Git is a content tracker. So Git can be used to store content — it is mostly used to store code due to the other features it provides.

    控制系统:这基本上意味着Git是内容跟踪器。 因此,Git可用于存储内容-由于它提供的其他功能,它通常用于存储代码。

  • Version Control System: The code which is stored in Git keeps changing as more code is added. Also, many developers can add code in parallel. So Version Control System helps in handling this by maintaining a history of what changes have happened. Also, Git provides features like branches and merges, which I will be covering later.

    版本控制系统 :随着更多代码的添加,存储在Git中的代码会不断变化。 而且,许多开发人员可以并行添加代码。 因此,版本控制系统通过保持已发生更改的历史记录来帮助处理此问题。 另外,Git提供了诸如分支和合并之类的功能,我将在稍后介绍。

  • Distributed Version Control System: Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. Git is a Distributed Version Control System since the code is present in every developer’s computer. I will explain the concept of remote and local repositories later in this article.

    分布式版本控制系统 :Git有一个存储在服务器中的远程存储库和一个存储在每个开发人员的计算机中的本地存储库。 这意味着代码不仅存储在中央服务器中,而且代码的完整副本也存在于所有开发人员的计算机中。 Git是一个分布式版本控制系统,因为代码存在于每个开发人员的计算机中。 我将在本文后面解释远程存储库和本地存储库的概念。

为什么需要像Git这样的版本控制系统 (Why a Version Control System like Git is needed)

Real life projects generally have multiple developers working in parallel. So a version control system like Git is needed to ensure there are no code conflicts between the developers.

现实生活中的项目通常有多个开发人员并行工作。 因此,需要一个类似Git的版本控制系统来确保开发人员之间没有代码冲突。

Additionally, the requirements in such projects change often. So a version control system allows developers to revert and go back to an older version of the code.

此外,此类项目中的要求经常更改。 因此,版本控制系统允许开发人员还原并返回到旧版本的代码。

Finally, sometimes several projects which are being run in parallel involve the same codebase. In such a case, the concept of branching in Git is very important.

最后,有时并行运行的几个项目涉及相同的代码库。 在这种情况下,Git中的分支概念非常重要。

现在开始使用Git (Let’s get started on using Git now)

Rather than mentioning all the concepts at once, I will explain the concepts of Git through an example so that it is easier to follow.

我将不通过示例来解释Git的概念,而是让它更容易理解,而不是一次提及所有概念。

下载git (Download git)

This link has details on how to install Git in multiple operating systems:https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

该链接包含有关如何在多种操作系统中安装Git的详细信息: https : //git-scm.com/book/en/v2/Getting-Started-Installing-Git

Verify if Git is installed by using the following command in the command prompt:

通过在命令提示符下使用以下命令来验证是否已安装Git:

git --version
创建本地Git存储库 (Create your local Git repository)

In your computer, create a folder for your project. Let’s call the project folder simple-git-demo.

在您的计算机上,为您的项目创建一个文件夹。 我们将项目文件夹称为simple-git-demo

Go into your project folder and add a local Git repository to the project using the following commands:

进入您的项目文件夹,然后使用以下命令将本地Git存储库添加到项目中:

cd simple-git-demo
git init

The git init command adds a local Git repository to the project.

git init命令将本地Git存储库添加到项目中。

让我们现在添加一些小代码 (Let’s Add some Small Code now)

Create a file called demo.txt in the project folder and add the following text into it:

在项目文件夹中创建一个名为demo.txt的文件,并将以下文本添加到其中:

Initial Content

Here we will be demoing with just plain text instead of actual code, since the main focus of this article is on Git and not on any specific programming language.

在这里,我们将仅用纯文本而不是实际代码进行演示,因为本文的主要重点是Git,而不是任何特定的编程语言。

暂存和提交代码 (Staging and Committing the code)

Committing is the process in which the code is added to the local repository. Before committing the code, it has to be in the staging area. The staging area is there to keep track of all the files which are to be committed.

提交是将代码添加到本地存储库的过程 。 在提交代码之前,它必须位于暂存区域中 。 暂存区域用于跟踪要提交的所有文件。

Any file which is not added to the staging area will not be committed. This gives the developer control over which files need to be committed.

未添加到暂存区的任何文件都不会提交。 这使开发人员可以控制需要提交的文件。

分期 (Staging)

Use the following command for staging the file:

使用以下命令暂存文件:

git add demo.txt

In case you want to add multiple files you can use:

如果要添加多个文件,可以使用:

git add file1 file2 file3

git add file1 file2 file3

If you want to add all the files inside your project folder to the staging area, use the following command:

如果要将项目文件夹中的所有文件添加到暂存区,请使用以下命令:

git add .

Use this carefully since it adds all the files and folders in your project to the staging area.

请谨慎使用,因为它将项目中的所有文件和文件夹添加到暂存区。

提交中 (Committing)

Use the following command to commit the file:

使用以下命令来提交文件:

git commit -m "Initial Commit"

“Initial Commit” is the commit message here. Enter a relevant commit message to indicate what code changes were done in that particular commit.

“初始提交”是此处的提交消息。 输入相关的提交消息以指示在该特定提交中进行了哪些代码更改。

Git状态和Git日志 (Git Status and Git Log)

Now modify the demo.txt file and add the following snippet:

现在修改demo.txt 文件并添加以下代码段:

Initial Content Adding more Content
状态 (Status)

Use git status to find out information regarding what files are modified and what files are there in the staging area — it shows other information as well, which we can ignore for now.

使用git status可以找到有关已修改哪些文件以及登台区域中有哪些文件的信息-它还显示了其他信息,我们现在可以忽略这些信息。

Use the following command to see the status:

使用以下命令查看状态:

git status

The status shows that demo.txt is modified and is not yet in the staging area.

状态显示demo.txt已被修改,尚未位于暂存区域中。

Now let us add demo.txt to the staging area and commit it using the following commands:

现在,让我们将demo.txt添加到临时区域,并使用以下命令来提交它:

git add demo.txt git commit -m "demo.txt file is modified"

记录 (Log)

Use git log to print out all the commits which have been done up until now.

使用git log打印出到目前为止所有已完成的提交。

The command used for this is:git log

用于此的命令是: git log

The log shows the author of each commit, the date of the commit, and the commit message.

日志显示每个提交的作者,提交日期和提交消息。

分行 (Branches)

Up until now we have not created any branch in Git. By default, Git commits go into the master branch.

到目前为止,我们尚未在Git中创建任何分支。 默认情况下,Git提交进入master分支。

什么是分支? (What is a branch?)

A branch is nothing but a pointer to the latest commit in the Git repository. So currently our master branch is a pointer to the second commit “demo.txt file is modified”.

分支不过是指向Git存储库中最新提交的指针。 因此,当前我们的master分支是指向第二个提交“demo.txt file is modified”的指针。

为什么需要多个分支? (Why are multiple branches needed?)

Multiple branches are needed to support multiple parallel developments. Refer the image below to see how branches work.

需要多个分支来支持多个并行开发。 请参考下图查看分支如何工作。

Initially, commit 1 and commit 2 were done in the master branch. After commit 2 a new Branch called as “Test” is created, and commit 3 and commit 4 are added to the test branch.

最初,提交1和提交2在master分支中完成。 在提交2之后,将创建一个称为“测试”的新分支,并将提交3和提交4添加到测试分支。

At the same time, a different commit 3 and commit 4 are added to the master branch. Here we can see that after Commit 2, two parallel developments are being done in 2 separate branches.

同时,将不同的提交3和提交4添加到master分支。 在这里,我们可以看到在提交2之后,在2个独立的分支中进行了两个并行开发。

The Test Branch and the Master Branch have diverged here and have different code — the code from Test Branch can be merged with the Master branch using git merge. This will be covered later.

Test分支和Master分支在这里有所不同,并且具有不同的代码-可以使用git merge来自Test Branch的代码与Master分支git merge 。 稍后将介绍。

在本地创建新分支 (Create a New Branch in Local)

Create a new branch called test using the following command:

使用以下命令创建一个名为test的新分支:

git branch test

This command creates the test branch.

此命令创建test分支。

We are still in the context of the master branch. In order to switch to the test branch. use the following command:

我们仍然处于master分支的上下文中。 为了切换到test分支。 使用以下命令:

git checkout test

Now we are in the test branch.

现在我们在test分支中。

You can list out all the branches in local using the following command:

您可以使用以下命令列出本地的所有分支:

git branch
在新分支中做一些承诺 (Do Some Commits in the New Branch)

Modify demo.txt by adding the following snippet:

通过添加以下代码段来修改demo.txt

Initial Content Adding more Content Adding some Content from test Branch

Now stage and commit using the following commands:

现在,使用以下命令暂存并提交:

git add demo.txt git commit -m "Test Branch Commit"

This commit was done in the Test Branch, and now Test Branch is ahead of Master Branch by 1 commit — as the test branch also includes the 2 commits from the master branch.

该提交是在Test分支中完成的,现在Test分支比Master分支高出1个提交,因为Test分支还包括来自master分支的2个提交。

You can verify the commit history in Test Branch using:

您可以使用以下方法在“测试分支”中验证提交历史记录:

git log

合并中 (Merging)

Currently, Test Branch is ahead of the Master by 1 commit. Let’s say that now we want all the code in the Test Branch to be brought back to the Master Branch. This is where git merge is very useful.

目前,Test Branch领先Master 1次提交。 假设现在我们希望将Test分支中的所有代码都带回Master分支。 这是git merge非常有用的地方。

In order to merge the code from the test branch into the master branch, follow these steps:

为了将代码从测试分支合并到master分支,请按照下列步骤操作:

First go back to the master branch:

首先回到master分支:

git checkout master

Then run the merge command:

然后运行merge命令:

git merge test

After running these 2 commands, the merge should be successful. In this example, there are no conflicts.

运行这两个命令后,合并应该成功。 在此示例中,没有冲突。

But in real projects, there will be conflicts when a merge is being done. Resolving the conflict is something which comes with experience, so as you work more with Git you will be able to get the hang of resolving conflicts.

但是在实际项目中,合并完成后会发生冲突。 解决冲突是经验带来的,因此,当您与Git进行更多合作时,您将能够摆脱冲突的束缚。

Run git log now and you will notice that the master also has 3 commits.

运行git log 现在,您会注意到母版也有3个提交。

远程Git存储库 (The Remote Git Repository)

Until now, we have been working only in the local repository. Each developer will work in their local repository but eventually, they will push the code into a remote repository. Once the code is in the remote repository, other developers can see and modify that code.

到目前为止,我们仅在本地存储库中工作。 每个开发人员都将在其本地存储库中工作,但最终,他们会将代码推送到远程存储库中。 一旦代码位于远程存储库中,其他开发人员就可以查看和修改该代码。

的GitHub (GitHub)

Here we will be using GitHub for the remote repository.

在这里,我们将使用GitHub作为远程存储库。

Go to https://github.com/ and create an account.

转到https://github.com/并创建一个帐户。

After registering in the GitHub homepage, click on Start a Project to create a new Git repository. Give the repository a name and click “Create Repository”

在GitHub主页上注册后,单击Start a Project创建一个新的Git存储库。 为存储库命名,然后单击“创建存储库”

Give the name as git-blog-demo.

命名为git-blog-demo

This will create a remote repository in GitHub, and when you open the repository, a page like the image below will open up:

这将在GitHub中创建一个远程存储库,当您打开存储库时,将打开如下图所示的页面:

The repository URL is the highlighted portion https://github.com/aditya-sridhar/git-blog-demo.git

存储库URL是突出显示的部分https://github.com/aditya-sridhar/git-blog-demo.git

In order to point your local repository to the remote repository, use the following command:

为了将本地存储库指向远程存储库,请使用以下命令:

git remote add origin [repository url]
Git推 (Git Push)

In order to push all the code from the local repository into the remote repository, use the following command:

为了将所有代码从本地存储库推送到远程存储库,请使用以下命令:

git push -u origin master

This pushes the code from the master branch in the local repository to the master branch in the remote repository.

这会将代码从本地存储库中的master分支推送到远程存储库中的master分支。

附加命令 (Additional Commands)

吉特拉 (Git Pull)

git pull is used to pull the latest changes from the remote repository into the local repository. The remote repository code is updated continuously by various developers, hence git pull is necessary:

git pull用于将最新的更改从远程存储库中提取到本地存储库中。 远程存储库代码由各种开发人员不断更新,因此需要git pull

git pull origin master
Git克隆 (Git Clone)

git clone is used to clone an existing remote repository into your computer. The command for this is:

git clone用于将现有的远程存储库克隆到您的计算机中。 该命令是:

git clone [repository url]

恭喜 (Congrats)

Now you know the basics of how to use Git, so go ahead and explore more!

现在您已经知道了如何使用Git的基础知识,因此继续进行探索!

I will soon be publishing one more article on slightly more advanced concepts of Git. Stay tuned!

我很快将再发表一篇有关Git的更高级概念的文章。 敬请关注!

关于作者 (About the author)

I love technology and follow the advancements in technology. I also like helping others with any knowledge I have in the technology space.

我热爱技术,并追随技术的进步。 我也喜欢以我在技术领域拥有的任何知识来帮助他人。

Feel free to connect with me on my LinkdIn account https://www.linkedin.com/in/aditya1811/

随时通过我的LinkdIn帐户与我联系https://www.linkedin.com/in/aditya1811/

You can also follow me on twitter https://twitter.com/adityasridhar18

您也可以在Twitter上关注我https://twitter.com/adityasridhar18

My Website: https://adityasridhar.com/

我的网站: https : //adityasridhar.com/

How to use Git efficiently

如何有效使用Git

翻译自: https://www.freecodecamp.org/news/what-is-git-and-how-to-use-it-c341b049ae61/

git 使用简介

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值