当前位置:   article > 正文

Git系列教程(一)——git版本管理

git版本管理

一、Git简介

Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆,在本地机器上拷贝一个完整的Git仓库。


二、创建版本库

什么是版本库(repository)?简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,Git能对每个文件的修改、删除进行跟踪,以便在将来某个时刻可以进行还原。那么如何创建一个版本库呢?

第一步,创建一个空目录(非空目录也可以),Windows系统下请确保路径不包含中文。

$ mkdir test
$ cd test
  • 1
  • 2

第二步,通过git init命令将目录变成Git仓库

$ git init
Initialized empty Git repository in C:/Users/yyz/Desktop/test/.git/
  • 1
  • 2

此时一个普通目录就变成了了Git仓库,可以发现目录下多了一个.git文件夹,这个目录是Git来跟踪管理版本库的。


三、添加文件到版本库

现在向test目录或其子目录下创建一个 readme.txt 文件,因为这是一个Git仓库,放到其他地方Git找不到这个文件。内容如下:

Git is a version control system.
Git is free software.
  • 1
  • 2

第一步,用命令git add把文件添加到Git仓库:

$ git add readme.txt
  • 1

第二步,用命令git commit把文件提交到Git仓库(-m后面输入的是本次提交的说明)

$ git commit -m "wrote a readme file"
[master (root-commit) eaadf4e] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt
  • 1
  • 2
  • 3
  • 4

执行git commit命令后会告诉你,1 file changed:1个文件被改动(新添加的readme.txt文件);2 insertions:插入了两行内容(readme.txt有两行内容)。

为什么Git添加文件需要addcommit一共两步呢?因为commit可以一次提交很多文件,所以你可以多次add不同的文件,比如:

$ git add file1.txt
$ git add file2.txt file3.txt
$ git commit -m "add 3 files."
  • 1
  • 2
  • 3

四、版本管理

现在已经成功添加并提交了一个 readme.txt 文件,接着继续修改 readme.txt,修改内容如下:

Git is a distributed version control system.
Git is free software.
  • 1
  • 2

运行git status命令查看状态:

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

git status命令可以查看仓库当前的状态,通过输出得知 readme.txt 被修改过了,但还没有准备提交的修改。

假如你休假两周之后继续上班,但已经记不清上次怎么修改的 readme.txt,这时可以用git diff这个命令看看:

$ git diff readme.txt 
diff --git a/readme.txt b/readme.txt
index 46d49bf..9247db6 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a version control system.
+Git is a distributed version control system.
 Git is free software.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

git diff即查看difference,显示的格式是Unix通用的diff格式,通过输出看到,我们在第一行添加了一个distributed单词。现在就知道了上一次对 readme.txt 做了什么修改,现在对文件进行提交。

$ git add readme.txt
  • 1

执行git commit之前,再查看一下当前仓库的状态

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   readme.txt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

git status告诉我们,将要被提交的修改包括 readme.txt,下一步提交

$ git commit -m "add distributed"
[master e475afc] add distributed
 1 file changed, 1 insertion(+), 1 deletion(-)
  • 1
  • 2
  • 3

提交后,再用git status命令查看仓库的当前状态:

$ git status
On branch master
nothing to commit, working tree clean
  • 1
  • 2
  • 3

Git告诉我们当前没有需要提交的修改,工作目录是干净的。


五、版本回退

现在继续修改 readme.txt,内容如下:

Git is a distributed version control system.
Git is free software distributed under the GPL.
  • 1
  • 2

然后提交:

$ git add readme.txt
$ git commit -m "append GPL"
[master 1094adb] append GPL
 1 file changed, 1 insertion(+), 1 deletion(-)
  • 1
  • 2
  • 3
  • 4

可以把Git中的commit理解成“快照”,每当觉得文件修改到一定程度的时候,就可以保存一个快照,当你把文件改乱或者误删,还可以从最近的一个commit恢复。

在实际工作中,我们怎么知道 readme.txt 文件一共有几个版本被提交到Git仓库里了呢?这时就用到git log命令了。

$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 21:06:15 2018 +0800

    append GPL

commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 21:03:36 2018 +0800

    add distributed

commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 20:59:18 2018 +0800

    wrote a readme file
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

git log命令显示从最近到最远的提交日志,我们可以看到3次提交,最近的一次是append GPL,上一次是add distributed,最早的一次是wrote a readme file

如果嫌输出信息太多,可以加上--pretty=oneline参数:

$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
  • 1
  • 2
  • 3
  • 4

上面一大串类似1094adb...的是commit id(版本号),它是一个SHA1计算出来的一个非常大的数字,因为Git是分布式的版本控制系统,在工作中可能多人在同一个版本库里工作,如果大家都用1,2,3……作为版本号就会发生冲突。

那么如何把 readme.txt 回退到上一个版本,即add distributed版本呢?

首先,Git必须知道当前版本是哪个版本,在Git中用HEAD表示当前版本,也就是最新的提交1094adb...,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100

要把当前版本append GPL回退到上一个版本add distributed,就可以使用git reset命令:

$ git reset --hard HEAD^
HEAD is now at e475afc add distributed
  • 1
  • 2

查看 readme.txt 的内容可以发现已经回到上一个版本了。

$ cat readme.txt
Git is a distributed version control system.
Git is free software.
  • 1
  • 2
  • 3

git log再看看现在版本库的状态:

$ git log
commit e475afc93c209a690c39c13a46716e8fa000c366 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 21:03:36 2018 +0800

    add distributed

commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 20:59:18 2018 +0800

    wrote a readme file
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

会发现最新的那个版本append GPL不见了,好比你从21世纪坐时光穿梭机来到了19世纪,想再回去已经回不去了,怎么办?

其实只要上面的命令行窗口还没有被关掉,你就可以找到append GPLcommit id,然后回到未来的版本:

$ git reset --hard 1094a
HEAD is now at 83b0afe append GPL
  • 1
  • 2

版本号写前几位就可以,Git会自动去找(不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个)

再次查看 readme.txt 的内容,发现果然回来了。

$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
  • 1
  • 2
  • 3

假如你回退到了某个版本,关掉了电脑,第二天想恢复到新版本但找不到新版本的commit id怎么办?

Git提供了一个命令git reflog用来记录你的每一次命令:

$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
  • 1
  • 2
  • 3
  • 4
  • 5

可以看到append GPL的commit id是1094adb,现在又可以回到新版本了。

Git的版本回退速度之所以快,是因为Git在内部有个指向当前版本的HEAD指针,当回退版本的时候,Git仅仅是把HEAD从指向append GPL改为指向add distributed,顺便把工作区的文件更新了。

┌────┐
│HEAD│
└────┘
   │
   └──> ○ append GPL
        │
        ○ add distributed
        │
        ○ wrote a readme file
--------------------------------------
# 改为指向 add distributed 

┌────┐
│HEAD│
└────┘
   │
   │    ○ append GPL
   │    │
   └──> ○ add distributed
        │
        ○ wrote a readme file
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

六、总结

  • 使用命令git init,初始化Git仓库

  • 使用命令git add <file>,可反复多次使用,添加多个文件

  • 使用命令git commit -m <message>完成提交

  • 使用命令git status,查看工作区状态

  • 如果git status告诉你有文件被修改过,用git diff可以查看修改内容

  • HEAD指向当前版本,使用命令git reset --hard commit_id可以切换版本

  • 穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本

  • 要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本


感谢大家的耐心阅读,如有建议请私信或评论留言

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/blog/article/detail/40617
推荐阅读
相关标签
  

闽ICP备14008679号