博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Git简介,第1部分
阅读量:2514 次
发布时间:2019-05-11

本文共 16525 字,大约阅读时间需要 55 分钟。

Git is a (distributed) version control system. What is that? A version control system is software that allows a programmer to track and manage the history of a project, where such a project could be a single file, a set of files, or an entire open source project with multiple programmers contributing from around the world.

Git是一个(分布式)版本控制系统。 那是什么? 版本控制系统是一种软件,它使程序员可以跟踪和管理项目的历史记录,其中这样的项目可以是一个文件,一组文件或整个开源项目,其中有来自世界各地的多个程序员。

The distinction of “distributed” means that there is no central server where the source code and history of a project lives. When a Git project is cloned (“clone” is Git’s word for the initial checkout of a project), you receive the entire history of the project from the current state all the way back to the beginning of time. Having the entire project stored locally on your computer without a central server coordinating everything makes for extremely fast merging, diffing, and log history lookups.

“分布式”的区别意味着没有源代码和项目历史记录存在的中央服务器。 克隆了Git项目后(“克隆”是Git表示项目的初始签出的字眼),您会收到从当前状态一直到时间开始的整个项目历史记录。 将整个项目本地存储在您的计算机上,而无需中央服务器来协调所有内容,因此可以非常快速地进行合并,区分和日志历史记录查找。

Some of the more commonly used version control systems that you have likely heard about are CVS and Subversion. This tutorial will take a “forget everything you know about CVS or Subversion” approach. As someone who has used all three of these systems in the professional realm, I can testify that some knowledge of CVS or Subversion can be useful when approaching Git, but it is not necessary. The best way to learn Git is to start using Git for what Git is.

您可能听说过的一些更常用的版本控制系统是CVS和Subversion。 本教程将采用“忘记有关CVS或Subversion的所有知识”方法。 作为在专业领域中同时使用所有这三个系统的人,我可以证明在接近Git时对CVS或Subversion的一些了解可能是有用的,但不是必须的。 学习Git的最好方法是开始使用Git。

为什么要使用Git? (Why use Git?)

That’s a perfectly cromulent question. If I am just one person working on my own website, why should I bother with version control? I have a pretty good memory and I know others won’t be overwriting my code.

这是一个非常棘手的问题。 如果我只是在自己的网站上工作的一个人,为什么还要麻烦版本控制? 我的记忆力很好,我知道其他人不会覆盖我的代码。

Maybe you have a set of files for a website you are developing for a client, looking something like this.

也许您正在为客户开发的网站有一组文件,看起来像这样。

sean@beerhaus:~/Workspace$ lsimages/  templates/  index.php.save1  index.php.save3js/      index.php   index.php.save2  index.php.save4

Here, index.php is your current file and index.php.save# are a set of backups from previous days’ work.

在这里, index.php是当前文件,而index.php.save#是前几天工作的一组备份。

Now you are editing index.php. You have just added a pretty neat feature after an overdue but deserved moment of inspiration. To be smart, you make a backup of this file called index.php.save5. The next day you go back to work on index.php and you decide the new feature from yesterday isn’t really going to work and you remove it from the file. A few weeks go by and by this time you’ve saved index.php.save27. The current state of the project is pretty stable so you no longer need the previous 27 saved files, and so you delete them all.

现在,您正在编辑index.php 。 在过期但应得的启发之后,您刚刚添加了一个非常简洁的功能。 为聪明起见,请备份此文件的index.php.save5 。 第二天,您回到index.php上工作,然后决定昨天起的新功能将无法使用,并将其从文件中删除。 到现在index.php.save27已经有几个星期了,您已经保存了index.php.save27 。 项目的当前状态非常稳定,因此您不再需要之前保存的27个文件,因此将它们全部删除。

Several days after your cleanup you feel that feature from index.php.save5 deserves a second chance, but now it is gone forever. Sure, you might have some extra backup copies, and you might even have a file undelete tool. But is that really the best way to be managing your source code?

清理几天后,您会觉得index.php.save5中的功能值得第二次机会,但是现在它永远消失了。 当然,您可能会有一些额外的备份副本,甚至可能有文件取消删除工具。 但这真的是管理源代码的最佳方法吗?

The problem gets worse when you add more people to your project. Even with two people working on the same project, there will be some unintentionally deleted and overwritten files. Maybe you email tarballs of the source code back and forth and you can look up and old email to get the archive with files you have just deleted. Good luck.

当您向项目中添加更多人员时,问题变得更加严重。 即使有两个人在同一个项目上工作,也会有一些无意中删除和覆盖的文件。 也许您通过电子邮件来回发送源代码的压缩文件,并且可以查找和使用旧电子邮件来获取包含刚删除的文件的存档。 祝好运。

This is the part where 99 out of 100 tutorials would predictably repeat the prized cliche “enter Git.”

这是100个教程中的99个可以预测地重复珍贵的陈词滥调“输入Git”的部分。

如何使用Git (How to use Git)

The first step in using Git is to get Git. If you are using an a variety of Linux, this is pretty easy. (Related fact: Git was initially designed and developed for distributed development on the Linux kernel by the same guy who created the kernel, Linus Torvalds.) Whatever distribution of Linux you are running, the package you are going to need will most likely be called “git” or “git-core”. If you are using Windows or Mac, or would just like to build Git from source, see for download and installation options.

使用Git的第一步是获取Git。 如果您使用各种Linux,这将非常容易。 (相关事实:Git最初是由创建内核的同一人Linus Torvalds设计和开发的,用于Linux内核上的分布式开发。)无论您运行的是哪种Linux发行版,都将很可能需要使用所需的软件包。 “ git”或“ git-core”。 如果您使用Windows或Mac,或者只是想从源代码构建Git,请访问以获取下载和安装选项。

创建一个仓库 (Creating a Repository)

Once you have Git installed you’ll want to add some information about yourself. This will be helpful in identifying who has committed what code when looking at the Git log for history information.

一旦安装了Git,您将需要添加一些有关您自己的信息。 这有助于在查看Git日志中的历史记录信息时识别谁提交了什么代码。

sean@beerhaus:~$ git config --global user.name "FirstName LastName"sean@beerhaus:~$ git config --global user.email "your@email"

Now it’s time to create a repository. There are two ways to do this, but essentially one command. If you are starting a new project from scratch and you would like Git to look after it during its whole life span, you can create your directory that will contain all project files and initialize the directory for use with Git:

现在该创建存储库了。 有两种方法可以执行此操作,但是本质上是一种命令。 如果您是从头开始一个新项目,并且希望Git在其整个生命周期内进行维护,则可以创建包含所有项目文件的目录,并初始化该目录以供Git使用:

sean@beerhaus:~/$ mkdir new_projectsean@beerhaus:~/$ cd new_project/sean@beerhaus:~/new_project$ git initInitialized empty Git repository in /home/sean/new_project/.git/

You now have an empty Git repository ready for use. If you run a listing of all hidden files in your new directory, you will notice the hidden directory .git was created:

现在,您可以使用一个空的Git存储库。 如果运行新目录中所有隐藏文件的列表,您会注意到隐藏目录.git已创建:

sean@beerhaus:~/new_project$ ls -altotal 12drwxr-xr-x 3 sean sean 4096 2011-11-19 16:09 .drwxr-xr-x 5 sean sean 4096 2011-11-19 16:09 ..drwxr-xr-x 7 sean sean 4096 2011-11-19 16:09 .git

The .git directory is where Git stores all the information and history it needs to track your repository, and so it’s best to leave it alone.

.git目录是Git用来存储跟踪存储库所需的所有信息和历史记录的位置,因此最好不要管它。

git init will also work if you have a directory of existing source code. Simply enter your directory and run git init as I did in this example.

如果您已有现有源代码目录,则git init也将起作用。 只需输入您的目录,然后像本例中那样运行git init

添加/暂存文件 (Adding/Staging Files)

You should still be in your new_project directory that you’ve just initialized for Git. In version control terminology, this is now called a working directory, meaning it is a directory that is under the watch of some version control system.

您仍应位于刚刚为Git初始化的new_project目录中。 在版本控制术语中,这现在称为工作目录 ,这意味着它是某个版本控制系统监视下的目录。

Let’s create some files for Git to track, starting with a configuration file. Create the file config.php with the following contents in your editor of choice:

让我们从配置文件开始创建一些文件供Git跟踪。 在您选择的编辑器中,使用以下内容创建文件config.php

"mysql", "host" => "locahost", "username" => "user", "password" => "pass", "database" => "new_project");

Once you have saved the file as config.php we can run another Git command, git status.

将文件另存为config.php我们可以运行另一个Git命令git status

sean@beerhaus:~/new_project$ git status# On branch master## Initial commit## Untracked files:#   (use "git add 
..." to include in what will be committed)## config.phpnothing added to commit but untracked files present (use "git add" to track)

The default branch in Git is known as master. This is the same idea as “trunk” in other version control systems. A branch is simply a line of development that Git is tracking.

Git中的默认分支称为master 。 这与其他版本控制系统中的“主干”相同。 分支只是Git跟踪的一条发展线。

Untracked files are files that exist in your working directory, but are not yet tracked by Git because you have not yet told Git to track them.

未跟踪的文件是存在于您的工作目录中的文件,但是尚未被Git跟踪,因为您尚未告知Git对其进行跟踪。

The output of git status also provides helpful messages, like the message one line down:

git status的输出还提供有用的消息,例如一行以下的消息:

(use "git add 
..." to include in what will be committed)

Go ahead and add congif.php file to Git’s list of tracked files.

继续,并将congif.php文件添加到Git的跟踪文件列表中。

sean@beerhaus:~/new_project$ git add config.php

Now if you run git status again you’ll see what has changed.

现在,如果再次运行git status ,您将看到发生了什么变化。

sean@beerhaus:~/new_project$ git status# On branch master## Initial commit## Changes to be committed:#   (use "git rm --cached 
..." to unstage)## new file: config.php#

config.php is now being tracked by Git, as you can see by the change from “untracked file” to “new file”.

Git现在正在跟踪config.php ,从“未跟踪的文件”到“新文件”的更改可以看到。

With Git, adding files in this fashion is known as staging. The staging area is the step after a file has been added but before it has been committed. The term adding does not have to refer to a new file. It can also mean a set of changes made to an existing file that will be included in the next commit. The staging area allows us to pick and choose which files to include in a commit before the commit takes place.

使用Git,以这种方式添加文件被称为staging 。 暂存区是添加文件之后但提交之前的步骤。 术语“ 添加”不必引用新文件。 它还可能意味着对现有文件进行的一组更改,这些更改将包含在下一次提交中。 暂存区域使我们可以在提交发生之前选择并选择要包含在提交中的文件。

提交文件 (Committing Files)

Now you can commit the file. Committing a file is how you tell Git to record a “snapshot” of the current state of your project. It is important that with each commit you provide a commit message. The message is helpful to remind yourself (or others) later what the purpose of each change was. You should keep it short but informative.

现在您可以提交文件了。 提交文件是告诉Git如何记录项目当前状态的“快照”。 重要的是,每次提交时都必须提供一条提交消息。 该消息有助于以后提醒自己(或其他人)每次更改的目的。 您应该使它简短但有用。

The quickest way to provide a commit message is to use the -m option with the git commit command. If you leave the -m option out of the command, an editor window will pop up and you can type your commit message there.

提供提交消息的最快方法是将-m选项与git commit命令一起使用。 如果您在命令中未使用-m选项,则会弹出一个编辑器窗口,您可以在其中键入提交消息。

To configure which editor will be used, you can run git config --global core.editor vim. In my case I use vim, but emacs, pico, or any other editor should do.

要配置将使用的编辑器,可以运行git config --global core.editor vim 。 就我而言,我使用vim,但是emacs,pico或任何其他编辑器都应该这样做。

sean@beerhaus:~/new_project$ git commit config.php -m "Initial commit. Added a configuration file."[master (root-commit) c1d55de] Initial commit. Added a configuration file. 1 files changed, 7 insertions(+), 0 deletions(-) create mode 100644 config.php

Now if you run git status again you’ll get a message that tells you there have been no new changes since the last commit.

现在,如果再次运行git status ,将会收到一条消息,告诉您自上次提交以来没有任何新更改。

sean@beerhaus:~/new_project$ git status# On branch masternothing to commit (working directory clean)

查看项目历史 (Viewing Project History)

The git log command is a very helpful tool for viewing the timeline of your project. You can see each commit in detail, including the date and and who made the commit.

git log命令是查看项目时间线的非常有用的工具。 您可以详细查看每个提交,包括日期和提交者。

sean@beerhaus:~/new_project$ git logcommit c1d55debc7be8f50e363df462f84672ad029b703Author: FirstName LastName 
Date: Sat Nov 19 16:45:35 2011 -0400 Initial commit. Added a configuration file.

There are a few things to note here:

这里有几件事要注意:

  1. The name and email address is the information you provided with git config.

    名称和电子邮件地址是git config提供的信息。

  2. The commit message is the message you provided in the first commit.

    提交消息是您在第一次提交中提供的消息。
  3. The word “commit” is followed by a 40-digit hex string.

    单词“ commit”后跟一个40位十六进制字符串。

The 40-character string of hexadecimal numbers is known as commit hash, in reference to the SHA1 cryptographic hash function used to generate it. All information related to a commit is stored in a commit object. The commit hash is the output of the hash function applied to the contents of the object, and is then used as a unique reference to that particular commit.

参照用于生成它的SHA1密码哈希函数,该由40个字符组成的十六进制字符串称为commit hash 。 与提交有关的所有信息都存储在提交对象中。 提交哈希是应用于对象内容的哈希函数的输出,然后用作对该特定提交的唯一引用。

If you wanted to look up the log entry for a single commit, you can provide the commit hash:

如果要查找单个提交的日志条目,则可以提供提交哈希:

sean@beerhaus:~/new_project$ git log c1d55debc7be8f50e363df462f84672ad029b703

To see which files were involved in a commit, you can use the --stat option:

要查看提交中涉及哪些文件,可以使用--stat选项:

sean@beerhaus:~/new_project$ git log --stat c1d55debc7be8f50e363df462f84672ad029b703

The log message now contains:

日志消息现在包含:

config.php |    7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-)

查看提交差异 (Viewing Commit Diffs)

Now lets makes some changes to config.php in order to see what git diff has to offer. Start with a simple change; change the database username to “Foo”. When you save the file and run git status, this time you’ll see config.php is now in the modified state:

现在让我们对config.php进行一些更改,以查看git diff必须提供的功能。 从简单的更改开始; 将数据库用户名更改为“ Foo”。 当您保存文件并运行git status ,这次您将看到config.php现在处于修改状态:

sean@beerhaus:~/new_project$ git status# On branch master# Changes not staged for commit:#   (use "git add 
..." to update what will be committed)# (use "git checkout --
..." to discard changes in working directory)## modified: config.php#no changes added to commit (use "git add" and/or "git commit -a")

To see what has been modified, use git diff.

要查看已修改的内容,请使用git diff

sean@beerhaus:~/new_project$ git diffdiff --git a/config.php b/config.phpindex ab6e11d..02d30f8 100644--- a/config.php+++ b/config.php@@ -2,6 +2,6 @@ $database = array(     "driver"   => "mysql",     "host"     => "locahost",-    "username" => "user",+    "username" => "Foo",     "password" => "pass",     "database" => "new_project");

Your changes are pretty easy to spot. The leading minus tells you the line has been removed, and the leading plus tells you the line has been added.

您的更改非常容易发现。 前减号告诉您该行已删除,而前加号告诉您该行已添加。

The command git diff by default will compare the current state of your project with the previous commit. You can pass arguments to git diff command, such as a commit hash or the special pointer HEAD, which refers to the most recent commit of your current project.

默认情况下,命令git diff将比较项目的当前状态和上一次提交。 您可以将参数传递给git diff命令,例如提交哈希或特殊指针HEAD ,它指向当前项目的最新提交。

Go ahead and commit the changed file so you can compare some commit hashes.

继续并提交更改的文件,以便您可以比较一些提交哈希。

sean@beerhaus:~/new_project$ git commit -am "Changed database username to Foo."[master e69a7f9] Changed database username to Foo. 1 files changed, 1 insertions(+), 1 deletions(-)

When you run git log this time, you’ll see a timeline of changes building up.

这次运行git log时,您会看到建立更改的时间表。

sean@beerhaus:~/new_project$ git logcommit e69a7f9b3b55c116a5c2edf730cd03df7e093edaAuthor: FirstName LastName 
Date: Sun Nov 20 14:16:43 2011 -0400 Changed database username to Foo.commit c1d55debc7be8f50e363df462f84672ad029b703Author: FirstName LastName
Date: Sat Nov 19 16:45:35 2011 -0400 Initial commit. Added a configuration file.

When you run git diff using the two commit hashes, the output is identical to the diff you looked at before the last commit.

使用两个提交哈希值运行git diff ,输出与上次提交之前查看的diff相同。

sean@beerhaus:~/new_project$ git diff c1d55debc7be8f50e363df462f84672ad029b703 e69a7f9b3b55c116a5c2edf730cd03df7e093edadiff --git a/config.php b/config.phpindex ab6e11d..02d30f8 100644--- a/config.php+++ b/config.php@@ -2,6 +2,6 @@ $database = array(     "driver"   => "mysql",     "host"     => "locahost",-    "username" => "user",+    "username" => "Foo",     "password" => "pass",     "database" => "new_project");

Try it again using the --color option. This makes changes a little more easier to see.

使用--color选项再试一次。 这使更改更容易看到。

摘要 (Summary)

After getting used to Git and its cycle of file edits, git add, and git commit, you will never look back to the days of using file backups or your favorite editor’s undo feature as a substitute for proper version control. In this article I showed you the essential commands you’ll need to get started with Git. Other topics including branching, merging, and remote repositories will follow.

在习惯了Git及其文件编辑, git addgit commit周期之后,您再也不会回头使用文件备份或您喜欢的编辑器的undo功能来代替适当的版本控制的日子了。 在本文中,我向您展示了开始使用Git所需的基本命令。 其他主题包括分支,合并和远程存储库。

翻译自:

转载地址:http://lqrgb.baihongyu.com/

你可能感兴趣的文章
Python字典遍历的几种方法
查看>>
js原生实现三级联动下拉菜单
查看>>
[COGS 2066]七十和十七
查看>>
JS 中对变量类型的五种判断方法
查看>>
学习进度十五
查看>>
解决Android Studio启动项目后一直处于refreshing 'View' gradle project,快速解决亲测有效...
查看>>
4.12 | 学习笔记
查看>>
python开发【第一篇】:基础知识
查看>>
javascript的window.onload()方法和jQuery的$(document).ready()的对比
查看>>
mysql数据库维护(备份和还原)和性能提高
查看>>
第八章 springboot + mybatis + 多数据源
查看>>
Arab and North African Region,2002(Snakes & ladders)
查看>>
React中的Refs
查看>>
自己使用MySQL中的GROUP_CONCAT(CONCAT_WS())函数查询的数据显示不全的问题. 以及在后台开发中怎么设置使用....
查看>>
Mysql强制修改密码
查看>>
100
查看>>
新手springmvc web简单搭建过程-caidachun
查看>>
Inline Edit
查看>>
Mybatis generator生成工具简单介绍
查看>>
分布式锁----Redis实现
查看>>