Git-基本操作(同SVN)

本人拜读了廖雪峰老师关于Git的讲述后整理所得

1.创建版本库:

 版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。

找一个地方创建一个目录

$ mkdir GitStudy
$ cd GitStudy/

$ pwd
/d/GitStudy

通过git init命令把这个目录变成Git可以管理的仓库:

$ git init
Initialized empty Git repository in D:/GitStudy/.git/

Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository),细心的读者可以发现当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。

没有看到.git目录,那是因为这个目录默认是隐藏的,用ls -al命令就可以看见。

$ ls -al
total 8
drwxr-xr-x 1 chao03.li 1049089 0 七月 22 15:31 ./
drwxr-xr-x 1 chao03.li 1049089 0 七月 22 15:30 ../
drwxr-xr-x 1 chao03.li 1049089 0 七月 22 15:31 .git/

然后创建一个测试文件

$ touch readme.txt

$ vim readme.txt

$ cat readme.txt
I will study git in this txt

查看工作区中的文件状态【untracked files】未被标记的、未被跟踪的文件就是在工作区没有add到暂存区

$ git status
On branch master

Initial commit

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

readme.txt

nothing added to commit but untracked files present (use "git add" to track)

用命令git add告诉Git,把文件添加到仓库

$ git add readme.txt

$ git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: readme.txt

用命令git commit告诉Git,把文件提交到仓库

git commit命令,-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。

$ git commit -m "first time to commit readme.txt"
[master (root-commit) bb86898] first time to commit readme.txt
1 file changed, 1 insertion(+)
create mode 100644 readme.txt

初始化一个Git仓库,使用git init命令。

添加文件到Git仓库,分两步:

  • 第一步,使用命令git add <file>,注意,可反复多次使用,添加多个文件;

  • 第二步,使用命令git commit,完成。

2.版本状态及文件对比:

 修改readme.txt

$ vi readme.txt

$ cat readme.txt
I will study git in this txt
Git is not too hard

git status命令可以让我们时刻掌握仓库当前的状态,下面的命令告诉我们,readme.txt被修改过了,但还没有准备提交的修改。

$ 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告诉我们readme.txt被修改了,通过git diff命令看具体修改了什么内容

$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index e2905ad..14c34f7 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
I will study git in this txt
+Git is not too hard

将修改过后的文件提交到Git同样分为两步:add+commit

$ git add readme.txt

$ git commit -m "second time to add a row"
[master f609340] second time to add a row
1 file changed, 1 insertion(+)

再用git status命令看看仓库的当前状态,工作目录是干净(working directory clean)的。

$ git status
On branch master
nothing to commit, working tree clean
  • 要随时掌握工作区的状态,使用git status命令。

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

3.版本回退:

 在readme.txt文件中新添加一行

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder

然后将其提交,像这样,你不断对文件进行修改,然后不断提交修改到版本库里,就好比玩RPG游戏时,每通过一关就会自动把游戏状态存盘,如果某一关没过去,你还可以选择读取前一关的状态。有些时候,在打Boss之前,你会手动存盘,以便万一打Boss失败了,可以从最近的地方重新开始。Git也是一样,每当你觉得文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit。一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失。

现在,我们回顾一下readme.txt文件一共有几个版本被提交到Git仓库里了:git log readme.txt

$ git log readme.txt
commit 31bd1cc7f2ddd14dadd02a314009a4c50924e9a0 (HEAD -> master)
Author: chao03.li <chao03.li@phicomm.com>
Date: Sat Jul 22 16:24:29 2017 +0800

add row god is coder

commit f609340795ca1b078a473368d3b815b88a2fc0be
Author: chao03.li <chao03.li@phicomm.com>
Date: Sat Jul 22 16:05:07 2017 +0800

second time to add a row

commit bb86898e9b497f7dea9fa653e0c66d4b8ee7ffa2
Author: chao03.li <chao03.li@phicomm.com>
Date: Sat Jul 22 15:47:29 2017 +0800

first time to commit readme.txt

git log命令显示从最近到最远的提交日志,我们可以看到3次提交

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

$ git log --pretty=oneline
31bd1cc7f2ddd14dadd02a314009a4c50924e9a0 (HEAD -> master) add row god is coder
f609340795ca1b078a473368d3b815b88a2fc0be second time to add a row
bb86898e9b497f7dea9fa653e0c66d4b8ee7ffa2 first time to commit readme.txt

你看到的一大串类似3628164...882e1e0的是commit id(版本号),和SVN不一样,Git的commit id不是1,2,3……递增的数字,而是一个SHA1计算出来的一个非常大的数字,用十六进制表示,而且你看到的commit id和我的肯定不一样,以你自己的为准。为什么commit id需要用这么一大串数字表示呢?因为Git是分布式的版本控制系统,后面我们还要研究多人在同一个版本库里工作,如果大家都用1,2,3……作为版本号,那肯定就冲突了。

把readme.txt回退到上一个版本:

Git用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100

$ git reset --hard HEAD^
HEAD is now at f609340 second time to add a row

$ cat readme.txt
I will study git in this txt
Git is not too hard

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

git-head

改为指向add distributed

git-head-move

然后顺便把工作区的文件更新了。所以你让HEAD指向哪个版本号,你就把当前版本定位在哪。

我们用git log再看看现在版本库的状态你会发现最新的那个版本God is a coder已经看不到了!

$ git log
commit f609340795ca1b078a473368d3b815b88a2fc0be (HEAD -> master)
Author: chao03.li <chao03.li@phicomm.com>
Date: Sat Jul 22 16:05:07 2017 +0800

second time to add a row

commit bb86898e9b497f7dea9fa653e0c66d4b8ee7ffa2
Author: chao03.li <chao03.li@phicomm.com>
Date: Sat Jul 22 15:47:29 2017 +0800

first time to commit readme.txt

如果想回去之前的那个版本,使用git reflog 找到之前提交的那个commit id是31bd1cc...,于是就可以指定回到未来的某个版本:

$ git reflog
f609340 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
31bd1cc HEAD@{1}: commit: add row god is coder
f609340 (HEAD -> master) HEAD@{2}: commit: second time to add a row
bb86898 HEAD@{3}: commit (initial): first time to commit readme.txt

$ git reset --hard 31bd1cc
HEAD is now at 31bd1cc add row god is coder

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
  • HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id

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

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

4.工作区和暂存区:

 Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念。

工作区(Working Directory)

就是你在电脑里能看到的目录,比如我的GitStudy文件夹就是一个工作区:

版本库(Repository)

工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。

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


git-repo

前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的:

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。

你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

示例修改readme.txt新增LICENSE:

$ vi readme.txt

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man

$ touch LICENSE

$ cat LICENSE
sad

查看状态

$ 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.txtLICENSE都添加后,用git status再查看一下:

$ git add readme.txt

$ git add LICENSE

再一次查看文件状态:

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

new file: LICENSE
modified: readme.txt

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

git-stage

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

$ git commit -m "test stage"
[master afcd497] test stage
2 files changed, 2 insertions(+)
create mode 100644 LICENSE

一旦提交后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的:

$ git status
On branch master
nothing to commit, working tree clean

现在版本库变成了这样,暂存区就没有任何内容了:

git-stage-after-commit

5.管理修改:

 修改readme.txt文件,add到暂存区,然后在进行修改,然后在进行commit操作,只将第一次修改的内容进行了提交。

$ git add readme.txt

$ vi readme.txt

$ git commit -m "two update one add one commit"
[master 155d578] two update one add one commit
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 -- readme.txt
diff --git a/readme.txt b/readme.txt
index a0f2585..e240599 100644
--- a/readme.txt
+++ b/readme.txt
@@ -3,3 +3,4 @@ Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man
2017-07-25
+18:51 writen

Git是如何跟踪修改的?每次修改,如果不add到暂存区,那就不会加入到commit中。

6.撤销修改:

场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file

场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。

 修改完一个文件--》发现不对劲--》撤销修改

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man
2017-07-25
18:51 writen
We will fuck!

$ 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 checkout -- readme.txt

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man
2017-07-25
18:51 writen

$ git status
On branch master
nothing to commit, working tree clean

 修改完一个文件--》add到暂存区--》发现有问题--》撤销更改(checkout不能修复这种情况)

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man
2017-07-25
18:51 writen
Fight Now

$ git add readme.txt

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

modified: readme.txt

修改完一个文件--》add到暂存区--》--》继续对文件进行修改--》发现有问题--》撤销更改

$ vi readme.txt

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man
2017-07-25
18:51 writen
Fight Now
I will buy macbook
it cost $990.00

$ git checkout -- readme.txt

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man
2017-07-25
18:51 writen
Fight Now
I will buy macbook

命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:

一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

总之,就是让这个文件回到最近一次git commitgit add时的状态。

 修改完一个文件--》add到暂存区--》发现有问题--》撤销更改(reset可以修复这种情况)

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

modified: readme.txt

$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt

$ 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

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man
2017-07-25
18:51 writen
Fight Now
I will buy macbook

还记得如何丢弃工作区的修改吗?

$ git checkout -- readme.txt

$ cat readme.txt
I will study git in this txt
Git is not too hard
God is a coder
LiaoXueFeng is a subalasi man
2017-07-25
18:51 writen
Fight Now

场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- file

场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD file,就回到了场景1,第二步按场景1操作。

7.删除修改:

 一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:

这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:

--》一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit

--》另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:

先说第二种:

$ vi test.txt

$ git add test.txt

$ git commit -m "test rm"
[master 21aa1a8] test rm
1 file changed, 1 insertion(+)
create mode 100644 test.txt

$ git status
On branch master
nothing to commit, working tree clean

$ rm -rf test.txt

$ 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.txt

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

$ ls
LICENSE readme.txt

$ git checkout -- test.txt

$ ls
LICENSE readme.txt test.txt

$ git status
On branch master
nothing to commit, working tree clean

第一种情况:

$ rm -rf test.txt

$ 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.txt

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

$ git rm test.txt
rm 'test.txt'

$ git commit -m "remove test.txt"
[master 929d205] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt

命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容

原文地址:https://www.cnblogs.com/lingyejun/p/7221461.html