关山难越,谁悲失路之人;萍水相逢,尽是他乡之客。
百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程教程 > 技术文章 > 正文

干货分享程序猿必备工具Git之创建版本库及版本库操作(详细教程)

guanshanw 2023-10-07 11:49 16 浏览 0 评论


如果手机上显示代码错乱,请分享到QQ或者其他地方,用电脑查看!!!


git能干的东西有很多,这里不再过多叙述,直接重点干货。

什么是版本库呢?版本库又名仓库,英文名repository,其实就是一个目录,可以进行增删查改

创建一个目录,这里在根目录下创建一个git_home目录

mkdir /git_home

cd git_home

git init

这样就创建好了一个仓库,当然目前是一个空仓库

这个时候在当前目录通过ls -a可以看到多了一个.git的目录

把文件添加到版本库

版本控制系统可以告诉你每次的改动,比如在第5行加了一个单词“Linux”,在第8行删了一个单词“Windows”。而图片、视频这些二进制文件,虽然也能由版本控制系统管理,但没法跟踪文件的变化,只能把二进制文件每次改动串起来,也就是只知道图片从100KB改成了120KB,但到底改了啥,版本控制系统不知道,也没法知道。

我们在git_home目录下创建一个文件,并填写如下内容

git is a version control system

git is fee software

把文件放到git需要两步:

1. git add 文件名

2. git commit -m "说明"

下面我们把readme.txt放到git,操作如下:

[root@centos-linux git_home]# git add readme.txt

[root@centos-linux git_home]# git commit -m "wrote a readme file"

[master (root-commit) 8a044aa] wrote a readme file

Committer: root <root@centos-linux.shared>

1 file changed, 2 insertions(+)

create mode 100644 readme.txt

[root@centos-linux git_home]#

第一步执行git add成功后是没有任何提示的

第二步git commit命令中 -m 后面输入的是本次提交的说明,一般输入的对当前提交记录的一个简单说明,这样在历史记录里查看的时候,就可以看到这个说明,从而知道每次提交的意义

并且这里需要知道git commit可以一次提交多个文件,也就是说你可以add 多次,但是只需要一次commit.

[root@centos-linux git_home]# touch file1.txt file2.txt file3.txt

[root@centos-linux git_home]# ls

file1.txt file2.txt file3.txt readme.txt

[root@centos-linux git_home]# git add file1.txt

[root@centos-linux git_home]# git add file2.txt file3.txt

[root@centos-linux git_home]# git status

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

new file: file1.txt

new file: file2.txt

new file: file3.txt

[root@centos-linux git_home]# git commit -m "add 3 files"

[master 4d0b5e2] add 3 files

3 files changed, 0 insertions(+), 0 deletions(-)

create mode 100644 file1.txt

create mode 100644 file2.txt

create mode 100644 file3.txt

[root@centos-linux git_home]#

总结

上面一共有学了三个命令

初始化一个git仓库:git init

添加文件到git仓库:

1. git add 文件名

2. git commit -m "说明"




之前我们提交了一个readme.txt文件,现在我们对文件进行修改

1

2

3

4

[root@centos-linux git_home]# cat readme.txt

git is a distributed version control system

git is fee software

[root@centos-linux git_home]#

我们通过git status命令查看结果

1

2

3

4

5

6

7

8

[root@centos-linux git_home]# 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")

[root@centos-linux git_home]#

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

通过git status虽然可以看到修改了,但是具体修改了哪些内容看不到,这个时候可以用git diff

1

2

3

4

5

6

7

8

9

10

[root@centos-linux git_home]# git diff readme.txt

diff --git a/readme.txt b/readme.txt

index 9489ca3..d5a58b6 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 fee software

[root@centos-linux git_home]#

这样就可以查看修改之后和修改之前的区别

然后执行添加,并查看状态

1

2

3

4

5

6

7

[root@centos-linux git_home]# git add readme.txt

[root@centos-linux git_home]# git status

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

modified: readme.txt

[root@centos-linux git_home]#

最后执行commit

1

2

3

4

[root@centos-linux git_home]# git commit -m "add distributed"

[master 71f419d] add distributed

1 file changed, 1 insertion(+), 1 deletion(-)

[root@centos-linux git_home]#

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

1

2

3

4

[root@centos-linux git_home]# git status

On branch master

nothing to commit, working tree clean

[root@centos-linux git_home]#

Git告诉我们当前没有需要提交的修改,而且,工作目录是干净(working directory clean)的。

小结

这里我们需要掌握下面的命令

查看当前git的状态:git status

查看当前文件与git文件的区别:git diff 文件名

一、版本回退

我们对readme.txt进行修改,并通过git log查看自己的版本日志,操作如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control system

git is fee software distributed under the GPL

[root@centos-linux git_home]# git add readme.txt

[root@centos-linux git_home]# git commit -m "append GPL"

[master 75243cc] append GPL 1 file changed, 1 insertion(+), 1 deletion(-)

[root@centos-linux git_home]# git log

commit 75243cc4ee64ce2a7969a6107e31b08a75c59bba (HEAD -> master)

Author: zhaofan <hjzhaofan@163.com>

Date: Wed May 24 13:27:57 2017 +0800

append GPL

commit 71f419d333510e958a10f03b410fd8c1fd34cd39

Author: zhaofan <hjzhaofan@163.com>

Date: Tue May 23 23:39:55 2017 +0800

add distributed

commit 4d0b5e293bcd0e04a2bddafbcaefcbe46b6e6d68

Author: zhaofan <hjzhaofan@163.com>

Date: Tue May 23 23:36:07 2017 +0800

add 3 files

commit 153363d767fc012bc263fdecd2aa7516dcedf912

Author: zhaofan <hjzhaofan@163.com>

Date: Tue May 23 23:35:15 2017 +0800

remove file1.2.3.txt

commit 83755f68346e41233beeb9fb7248801fa548fbf0

Author: root <root@centos-linux.shared>

Date: Tue May 23 23:31:02 2017 +0800

add distributed

commit cbe82f14eba075be3b6697becde706bf8cc35901

Author: root <root@centos-linux.shared>

Date: Tue May 23 23:01:58 2017 +0800

add 3 files

commit 8a044aa3afc6bcf39c3fe1002edddc29a08a1a74

Author: root <root@centos-linux.shared>

Date: Tue May 23 22:56:12 2017 +0800

wrote a readme file

[root@centos-linux git_home]#

git log命令显示从最近到最远的提交日志。如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:

1

2

3

4

5

6

7

8

9

[root@centos-linux git_home]# git log --pretty=oneline

75243cc4ee64ce2a7969a6107e31b08a75c59bba (HEAD -> master) append GPL

71f419d333510e958a10f03b410fd8c1fd34cd39 add distributed

4d0b5e293bcd0e04a2bddafbcaefcbe46b6e6d68 add 3 files

153363d767fc012bc263fdecd2aa7516dcedf912 remove file1.2.3.txt

83755f68346e41233beeb9fb7248801fa548fbf0 add distributed

cbe82f14eba075be3b6697becde706bf8cc35901 add 3 files

8a044aa3afc6bcf39c3fe1002edddc29a08a1a74 wrote a readme file

[root@centos-linux git_home]#

如果把readme.txt回退到上一个版本“add distributed”的那个版本,怎么做?

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

1

2

3

4

5

6

7

[root@centos-linux git_home]# git reset --hard HEAD^

HEAD is now at 71f419d add distributed

[root@centos-linux git_home]# ls

file1.txt file2.txt file3.txt readme.txt

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control systemgit is fee software

[root@centos-linux git_home]#

当然这个时候其实也是可以重新会带上一次的,前提是你当前的窗口没有关,你可以找到上面的版本号

1

2

3

4

5

6

7

8

9

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control system

git is fee software

[root@centos-linux git_home]# git reset --hard 75243cc4ee64ce2a79

HEAD is now at 75243cc append GPL

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control system

git is fee software distributed under the GPL

[root@centos-linux git_home]#

版本号没必要写全,前几位就可以了,Git会自动去找。当然也不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个了。

同时git又一个git reflog记录所有git的命令

1

2

3

4

5

6

7

8

9

10

11

12

[root@centos-linux git_home]# git reflog

75243cc (HEAD -> master) HEAD@{0}: reset: moving to 75243cc4ee64ce2a79

71f419d HEAD@{1}: reset: moving to 71f419d333

71f419d HEAD@{2}: reset: moving to HEAD^

75243cc (HEAD -> master) HEAD@{3}: commit: append GPL

71f419d HEAD@{4}: commit: add distributed

4d0b5e2 HEAD@{5}: commit: add 3 files

153363d HEAD@{6}: commit: remove file1.2.3.txt

83755f6 HEAD@{7}: commit: add distributed

cbe82f1 HEAD@{8}: commit: add 3 files

8a044aa HEAD@{9}: commit (initial): wrote a readme file

[root@centos-linux git_home]#

小结

这里我们需要掌握下面的命令

查看git的提交历史:git log

git log --pretty=oneline

回退到某个版本:git reset --hard HEAD^ 上一个版本

git reset --hard HEAD^ 上上一个版本

git reset --hard HEAD~10 上10个版本

查看所有git 命令日志:git reflog

二、工作区和暂缓区

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

工作区

最开始我创建的git_home就是一个工作区

版本库

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

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

分支和HEAD的概念我们以后再讲。

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

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

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

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

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

我们通过下面的一个操作理解

在工作区增加一个LICENSE文本文件,并对readme.txt文件修改

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[root@centos-linux git_home]# touch license

[root@centos-linux git_home]# ls

file1.txt file2.txt file3.txt license readme.txt

[root@centos-linux git_home]# 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")

[root@centos-linux git_home]#

从git status的结果我们可以看出readme被修改了,以及添加了license文件

执行git add 并再次查看状态

1

2

3

4

5

6

7

8

[root@centos-linux git_home]# git add readme.txt license

[root@centos-linux git_home]# git status

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

new file: license

modified: readme.txt

[root@centos-linux git_home]#

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

1

2

3

4

5

6

7

[root@centos-linux git_home]# git commit -m "understand how stage works"

[master f93fb9f] understand how stage works

2 files changed, 1 insertion(+) create mode 100644 license

[root@centos-linux git_home]# git status

On branch master

nothing to commit, working tree clean

[root@centos-linux git_home]#

此时,暂缓存区就没有任何内容了

三、管理修改

git跟踪并管理的是修改而非文件

通过下面的例子来理解这句话的意思

我们将readme.txt添加一行后如下:

1

2

3

4

5

6

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control system

git is fee software distributed under the GPL

git has a mutable index called stage

git tracks changes

[root@centos-linux git_home]#

这个时候通过git status查看:

1

2

3

4

5

6

7

[root@centos-linux git_home]# 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.txtno changes added to commit (use "git add" and/or "git commit -a")

[root@centos-linux git_home]#

我们进行一次add,并再次通过git status查看:

1

2

3

4

5

6

7

[root@centos-linux git_home]# git add readme.txt

[root@centos-linux git_home]# git status

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

modified: readme.txt

[root@centos-linux git_home]#

然后对文件再次修改:

1

2

3

4

5

6

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control system

git is fee software distributed under the GPL

git has a mutable index called stage

git tracks changesmy name is zhaofan

[root@centos-linux git_home]#

这个时候我们执行git commit -m 'git tracks changes',并查看状态:

1

2

3

4

5

6

7

8

9

10

[root@centos-linux git_home]# git commit -m 'git tracks changes'

[master e0e3ab7] git tracks changes 1 file changed, 1 insertion(+)

[root@centos-linux git_home]# 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")

[root@centos-linux git_home]#

这里我们可以看到其实这个时候我们第二次添加的my name is zhaofan其实并没有被提交

分析:我们重新来看这句话,git管理的是修改,我们使用git add命令之后其实是把工作区第一次修改后的内容放入暂缓区,准备提交,但是在工作区的第二次修改并没有执行git add添加到暂缓区,所以当我们使用git commit命令的时候只是把第一次修改的提交了,第二次修改的还在工作区,并不会被提交

我们通过git diff可以查看工作区和版本库里的区别:

1

2

3

4

5

6

7

8

9

10

11

[root@centos-linux git_home]# git diff HEAD -- readme.txt

diff --git a/readme.txt b/readme.txt

index 2853fee..cc9f379 100644

--- a/readme.txt

+++ b/readme.txt

@@ -2,3 +2,4 @@ git is a aaaa distributed version control system

git is fee software distributed under the GPL

git has a mutable index called stage

git tracks changes

+my name is zhaofan

[root@centos-linux git_home]#

四、撤销修改

其实这里的撤销修改主要包括两种情况:一种是当你在工作区修改文件了,并没有执行git add,想恢复回去。第二种是当你在工作区修改文件并执行了git add,但是没有执行git commit。

情况一:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control system

git is fee software distributed under the GPL

git has a mutable index called stage

git tracks changes

my name is zhaofanthis is a error

[root@centos-linux git_home]# 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")

[root@centos-linux git_home]#

当在readme.txt文件最后添加this is a error之后,但是并没有add添加到缓冲区,这个时候如果想要恢复到添加之前的状态,git checkout -- file可以丢弃工作区的修改

命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销

1

2

3

4

5

6

7

8

9

10

[root@centos-linux git_home]# git checkout -- readme.txt

[root@centos-linux git_home]# git statusOn branch master

nothing to commit, working tree clean

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control system

git is fee software distributed under the GPL

git has a mutable index called stage

git tracks changes

my name is zhaofan

[root@centos-linux git_home]#

情况二:

1

2

3

4

5

6

7

8

9

10

11

12

13

[root@centos-linux git_home]# git add readme.txt

[root@centos-linux git_home]# git status

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

modified: readme.txt

[root@centos-linux git_home]# cat readme.txt

git is a aaaa distributed version control system

git is fee software distributed under the GPL

git has a mutable index called stage

git tracks changes

my name is zhaofanthis is a error

[root@centos-linux git_home]#

修改了文件并执行了git add,但是没有git commit ,这个时候如果想要恢复到之前的状态

git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区,然后再次执行git checkout

五、删除文件

在git中,删除也是一种修改操作

我们在工作区中创建一个文件,并git add添加到缓冲区,然后通过git commit提交到版本库

这个时候我们把工作区的文件给删除,因此工作区或版本库不一致了

1

2

3

4

5

6

7

8

[root@centos-linux git_home]# rm -f test.txt

[root@centos-linux git_home]# 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.txtno changes added to commit (use "git add" and/or "git commit -a")

[root@centos-linux git_home]#

这个时候分两种情况,情况一:版本库的文件也要删除。 情况二:删除错,版本库中还有,可以恢复

如果是情况一:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

[root@centos-linux git_home]# git rm test.txt

rm 'test.txt'

[root@centos-linux git_home]# git status

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

deleted: test.txt

[root@centos-linux git_home]# git commit -m 'remove test.txt'

[master c207d00] remove test.txt

1 file changed, 0 insertions(+), 0

deletions(-) delete mode 100644 test.txt

[root@centos-linux git_home]# git status

On branch master

nothing to commit, working tree clean

[root@centos-linux git_home]#

情况二:

这个时候是删除错了,可以通过git checkout -- test.txt,把误删除的文件恢复到最新状态

git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

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


以上是全部代码,只是善于分享,不足之处请包涵!爬虫基本的原理就是,获取源码,进而获取网页内容。一般来说,只要你给一个入口,通过分析,可以找到无限个其他相关的你需要的资源,进而进行爬取。


我也写了很多其他的非常简单的入门级的爬虫详细教程,关注后,点击我的头像,就可以查看到。


欢迎大家一起留言讨论和交流,谢谢!

相关推荐

七条简单命令让您玩转Git
七条简单命令让您玩转Git

凭借着出色的协作能力、快速部署效果与代码构建辅助作用,Git已经得到越来越多企业用户的青睐。除了用于开发商业及消费级应用之外,众多科学及政府机构也开始尝试使用这...

2023-10-07 12:14 guanshanw

基本完整的关于Git分支branch的操作
基本完整的关于Git分支branch的操作

Git使用背景项目中要用到dev或者其他分支开发完代码,需要将该分支合并到master的需求操作步骤下面以dev名称为lex为分支名为例来操作一遍客户端操作:...

2023-10-07 12:14 guanshanw

Git 进阶(合并与变基)
Git 进阶(合并与变基)

在Git中整合来自不同分支的修改主要有两种方法:合并(merge)以及变基(rebase)合并(merge)merge流程图merge的原理是找到这两个分...

2023-10-07 12:13 guanshanw

Git学习笔记 003 Git进阶功能 part5 合并(第一部分)

合并(merge)是很常用的操作。尤其是一个庞大的很多人参与开发的企业级应用。一般会设定一个主分支,和多个副分支。在副分支开发完成后,合并到主分支中。始终保持主分支是一个完整的,稳定的最新状态的分支。...

非标题党,三张图帮你理解git merge和git rebase的区别
非标题党,三张图帮你理解git merge和git rebase的区别

初始场景:基于正常的开发分支修改几个小bug,然后在合并到开发分支上。gitmergegitcheckoutfeaturegitmergeho...

2023-10-07 12:13 guanshanw

git 初次使用(01)
git 初次使用(01)

先从github上克隆代码下来:使用vscode克隆代码如下图,填写上github仓库地址:vscode有时候克隆代码速度比较慢,可以用命令行方式克隆gitc...

2023-10-07 12:12 guanshanw

Git 远程操作

4.Git远程操作命令说明gitremote远程版本库操作gitfetch从远程获取版本库gitpull下载远程代码并合并gitpush上传远程代码并合并4.1远程版本库操作gitre...

Git常用命令-总结
Git常用命令-总结

创建git用户$gitconfig--globaluser.name"YourName"$gitconfig--globaluser.em...

2023-10-07 12:12 guanshanw

git中删除从别人clone下来项目的git信息,并修改为自己的分支

如果你从别人的Git存储库中克隆了一个项目,并想要删除与该存储库相关的Git信息,并将其修改为你自己的分支,则可以执行以下步骤:使用gitclone命令克隆存储库:gitclone<u...

git系列-回滚和放弃本地修改

回滚历史提交就是reset的功能。这种情况是已经提交远程仓库,需要回滚到之前的提交。gitreset--hardcommitId//注:强制提交后,当前版本后面的提交版本将会删掉!gi...

GIT使用小技巧大全
GIT使用小技巧大全

在大型软件工程的开发过程中,版本控制是无法绕过去的;目前来说,最火的版本控制软件就是GIT了。早两年SVN比较火,不过被大神linus喷了几次后,就日落西山了,...

2023-10-07 12:11 guanshanw

git相关命令-上
git相关命令-上

这些命令都是看了文档后,个人觉得比较有用的一些,展示给大家。回到远程仓库的状态抛弃本地所有的修改,回到远程仓库的状态。gitfetch--all&...

2023-10-07 12:10 guanshanw

Git命令行接口:掌握Git的必备技能
Git命令行接口:掌握Git的必备技能

Git是一款强大的分布式版本控制工具,它支持命令行界面操作。熟练掌握Git命令行接口,是开发者使用Git的必备技能之一。在这篇文章中,我们将介绍Git命令行接口...

2023-10-07 12:10 guanshanw

Git命令详解
Git命令详解

相信各位小伙伴们应该都对git有一些了解,毕竟作为代码管理的神器,就算不是IT行业的小伙伴肯定也或多或少的听说过一些。今天就来和小伙伴们分享一下自己总结的常用命...

2023-10-07 12:10 guanshanw

工作7年收集到的git命令
工作7年收集到的git命令

概念git中的术语解释:仓库也叫版本库(repository)stage:暂存区,add后会存到暂存区,commit后提交到版本库git安装linux...

2023-10-07 12:10 guanshanw

取消回复欢迎 发表评论: