[git入门到应用一天搞定(上)]2019-04-05

2019-04-07  本文已影响0人  Carl_TSNE

准备工作


git简介:

  Git是目前世界上最先进的分布式版本控制系统(没有之一,如果不认可,那就最流行的)。


git应用

  多个人同时协作一个项目,有可能出现两个人同时修改一个文件,这时后提交的人会遇到冲突,需要解决冲突;git能够记录每个人的提交修改等形成日志,可以根据提交记录进行回滚;并且git支持分布式部署。


git诞生

  本着实用主义(虽然感觉忘本,emmm),不说啦!


版本控制系统:集中式vs分布式


git安装

请参考https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137396287703354d8c6c01c904c7d9ff056ae23da865a000



进入正题


创建版本库

#第一步
mkdir learn_git
cd learn_git
pwd
#第二步:通过git init命令把这个目录变成Git可以管理的仓库
git init
git init

添加文件到git仓库

Git is a version control system.
Git is free software.

  一定要放到learn_git目录下(子目录也行),因为这是一个Git仓库,放到其他地方Git再厉害也找不到这个文件。
  和把大象放到冰箱需要3步相比,把一个文件放到Git仓库只需要两步。

git add readme.txt

  执行上面的命令,没有任何显示,这就说明成功添加readme.txt到仓库啦!

git commit -m "wrote a readme file"
git add&commit
git add file1.txt
git add file2.txt file3.txt
git commit -m "add 3 files."


git 工作流程

  继续下一步之前,先来看看git 工作流程


git 工作流
git 工作流
git 命令与流程的对应

git status & git diff

  掌握工作区的状态,使用git status命令;如果git status告诉你有文件被修改过,用git diff可以查看修改内容。
  继续修改readme.txt文件,改成如下内容:

Git is a distributed version control system.
Git is free software.

  结果如下:

EBJ1263:learn_git tao2.zhang$ 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")

  git status命令可以让我们时刻掌握仓库当前的状态,上面的命令输出告诉我们,readme.txt被修改过了,但还没有准备提交的修改。
  虽然Git告诉我们readme.txt被修改了,但如果能看看具体修改了什么内容,自然是很好的。比如你休假两周从国外回来,第一天上班时,已经记不清上次怎么修改的readme.txt,所以,需要用git diff这个命令看看:

git diff

  结果:

EBJ1263:learn_git tao2.zhang$ git diff
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.

  git diff顾名思义就是查看difference,显示的格式正是Unix通用的diff格式,可以从上面的命令输出看到,我们在第一行添加了一个distributed单词。
  知道了对readme.txt作了什么修改后,再把它提交到仓库就放心多了,提交修改和提交新文件是一样的两步,第一步是git add:

git add readme.txt

具体操作命令以及命令结果如下:

EBJ1263:learn_git tao2.zhang$ git add readme.txt 
EBJ1263:learn_git tao2.zhang$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   readme.txt

EBJ1263:learn_git tao2.zhang$ git commit -m "add distributed"
[master 1d8e5ba] add distributed
 1 file changed, 1 insertion(+), 1 deletion(-)
EBJ1263:learn_git tao2.zhang$ git status
On branch master
nothing to commit, working tree clean

总结:


版本回退

  可以继续对read.txt修改提交,练习一下。

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

  结果:

EBJ1263:learn_git tao2.zhang$ vim readme.txt 
EBJ1263:learn_git tao2.zhang$ 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")
EBJ1263:learn_git tao2.zhang$ git diff
diff --git a/readme.txt b/readme.txt
index 9247db6..8443d23 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
 Git is a distributed version control system.
-Git is free software.
+Git is free software distributed under the GPL.
EBJ1263:learn_git tao2.zhang$ git add readme.txt 
EBJ1263:learn_git tao2.zhang$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   readme.txt

EBJ1263:learn_git tao2.zhang$ git diff
EBJ1263:learn_git tao2.zhang$ git commit -m "append GPL"
[master bf93d54] append GPL
 1 file changed, 1 insertion(+), 1 deletion(-)
EBJ1263:learn_git tao2.zhang$ git status
On branch master
nothing to commit, working tree clean

截止目前,已经有三个版本提交到git仓库啦,分别是:
version-1:wrote a readme file
version-2:add distributed
version-3:append GPL
  git log命令显示从最近到最远的提交日志,我们可以看到3次提交,最近的一次是append GPL,上一次是add distributed,最早的一次是wrote a readme file。

EBJ1263:learn_git tao2.zhang$ git log
commit bf93d546ed6573c5742d34135e99f44e57d748bd (HEAD -> master)
Author: CarlBebetter <1393654034@qq.com>
Date:   Sun Apr 7 15:24:39 2019 +0800

    append GPL

commit 1d8e5ba3ffc284075f67e0fc57ef0af4d8922479
Author: CarlBebetter <1393654034@qq.com>
Date:   Sun Apr 7 15:18:21 2019 +0800

    add distributed

commit 1a6f842feeb3f991d1df792e808a25fe6e7aeb5a
Author: CarlBebetter <1393654034@qq.com>
Date:   Sat Apr 6 09:15:44 2019 +0800

    wrote a readme file

  如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:

EBJ1263:learn_git tao2.zhang$ git log --pretty=oneline
bf93d546ed6573c5742d34135e99f44e57d748bd (HEAD -> master) append GPL
1d8e5ba3ffc284075f67e0fc57ef0af4d8922479 add distributed
1a6f842feeb3f991d1df792e808a25fe6e7aeb5a wrote a readme file

注:类似1d8e5ba3ffc284075f67e0fc57ef0af4d8922479的是commit id(版本号)

  现在我们进入版本回退,准备把readme.txt回退到上一个版本,也就是add distributed的那个版本,怎么做呢?
  首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交 bf93d546ed6573c5742d34135e99f44e57d748bd (注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。
  现在,我们要把当前版本append GPL回退到上一个版本add distributed,就可以使用git reset命令并查看read.txt中内容:

EBJ1263:learn_git tao2.zhang$ git reset --hard HEAD^
HEAD is now at 1d8e5ba add distributed
EBJ1263:learn_git tao2.zhang$ cat readme.txt 
Git is a distributed version control system.
Git is free software.

  果然被还原了。
  还可以继续回退到上一个版本wrote a readme file,不过,我们用git log再看看现在版本库的状态:

EBJ1263:learn_git tao2.zhang$ git log
commit 1d8e5ba3ffc284075f67e0fc57ef0af4d8922479 (HEAD -> master)
Author: CarlBebetter <1393654034@qq.com>
Date:   Sun Apr 7 15:18:21 2019 +0800

    add distributed

commit 1a6f842feeb3f991d1df792e808a25fe6e7aeb5a
Author: CarlBebetter <1393654034@qq.com>
Date:   Sat Apr 6 09:15:44 2019 +0800

    wrote a readme file

  最新的那个版本append GPL已经看不到了!好比你从21世纪坐时光穿梭机来到了19世纪,想再回去已经回不去了,肿么办?
  办法其实还是有的,只要上面的命令行窗口还没有被关掉,你就可以顺着往上找啊找啊,找到那个append GPL的commit id是bf93d546ed6573c5742d34135e99f44e57d748bd ,于是就可以指定回到未来的某个版本(git reset --hard bf93d546):

EBJ1263:learn_git tao2.zhang$ git reset --hard bf93d5
HEAD is now at bf93d54 append GPL
EBJ1263:learn_git tao2.zhang$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.

  Git的版本回退速度非常快,因为Git在内部有个指向当前版本的HEAD指针,当你回退版本的时候,Git仅仅是把HEAD从指向append GPL

git-head

改为指向add distributed

git-head-move

  然后顺便把工作区的文件更新了。所以你让HEAD指向哪个版本号,你就把当前版本定位在哪。
  现在,你回退到了某个版本,关掉了电脑,第二天早上就后悔了,想恢复到新版本怎么办?找不到新版本的commit id怎么办?
  在Git中,总是有后悔药可以吃的。当你用$ git reset --hard HEAD^回退到add distributed版本时,再想恢复到append GPL,就必须找到append GPL的commit id。Git提供了一个命令git reflog用来记录你的每一次命令:

EBJ1263:learn_git tao2.zhang$ git reflog
bf93d54 (HEAD -> master) HEAD@{0}: reset: moving to bf93d5
1d8e5ba HEAD@{1}: reset: moving to HEAD^
bf93d54 (HEAD -> master) HEAD@{2}: commit: append GPL
1d8e5ba HEAD@{3}: commit: add distributed
1a6f842 HEAD@{4}: commit (initial): wrote a readme file

总结(版本回退):


工作区和暂存区

  之前有说过git工作流,现在说一下工作区和暂存区。

EBJ1263:learn_git tao2.zhang$ ls
readme.txt
EBJ1263:learn_git tao2.zhang$ ls -a
.       ..      .git        readme.txt

  Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。

工作区&版本库
  分支HEAD的概念我们以后再说。
  前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的:
  第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
  第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
  因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
  可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

  俗话说,实践出真知。现在,我们再练习一遍,先对readme.txt做个修改,比如加上一行内容:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.

然后,在工作区新增一个LICENSE文本文件(内容随便写)。
先用git status查看一下状态:

EBJ1263:learn_git tao2.zhang$ vim readme.txt 
EBJ1263:learn_git tao2.zhang$ vim LICENSE
EBJ1263:learn_git tao2.zhang$ 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

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    LICENSE

no changes added to commit (use "git add" and/or "git commit -a")

  Git非常清楚地告诉我们,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked
  现在,使用两次命令git add,把readme.txt和LICENSE都添加后,用git status再查看一下:

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

    new file:   LICENSE
    modified:   readme.txt

现在,暂存区的状态就变成这样了:

git add后暂存区的状态

  所以,git add命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit就可以一次性把暂存区的所有修改提交到分支。

EBJ1263:learn_git tao2.zhang$ ls
LICENSE     readme.txt
EBJ1263:learn_git tao2.zhang$ git add LICENSE readme.txt 
EBJ1263:learn_git tao2.zhang$ git commit -m "add one line on readme.txt create a new file LICENSE"
[master ead6da7] add one line on readme.txt create a new file LICENSE
 2 files changed, 2 insertions(+)
 create mode 100644 LICENSE
EBJ1263:learn_git tao2.zhang$ git status
On branch master
nothing to commit, working tree clean

一旦提交后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的.
现在版本库变成了这样,暂存区就没有任何内容了:

git-stage-after-commit

管理修改--深入理解

  如果完全掌握了暂存区的概念。下面,我们要讨论的就是,为什么Git比其他版本控制系统设计得优秀,因为Git跟踪并管理的是修改,而非文件。
  什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改。
  为什么说Git管理的是修改,而不是文件呢?我们还是做实验。第一步,对readme.txt做一个修改,比如加一行内容:

$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.

然后,添加:

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

然后,再修改readme.txt:

$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

提交:

$ git commit -m "git tracks changes"
[master 519219b] git tracks changes
 1 file changed, 1 insertion(+)

提交后,再看看状态:

$ 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")

怎么第二次的修改没有被提交?

  别激动,我们回顾一下操作过程:

  第一次修改 -> git add -> 第二次修改 -> git commit
  你看,我们前面讲了,Git管理的是修改,当你用git add命令后,在工作区的第一次修改被放入暂存区,准备提交,但是,在工作区的第二次修改并没有放入暂存区,所以,git commit只负责把暂存区的修改提交了,也就是第一次的修改被提交了,第二次的修改不会被提交。
  提交后,用git diff HEAD -- readme.txt命令可以查看工作区和版本库里面最新版本的区别:

$ git diff HEAD -- readme.txt 
diff --git a/readme.txt b/readme.txt
index 76d770f..a9c5755 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
 Git is a distributed version control system.
 Git is free software distributed under the GPL.
 Git has a mutable index called stage.
-Git tracks changes.
+Git tracks changes of files.

可见,第二次修改确实没有被提交。
那怎么提交第二次修改呢?你可以继续git addgit commit,也可以别着急提交第一次修改,先git add第二次修改,再git commit,就相当于把两次修改合并后一块提交了:

第一次修改 -> git add -> 第二次修改 -> git add -> git commit

好,现在,把第二次修改提交了。

-----------------------版本库--------------------------------------------
                                         a                        a
                                         |                          |
                                 git diff --cached                  |
                                         |                          |
                                         b
----------------暂存区----------------------                      git diff HEAD
                        a
                        |                                            |
                     git diff                                        |
                        |                                            |
                        b                                          b
---------------工作区----------------------------------------------------

现在,你又理解了Git是如何跟踪修改的,每次修改,如果不用git add到暂存区,那就不会加入到commit中。


撤销修改

三种情况:

EBJ1263:learn_git tao2.zhang$ vim readme.txt 
EBJ1263:learn_git tao2.zhang$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
EBJ1263:learn_git tao2.zhang$ 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")
EBJ1263:learn_git tao2.zhang$ git checkout -- readme.txt 
EBJ1263:learn_git tao2.zhang$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

注:命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
  一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
  一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit或git add时的状态(使工作区文件与暂存区或仓库区保持一致)
场景二:

EBJ1263:learn_git tao2.zhang$ vim readme.txt 
EBJ1263:learn_git tao2.zhang$ git add readme.txt
EBJ1263:learn_git tao2.zhang$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   readme.txt

EBJ1263:learn_git tao2.zhang$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
EBJ1263:learn_git tao2.zhang$ git reset HEAD readme.txt 
Unstaged changes after reset:
M   readme.txt
EBJ1263:learn_git tao2.zhang$ git checkout -- readme.txt 
EBJ1263:learn_git tao2.zhang$ cat readme.txt 
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.

用命令git reset HEAD <file>可以把暂存区的修改撤销掉(unstage),重新放回工作区。
场景三:
场景三其实我们知道如何处理:即版本回退(现在,假设你不但改错了东西,还从暂存区提交到了版本库,怎么办呢?还记得[版本回退]吗?可以回退到上一个版本。不过,这是有条件的,就是你还没有把自己的本地版本库推送到远程。还记得Git是分布式版本控制系统吗?我们后面会讲到远程版本库,一旦你把stupid boss提交推送到远程版本库,你就真的惨了……
)。


文件删除

涉及2个命令:git rmgit checkout --file
两种情况:

EBJ1263:learn_git tao2.zhang$ vim test_del.txt 
EBJ1263:learn_git tao2.zhang$ git add test_del.txt 
EBJ1263:learn_git tao2.zhang$ git commit - m "del demo"
error: pathspec '-' did not match any file(s) known to git.
error: pathspec 'm' did not match any file(s) known to git.
error: pathspec 'del demo' did not match any file(s) known to git.
EBJ1263:learn_git tao2.zhang$ git commit -m "del demo"
[master cc24c06] del demo
 1 file changed, 1 insertion(+)
 create mode 100644 test_del.txt
EBJ1263:learn_git tao2.zhang$ rm test_del.txt 
EBJ1263:learn_git tao2.zhang$ git rm tetest_del.txt
fatal: pathspec 'tetest_del.txt' did not match any files
EBJ1263:learn_git tao2.zhang$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    deleted:    test_del.txt

no changes added to commit (use "git add" and/or "git commit -a")
EBJ1263:learn_git tao2.zhang$ git rm test_del.txt
rm 'test_del.txt'
EBJ1263:learn_git tao2.zhang$ git commit -m "remove test_del.txt from reposity"
[master 0c38d86] remove test_del.txt from reposity
 1 file changed, 1 deletion(-)
 delete mode 100644 test_del.txt

情况二:

EBJ1263:learn_git tao2.zhang$ vim test_recon.txt
EBJ1263:learn_git tao2.zhang$ git add test_recon.txt 
EBJ1263:learn_git tao2.zhang$ git commit -m "wrong remove demo"
[master 063b798] wrong remove demo
 1 file changed, 1 insertion(+)
 create mode 100644 test_recon.txt
EBJ1263:learn_git tao2.zhang$ rm test_recon.txt
EBJ1263:learn_git tao2.zhang$ ls
LICENSE     readme.txt
EBJ1263:learn_git tao2.zhang$ git checkout --test_recon.txt
EBJ1263:learn_git tao2.zhang$ ls
LICENSE     readme.txt  test_recon.txt


结束单机,远程仓库来也

上一篇 下一篇

猜你喜欢

热点阅读