博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Git学习笔记(一)
阅读量:6913 次
发布时间:2019-06-27

本文共 7859 字,大约阅读时间需要 26 分钟。

1、Git的介绍及安装

Git:全宇宙最牛的分布式版本控制软件,Git是目前世界上最先进的分布式版本控制系统

1
2
3
4
5
6
7
8
9
10
#CentOS7下git的安装
[root@ip-172-31-22-8 ~]
# yum -y install git
#设置git账号信息
[root@ip-172-31-22-8 ~]
# git config --global user.name "molewan"
[root@ip-172-31-22-8 ~]
# git config --global user.email "314324506@qq.com"
a)因为Git是分布式版本控制系统,所以,每个机器都必须报家:你的名字和Email地址。你也许
会担,如果有故意冒充别怎么办?这个不必担,先我们相信家都是善良
知的群众,其次,真的有冒充的也是有办法可查的。
b)注意git config命令的--global参数,了这个参数,表你这台机器上所有的Git仓库都会使
这个配置,当然也可以对某个仓库指定不同的户名和Email地址。

2、查看git的配置:

1
2
3
[root@ip-172-31-22-8 ~]
# git config --list
user.name=molewan
user.email=314324506@qq.com

3、为git配置颜色

1
2
3
4
5
[root@ip-172-31-22-8 ~]
# git config --global color.ui true
[root@ip-172-31-22-8 ~]
# git config --list
user.name=molewan
user.email=314324506@qq.com
color.ui=
true

4、创建一个本地的git库

1
2
3
4
5
6
7
8
9
10
11
12
[root@ip-172-31-22-8 ~]
# mkdir molewan
[root@ip-172-31-22-8 ~]
# cd molewan
[root@ip-172-31-22-8 molewan]
# ll
total 0
[root@ip-172-31-22-8 molewan]
# git init
Initialized empty Git repository 
in 
/root/molewan/
.git/
说明:仓库初始化一个git仓库,使用git init命令,将这个目录变成git可以管理的
[root@ip-172-31-22-8 molewan]
# ls -la
total 8
drwxr-xr-x.  3 root root   17 Nov 26 17:04 .
dr-xr-x---. 12 root root 4096 Nov 26 17:04 ..
drwxr-xr-x.  7 root root 4096 Nov 26 17:04 .git

5、将文件添加到版本库:

将一个文件放到Git仓库只需要两步:

a)git add告诉git,将文本添加到仓库

b)用git commit告诉git,把文件提交到仓库

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@ip-172-31-22-8 molewan]
# vim readme.txt
You have new mail 
in 
/var/spool/mail/root
[root@ip-172-31-22-8 molewan]
# cat readme.txt 
hehe
[root@ip-172-31-22-8 molewan]
# 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)
添加一个新文件,readme.txt
[root@ip-172-31-22-8 molewan]
# git add readme.txt 
[root@ip-172-31-22-8 molewan]
# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#new file:   readme.txt
#
[root@ip-172-31-22-8 molewan]
# git commit -m "the first commity"
[master (root-commit) 24a5b30] the first commity
 
file 
changed, 1 insertion(+)
 
create mode 100644 readme.txt
You have new mail 
in 
/var/spool/mail/root
说明,commit -m 后面的内容只是针对本次提交的一个描述
[root@ip-172-31-22-8 molewan]
# git status
# On branch master
nothing to commit, working directory clean
[root@ip-172-31-22-8 molewan]
# vim deply.sh
You have new mail 
in 
/var/spool/mail/root
[root@ip-172-31-22-8 molewan]
# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#deply.sh
nothing added to commit but untracked files present (use 
"git add" 
to track)
[root@ip-172-31-22-8 molewan]
# git add deply.sh 
[root@ip-172-31-22-8 molewan]
# git commit -m "2th commit"
[master f30d737] 2th commit
 
file 
changed, 2 insertions(+)
 
create mode 100644 deply.sh
[root@ip-172-31-22-8 molewan]
# git status
# On branch master
nothing to commit, working directory clean
[root@ip-172-31-22-8 molewan]
# ls -l
total 8
-rw-r--r--. 1 root root 22 Nov 26 17:15 deply.sh
-rw-r--r--. 1 root root  5 Nov 26 17:07 readme.txt

6、 使用git log进行查看

1
2
3
4
5
6
7
8
9
[root@ip-172-31-22-8 molewan]
# git log
commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:16:27 2016 -0500
    
2th commit
commit 24a5b3094f55e815ad46561f4b741c23bc7ad371
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:13:37 2016 -0500
    
the first commity

7、git diff进行对比

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ip-172-31-22-8 molewan]
# vim readme.txt 
# 添加一行文字,hehe
[root@ip-172-31-22-8 molewan]
# cat readme.txt 
hehe
hehe
[root@ip-172-31-22-8 molewan]
# 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
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
[root@ip-172-31-22-8 molewan]
# git diff readme.txt 
diff 
--git a
/readme
.txt b
/readme
.txt
index 91ca0fa..197cac2 100644
--- a
/readme
.txt
+++ b
/readme
.txt
@@ -1 +1,2 @@
 
hehe
+hehe
[root@ip-172-31-22-8 molewan]
# git add readme.txt 
[root@ip-172-31-22-8 molewan]
# git commit -m "add 2hehe"
[master c33cc4f] add 2hehe
 
file 
changed, 1 insertion(+)
[root@ip-172-31-22-8 molewan]
# git log
commit c33cc4f3c6a1dc6741bce035de920f5a17c82b4b
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:22:05 2016 -0500
    
add 2hehe
commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:16:27 2016 -0500
    
2th commit
commit 24a5b3094f55e815ad46561f4b741c23bc7ad371
:...skipping...
commit c33cc4f3c6a1dc6741bce035de920f5a17c82b4b
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:22:05 2016 -0500
    
add 2hehe
commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:16:27 2016 -0500
    
2th commit
commit 24a5b3094f55e815ad46561f4b741c23bc7ad371
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:13:37 2016 -0500
    
the first commity

小结:

1
2
3
4
5
6
7
8
a)所有的版本控制系统,其实只能跟踪本件的改动,如TXT件,网页,所有的程序代码等等,Git也不
例外。
b)版本控制系统 可以告诉你每次的改动,如在第5加了个单词“Linux”删了个单词“Windows”
c)图片视频这些二进制文件,虽然也能由版本 控制系统管理,但没法跟踪件的变化,只能把二进制文
件每次改动串起来,也就是只知道图从100KB改成了120KB,但到底改了啥,版本控制系统不知道,没法
知道。
d)不幸的是,Microsoft的Word格式是进制格式,因此,版本控制系统是没法跟踪Word件的改动的,
我们举的例只是为了演示,真正使版本控制系统,就要以纯本式编写件。

8、版本回退(回退到上一个版本)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@ip-172-31-22-8 molewan]
# git reset --hard HEAD^
HEAD is now at f30d737 2th commit
# 说明:HEAD^代表上一个版本,^^代表上上个版本
[root@ip-172-31-22-8 molewan]
# cat readme.txt 
hehe
[root@ip-172-31-22-8 molewan]
# git log
commit f30d737d24b2c04f8ce70a8c88b0071bdcc069c8
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:16:27 2016 -0500
    
2th commit
commit 24a5b3094f55e815ad46561f4b741c23bc7ad371
Author: molewan <314324506@qq.com>
Date:   Sat Nov 26 17:13:37 2016 -0500
    
the first commity

9、回退到指定的某个版本

1
2
3
4
5
6
7
8
9
10
11
12
[root@ip-172-31-22-8 molewan]
# git reflog
f30d737 HEAD@{0}: reset: moving to HEAD^
c33cc4f HEAD@{1}: commit: add 2hehe
f30d737 HEAD@{2}: commit: 2th commit
24a5b30 HEAD@{3}: commit (initial): the first commity
[root@ip-172-31-22-8 molewan]
# git reset --hard 24a5b30
HEAD is now at 24a5b30 the first commity
[root@ip-172-31-22-8 molewan]
# ls -l
total 4
-rw-r--r--. 1 root root 5 Nov 26 17:25 readme.txt
说明:Git的版本回退速度常快,因为Git在内部有个指向当前版本的HEAD指针,当你回退版本
的时候,Git仅仅是把HEAD从指向“append GPL”

小结:

1
2
3
4
a)HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使命令
git reset --hard commit_id。
b)整理、排版: numbbbbb 穿梭前,git log可以查看提交历史,以便确定要回退到哪个版本。
c) 要重返未来,git reflog查看命令历史,以便确定要回到未来的哪个版本

10、工作区、版本库、暂存区:

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

版本库(Repository):作区有个隐藏目录“.git”,这个不算作区,是Git的版本库。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@LVS-DR01 git-data]
# pwd
/git-data
#工作区
[root@LVS-DR01 git-data]
# ls -la
total 40
drwxr-xr-x   3 root root   119 Jun 29 11:22 .
dr-xr-xr-x. 19 root root   281 Jun 29 09:32 ..
-rw-r--r--   1 root root    10 Jun 29 11:20 dev.txt
drwxr-xr-x   8 root root   201 Jun 29 11:21 .git
#版本库
-rw-r--r--   1 root root  1045 Jun 29 11:20 .gitignore
-rw-r--r--   1 root root  8870 Jun 29 11:20 git.txt
-rw-r--r--   1 root root 11357 Jun 29 11:14 LICENSE
-rw-r--r--   1 root root    25 Jun 29 11:20 README.md
-rw-r--r--   1 root root  1774 Jun 29 11:20 谈运维.txt
[root@ip-172-31-22-8 molewan]
# git status
# On branch master
nothing to commit, working directory clean
需要先add,才能commit
说明:要掌握工作区的状态,使用git status命令,而用git 
diff
可以查看修改内容
本文转自 冰冻vs西瓜 51CTO博客,原文链接:http://blog.51cto.com/molewan/1943143,如需转载请自行联系原作者
你可能感兴趣的文章
16进制 <--转换--> 10进制(RGB)
查看>>
图的存储结构(邻接矩阵)
查看>>
OD使用教程14(山外有山) - 调试篇14
查看>>
逻辑运算符
查看>>
PE格式详细讲解6(下)- 系统篇06|解密系列
查看>>
POJ-3278-Catch That Cow(BFS)
查看>>
模板:cin.getline用法
查看>>
表单插件——form
查看>>
Oracle 服务命名(别名)的配置及原理,plsql连接用
查看>>
【转】JavaScript 中值得注意的 for 循环
查看>>
【哈佛商评】好编辑成就内容营销
查看>>
【视频】真实的北漂程序员生活记录
查看>>
HTML 获取屏幕、浏览器、页面的高度宽度
查看>>
近日经验总结
查看>>
资源管理器总是生成 avi,mpeg的预览图
查看>>
[J2EE框架][Debug]
查看>>
[20190419]shared latch spin count 2.txt
查看>>
POJ 3145 Harmony Forever
查看>>
BCM93349DCM 手动升级 Fireware 指导
查看>>
只保留最新5个log文件
查看>>