admin 2025-09-29
114
如果手机上显示代码错乱,请分享到QQ或者其他地方,用电脑查看!!!
git能干的东西有很多,这里不再过多叙述,直接重点干货。
什么是版本库呢?版本库又名仓库,英文名repository,其实就是一个目录,可以进行增删查改
创建一个目录,这里在根目录下创建一个git_home目录
mkdir/git_home
cdgit_home
gitinit
这样就创建好了一个仓库,当然目前是一个空仓库
这个时候在当前目录通过ls-a可以看到多了一个.git的目录
把文件添加到版本库
我们在git_home目录下创建一个文件,并填写如下内容
gitisaversioncontrolsystem
gitisfeesoftware
把文件放到git需要两步:
1.gitadd文件名
2.gitcommit-m"说明"
下面我们把放到git,操作如下:
[root@centos-linuxgit_home]gitcommit-m"wroteareadmefile"
[master(root-commit)8a044aa]wroteareadmefile
Committer:rootroot@
1filechanged,2insertions(+)
[root@centos-linuxgit_home]
[root@centos-linuxgit_home]
[root@centos-linuxgit_home]gitstatus
Onbranchmaster
Changestobecommitted:
(use"gitresetHEADfile"tounstage)
newfile:
newfile:
newfile:
[root@centos-linuxgit_home]
总结
上面一共有学了三个命令
初始化一个git仓库:gitinit
添加文件到git仓库:
1.gitadd文件名
2.gitcommit-m"说明"
之前我们提交了一个文件,现在我们对文件进行修改
1
2
3
4
[root@centos-linuxgit_home]
我们通过gitstatus命令查看结果
1
2
3
4
5
6
7
8
[root@centos-linuxgit_home]
gitstatus命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,被修改过了,但还没有准备提交的修改。
通过gitstatus虽然可以看到修改了,但是具体修改了哪些内容看不到,这个时候可以用gitdiff
1
2
3
4
5
6
7
8
9
10
[root@centos-linuxgit_home]
这样就可以查看修改之后和修改之前的区别
然后执行添加,并查看状态
1
2
3
4
5
6
7
[root@centos-linuxgit_home]gitstatus
Onbranchmaster
Changestobecommitted:
(use"gitresetHEADfile"tounstage)
modified:
[root@centos-linuxgit_home]gitcommit-m"adddistributed"
[master71f419d]adddistributed
1filechanged,1insertion(+),1deletion(-)
[root@centos-linuxgit_home]gitstatus
Onbranchmaster
nothingtocommit,workingtreeclean
[root@centos-linuxgit_home]
gitisaaaaadistributedversioncontrolsystem
gitisfeesoftwaredistributedundertheGPL
[root@centos-linuxgit_home]gitcommit-m"appGPL"
[master75243cc1filechanged,1insertion(+),1deletion(-)
[root@centos-linuxgit_home]
gitlog命令显示从最近到最远的提交日志。如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:
1
2
3
4
5
6
7
8
9
[root@centos-linuxgit_home]
如果把回退到上一个版本“adddistributed”的那个版本,怎么做?
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交7524359bba,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。
1
2
3
4
5
6
7
[root@centos-linuxgit_home]ls
[root@centos-linuxgit_home]
当然这个时候其实也是可以重新会带上一次的,前提是你当前的窗口没有关,你可以找到上面的版本号
1
2
3
4
5
6
7
8
9
[root@centos-linuxgit_home]gitreset--hard75243cc4ee64ce2a79
HEADisnowat75243ccappGPL
[root@centos-linuxgit_home]
版本号没必要写全,前几位就可以了,Git会自动去找。当然也不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个了。
同时git又一个gitreflog记录所有git的命令
1
2
3
4
5
6
7
8
9
10
11
12
[root@centos-linuxgit_home]
小结
这里我们需要掌握下面的命令
查看git的提交历史:gitlog
gitlog--pretty=oneline
回退到某个版本:gitreset--hardHEAD^上一个版本
gitreset--hardHEAD^上上一个版本
gitreset--hardHEAD~10上10个版本
查看所有git命令日志:gitreflog
二、工作区和暂缓区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念。
工作区
最开始我创建的git_home就是一个工作区
版本库
工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master,以及指向master的一个指针叫HEAD。
分支和HEAD的概念我们以后再讲。
前面讲了我们把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用gitadd把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用gitcommit提交更改,实际上就是把暂存区的所有内容提交到当前分支。
因为我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,gitcommit就是往master分支上提交更改。
你可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。
我们通过下面的一个操作理解
在工作区增加一个LICENSE文本文件,并对文件修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@centos-linuxgit_home]ls
[root@centos-linuxgit_home]
从gitstatus的结果我们可以看出readme被修改了,以及添加了license文件
执行gitadd并再次查看状态
1
2
3
4
5
6
7
8
[root@centos-linuxgit_home]gitstatus
Onbranchmaster
Changestobecommitted:
(use"gitresetHEADfile"tounstage)
newfile:license
modified:
[root@centos-linuxgit_home]gitcommit-m"understandhowstageworks"
[masterf93fb9f]understandhowstageworks
2fileschanged,1insertion(+)createmode100644license
[root@centos-linuxgit_home]
此时,暂缓存区就没有任何内容了
三、管理修改
git跟踪并管理的是修改而非文件
通过下面的例子来理解这句话的意思
我们将添加一行后如下:
1
2
3
4
5
6
[root@centos-linuxgit_home]
这个时候通过gitstatus查看:
1
2
3
4
5
6
7
[root@centos-linuxgit_home]
我们进行一次add,并再次通过gitstatus查看:
1
2
3
4
5
6
7
[root@centos-linuxgit_home]gitstatus
Onbranchmaster
Changestobecommitted:
(use"gitresetHEADfile"tounstage)
modified:
[root@centos-linuxgit_home]
gitisaaaaadistributedversioncontrolsystem
gitisfeesoftwaredistributedundertheGPL
githasamutableindexcalledstage
gittrackschangesmynameiszhaofan
[root@centos-linuxgit_home]gitcommit-m'gittrackschanges'
[mastere0e3ab7]gittrackschanges1filechanged,1insertion(+)
[root@centos-linuxgit_home]
这里我们可以看到其实这个时候我们第二次添加的mynameiszhaofan其实并没有被提交
分析:我们重新来看这句话,git管理的是修改,我们使用gitadd命令之后其实是把工作区第一次修改后的内容放入暂缓区,准备提交,但是在工作区的第二次修改并没有执行gitadd添加到暂缓区,所以当我们使用gitcommit命令的时候只是把第一次修改的提交了,第二次修改的还在工作区,并不会被提交
我们通过gitdiff可以查看工作区和版本库里的区别:
1
2
3
4
5
6
7
8
9
10
11
[root@centos-linuxgit_home]
四、撤销修改
其实这里的撤销修改主要包括两种情况:一种是当你在工作区修改文件了,并没有执行gitadd,想恢复回去。第二种是当你在工作区修改文件并执行了gitadd,但是没有执行gitcommit。
情况一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@centos-linuxgit_home]gitstatus
Onbranchmaster
Changesnotstagedforcommit:
(use"gitaddfile"toupdatewhatwillbecommitted)
(use"gitcheckout--file"todiscardchangesinworkingdirectory)
modified:
nochangesaddedtocommit(use"gitadd"and/or"gitcommit-a")
[root@centos-linuxgit_home]
[root@centos-linuxgit_home]
gitisaaaaadistributedversioncontrolsystem
gitisfeesoftwaredistributedundertheGPL
githasamutableindexcalledstage
gittrackschanges
mynameiszhaofan
[root@centos-linuxgit_home]
[root@centos-linuxgit_home]
gitisaaaaadistributedversioncontrolsystem
gitisfeesoftwaredistributedundertheGPL
githasamutableindexcalledstage
gittrackschanges
mynameiszhaofanthisisaerror
[root@centos-linuxgit_home]
[root@centos-linuxgit_home]
这个时候分两种情况,情况一:版本库的文件也要删除。情况二:删除错,版本库中还有,可以恢复
如果是情况一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@centos-linuxgit_home]gitstatus
Onbranchmaster
Changestobecommitted:
(use"gitresetHEADfile"tounstage)
deleted:test.txt
[root@centos-linuxgit_home]gitstatus
Onbranchmaster
nothingtocommit,workingtreeclean
[root@centos-linuxgit_home]#
情况二:
这个时候是删除错了,可以通过,把误删除的文件恢复到最新状态
gitcheckout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。
命令gitrm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。
以上是全部代码,只是善于分享,不足之处请包涵!爬虫基本的原理就是,获取源码,进而获取网页内容。一般来说,只要你给一个入口,通过分析,可以找到无限个其他相关的你需要的资源,进而进行爬取。
欢迎大家一起留言讨论和交流,谢谢!