目录
背景说明
-
简单背景:
在linux发布后,linux不断壮大,因为是开源的原因,所以很多人把源码文件发给linux的作者整理,当然是手工整理,后来linux代码库就很大,无法手工整理如此多的文件,但是linux作者不想使用subversion版本控制等等原因。后来一个商业公司将其的分布式版本控制授权给linux开发社区免费使用。但是linux社区的黑客有点多,samba的开发者试图破解商业公司的分布式版本,所以商业公司就收回了。
然后基于C语言的Git分布式就上线了,并上传了linux的源码。
-
Git说明:
1:开源
2:关注文件数据整体变化,SHA-1加密
-
Git三种重要模式:
已提交、已修改、已暂存
安装配置
-
安装Git:
[root@www ~]# yum install -y git
......
更新完毕:
git.x86_64 0:1.8.3.1-14.el7_5
作为依赖被升级:
perl-Git.noarch 0:1.8.3.1-14.el7_5
完毕!
-
配置文件:
/etc/gitconfig、~/.gitconfig、~/.config/git/config、工作目录/.git/config(当然不一定都有这些文件,反正小博主没有)
1:配置个人用户名和电子邮箱。Git提交时会引用,随着更新内容永久存储在log中
[root@www etc]# git config --global user.name "Linux Git"
[root@www etc]# git config --global user.email "[email protected]"
2:设置vim为默认文本编辑器
[root@www etc]# git config --global core.editor vim
查询上面2项是否配置完成
[root@www etc]# git config --list
user.name=Linux Git
[email protected]
core.editor=vim
使用说明
-
提交数据:
1:创建本地工作目录
创建Git文档并初始化成Git的工作目录
[root@www ~]# mkdir Git文档
[root@www ~]# ls
Git文档 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@www ~]# cd Git文档/
[root@www Git文档]# ls
[root@www Git文档]# git init
初始化空的 Git 版本库于 /root/Git文档/.git/
Git下多为文件,不能判断图片、视频、可执行命令等
2:写一个新文件并提交
[root@www Git文档]# echo "你好!世界" >> world.txt
加到暂存区:
[root@www Git文档]# git add world.txt
修改本地文件内容:
[root@www Git文档]# echo "嗯,你好" >> world.txt
将暂存区的文件提交到Git版本仓库:
[root@www Git文档]# git commit -m "提交你好世界文件"
[master(根提交) b6cb176] 提交你好世界文件
1 file changed, 1 insertion(+)
create mode 100644 world.txt
查询工作目录的状态:
[root@www Git文档]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: world.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
提示:未提交——不是已经提交了吗?
对比本地文件和Git版本数据库中的文件:
[root@www Git文档]# git diff world.txt
diff --git a/world.txt b/world.txt
index 0ac89c2..83e1ccc 100644
--- a/world.txt
+++ b/world.txt
@@ -1 +1,2 @@
你好!世界
+嗯,你好
那是因为提交的是暂存区的文件,而本地的文件内容是:嗯,你好,所以对比后提示没有提交文件。所以,修改了本地文件就需要重新提交暂存区,再由暂存区提交到Git版本仓库中。
[root@www Git文档]# git add world.txt
[root@www Git文档]# git commit -m "提交新的你好世界文件"
[master 2a678f1] 提交新的你好世界文件
1 file changed, 1 insertion(+)
查询Git版本的状态信息:
[root@www Git文档]# git status
# 位于分支 master
无文件要提交,干净的工作区
-
提交文件小技巧:
当修改的文件较多的时候,可以选择一次提交所有文件到暂存区
[root@www Git文档]# git add .
[root@www Git文档]# git commit -m "提交所有文件"
忽略部分上传的文件
[root@www Git文档]# touch git.c
[root@www Git文档]# vim .gitignore
git.c
说明:忽略(*.a以.a结束文件、!a.txt以a.txt结束文件、dir/ dir下的所以文件、dir/*.txt dir下所有以.txt结束文件、git.c git.c的文件)
[root@www Git文档]# git add .
[root@www Git文档]# git commit -m "提交没有git.c的文件"
[master cf70b73] 提交没有git.c的文件
1 file changed, 1 insertion(+)
create mode 100644 .gitignore
直接上传到git数据库
[root@www Git文档]# echo "直接上传" >> 1.txt
[root@www Git文档]# git commit -a -m "直接上传到git数据库"
# 位于分支 master
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# 1.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
我们知道git.c是忽略的文件,那么怎么强制上传文件?
[root@www Git文档]# git add -f git.c
[root@www Git文档]# git commit --amend
提交没有git.c的文件
# 请为您的变更输入提交说明。以 '#' 开始的行将被忽略,而一个空的提交
# 说明将会终止提交。
# 位于分支 master
# 要提交的变更:
# (使用 "git reset HEAD^1 <file>..." 撤出暂存区)
#
# 新文件: .gitignore
# 新文件: git.c
#
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# 1.txt
浏览信息后输入:wq!
[master 9b509cf] 提交没有git.c的文件
2 files changed, 1 insertion(+)
create mode 100644 .gitignore
create mode 100644 git.c
-
删除数据:
1:只删除暂存区的文件,不删本地文件和git仓库的文件
[root@www Git文档]# touch 10
[root@www Git文档]# git add 10
[root@www Git文档]# git status
# 位于分支 master
# 要提交的变更:
# (使用 "git reset HEAD <file>..." 撤出暂存区)
#
# 新文件: 10
#
如上,先将10这个文件加入暂存区
删除10这个文件
[root@www Git文档]# git rm --cached 10
rm '10'
查询本地文件10是否删除
[root@www Git文档]# ls
10 1.txt git.c world.txt
查询暂存区文件10
[root@www Git文档]# git status
# 位于分支 master
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# 1.txt
# 10
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
2:删除暂存区和本地工作目录的文件
[root@www Git文档]# git add .
[root@www Git文档]# status
bash: status: 未找到命令...
[root@www Git文档]# git status
# 位于分支 master
# 要提交的变更:
# (使用 "git reset HEAD <file>..." 撤出暂存区)
#
# 新文件: 1.txt
# 新文件: 10
#
[root@www Git文档]# git rm -f 10
rm '10'
[root@www Git文档]# git status
# 位于分支 master
# 要提交的变更:
# (使用 "git reset HEAD <file>..." 撤出暂存区)
#
# 新文件: 1.txt
#
[root@www Git文档]# ls
1.txt git.c world.txt
- 修改文件名称:
[root@www Git文档]# git mv world.txt hello.txt
[root@www Git文档]# git status
# 位于分支 master
# 要提交的变更:
# (使用 "git reset HEAD <file>..." 撤出暂存区)
#
# 新文件: 1.txt
# 重命名: world.txt -> hello.txt
#
[root@www Git文档]# git commit -m "修改名称后的文件"
[master a0e319f] 修改名称后的文件
2 files changed, 1 insertion(+)
create mode 100644 1.txt
rename world.txt => hello.txt (100%)
[root@www Git文档]# ls
1.txt git.c hello.txt
当然也可以mv文件,然后删除以前暂存区的文件,再上传修改后的文件到暂存区,再上传到Git数据库中。难点就是暂存区。
-
历史记录:
[root@www Git文档]# git log
commit a0e319faf3ed97343fd589a8df7351069c73e560
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:51:51 2018 +0800
修改名称后的文件
commit 9b509cfda79b99e965e1cf3994094486758701a3
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:31:41 2018 +0800
提交没有git.c的文件
commit 2a678f1ffef581d79c5346e7f0ca3f0b197c8a88
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:16:30 2018 +0800
提交新的你好世界文件
commit b6cb176796a3ca73a1953c0dc56569337584b39e
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:07:10 2018 +0800
提交你好世界文件
[root@www Git文档]# git log -2
commit a0e319faf3ed97343fd589a8df7351069c73e560
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:51:51 2018 +0800
修改名称后的文件
commit 9b509cfda79b99e965e1cf3994094486758701a3
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:31:41 2018 +0800
提交没有git.c的文件
查询最后一次的文件差异有哪些:
[root@www Git文档]# git log -p -2
commit a0e319faf3ed97343fd589a8df7351069c73e560
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:51:51 2018 +0800
修改名称后的文件
diff --git a/1.txt b/1.txt
new file mode 100644
index 0000000..f28fcb9
--- /dev/null
+++ b/1.txt
@@ -0,0 +1 @@
+直接上传
diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..83e1ccc
--- /dev/null
+++ b/hello.txt
@@ -0,0 +1,2 @@
+你好!世界
+嗯,你好
diff --git a/world.txt b/world.txt
deleted file mode 100644
index 83e1ccc..0000000
--- a/world.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-你好!世界
-嗯,你好
commit 9b509cfda79b99e965e1cf3994094486758701a3
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:31:41 2018 +0800
提交没有git.c的文件
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a00368a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+git.c
diff --git a/git.c b/git.c
new file mode 100644
index 0000000..e69de29
使用--stat来显示数据增改的行数:
[root@www Git文档]# git log --stat -2
commit a0e319faf3ed97343fd589a8df7351069c73e560
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:51:51 2018 +0800
修改名称后的文件
1.txt | 1 +
hello.txt | 2 ++
world.txt | 2 --
3 files changed, 3 insertions(+), 2 deletions(-)
commit 9b509cfda79b99e965e1cf3994094486758701a3
Author: Linux Git <[email protected]>
Date: Tue Nov 13 16:31:41 2018 +0800
提交没有git.c的文件
.gitignore | 1 +
git.c | 0
2 files changed, 1 insertion(+)
展示信息(每一行显示一条提交记录的方式):
[root@www Git文档]# git log --pretty=oneline
a0e319faf3ed97343fd589a8df7351069c73e560 修改名称后的文件
9b509cfda79b99e965e1cf3994094486758701a3 提交没有git.c的文件
2a678f1ffef581d79c5346e7f0ca3f0b197c8a88 提交新的你好世界文件
b6cb176796a3ca73a1953c0dc56569337584b39e 提交你好世界文件
更为详细的信息:
[root@www Git文档]# git log --pretty=fuller -2
commit a0e319faf3ed97343fd589a8df7351069c73e560
Author: Linux Git <[email protected]>
AuthorDate: Tue Nov 13 16:51:51 2018 +0800
Commit: Linux Git <[email protected]>
CommitDate: Tue Nov 13 16:51:51 2018 +0800
修改名称后的文件
commit 9b509cfda79b99e965e1cf3994094486758701a3
Author: Linux Git <[email protected]>
AuthorDate: Tue Nov 13 16:31:41 2018 +0800
Commit: Linux Git <[email protected]>
CommitDate: Tue Nov 13 16:38:34 2018 +0800
提交没有git.c的文件
显示所有提交者:
[root@www Git文档]# git log --pretty=format:"%cn"
Linux Git
Linux Git
Linux Git
Linux Git
-
还原数据:
[root@www Git文档]# cat hello.txt
你好!世界
嗯,你好
修改hello.txt
[root@www Git文档]# echo "你好!" > hello.txt
[root@www Git文档]# cat hello.txt
你好!
提交修改过后的文件
[root@www Git文档]# git add hello.txt
[root@www Git文档]# git commit -m "修改hello.txt文件"
[master 2c04eee] 修改hello.txt文件
1 file changed, 1 insertion(+), 2 deletions(-)
开始还原:
[root@www Git文档]# git log --pretty=oneline
2c04eee7ad9935e14642a68218ecc366240864fd 修改hello.txt文件
a0e319faf3ed97343fd589a8df7351069c73e560 修改名称后的文件
9b509cfda79b99e965e1cf3994094486758701a3 提交没有git.c的文件
2a678f1ffef581d79c5346e7f0ca3f0b197c8a88 提交新的你好世界文件
b6cb176796a3ca73a1953c0dc56569337584b39e 提交你好世界文件
[root@www Git文档]# git reset --hard HEAD^
HEAD 现在位于 a0e319f 修改名称后的文件
[root@www Git文档]# cat hello.txt
你好!世界
嗯,你好
恢复上一个版本HEAD^;上上个版本HEAD^^;上5个版本HEAD~5
再次查询版本信息:
[root@www Git文档]# git log --pretty=oneline
a0e319faf3ed97343fd589a8df7351069c73e560 修改名称后的文件
9b509cfda79b99e965e1cf3994094486758701a3 提交没有git.c的文件
2a678f1ffef581d79c5346e7f0ca3f0b197c8a88 提交新的你好世界文件
b6cb176796a3ca73a1953c0dc56569337584b39e 提交你好世界文件
最新修改过的版本没有了?并不是,这里只是没显示,用下面的方法可以查询到使用恢复命令之前的版本信息
[root@www Git文档]# git reflog
a0e319f HEAD@{0}: reset: moving to HEAD^
2c04eee HEAD@{1}: commit: 修改hello.txt文件
a0e319f HEAD@{2}: commit: 修改名称后的文件
9b509cf HEAD@{3}: commit (amend): 提交没有git.c的文件
cf70b73 HEAD@{4}: commit: 提交没有git.c的文件
2a678f1 HEAD@{5}: commit: 提交新的你好世界文件
b6cb176 HEAD@{6}: commit (initial): 提交你好世界文件
还原到没有使用过恢复命令的版本
[root@www Git文档]# git reset --hard 2c04eee
HEAD 现在位于 2c04eee 修改hello.txt文件
还原一个文件:
[root@www Git文档]# cat hello.txt
你好!
[root@www Git文档]# echo "你好!世界" > hello.txt
[root@www Git文档]# cat hello.txt
你好!世界
写错了,恢复文件
[root@www Git文档]# git checkout -- hello.txt
[root@www Git文档]# cat hello.txt
你好!
Git优先还原暂存区中有的该文件,再使用Git数据库中的文件
-
管理标签:
给最近一次提交信息标签
[root@www Git文档]# git tag v1.0
查询所有标签
[root@www Git文档]# git tag
v1.0
查询标签的信息
[root@www Git文档]# git show v1.0
commit 2c04eee7ad9935e14642a68218ecc366240864fd
Author: Linux Git <[email protected]>
Date: Tue Nov 13 17:06:56 2018 +0800
修改hello.txt文件
diff --git a/hello.txt b/hello.txt
index 83e1ccc..efde6ef 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,2 +1 @@
-你好!世界
-嗯,你好
+你好!
创建说明标签:-a指定标签名当然也可以不加的,-m指定说明信息,
[root@www Git文档]# git tag v10 -m "这个v10标签"
最新版本有2个标签了v1.0和v10,所有删除之前的标签
[root@www Git文档]# git tag -d v1.0
已删除 tag 'v1.0'(曾为 2c04eee)
-
管理分支结构:
在工作中,主分支都是用于新版本的发布,所有平常更改的信息都需要存在个人创建的分支中
1:创建分支
[root@www Git文档]# git branch 个人文件
切换到分支个人文件中
[root@www Git文档]# git checkout 个人文件
切换到分支 '个人文件'
查询当前分支情况
[root@www Git文档]# git branch
master
* 个人文件
修改hello.txt文件
[root@www Git文档]# cat hello.txt
你好!
[root@www Git文档]# echo "个人文件" >> hello.txt
[root@www Git文档]# cat hello.txt
你好!
个人文件
提交:
[root@www Git文档]# git add hello.txt
[root@www Git文档]# git commit -m "个人文件类型"
[个人文件 0ddccfc] 个人文件类型
1 file changed, 1 insertion(+)
那么,回到主分支的话会有什么发生了?
[root@www Git文档]# git checkout master
切换到分支 'master'
[root@www Git文档]# cat hello.txt
你好!
嗯,文件还是以前的文件
2:合并分支
[root@www Git文档]# git merge 个人文件
更新 2c04eee..0ddccfc
Fast-forward
hello.txt | 1 +
1 file changed, 1 insertion(+)
[root@www Git文档]# cat hello.txt
你好!
个人文件
合并后删除个人文件这个分支了
[root@www Git文档]# git branch -d 个人文件
已删除分支 个人文件(曾为 0ddccfc)。
查询:
[root@www Git文档]# git branch
* master
3:分支冲突
当合并内容比较复杂的时候,就需要手动合并分支
创建并切换到该分支:
[root@www Git文档]# git checkout -b 冲突文件
切换到一个新分支 '冲突文件'
[root@www Git文档]# git branch
master
* 冲突文件
修改hello.txt文件
[root@www Git文档]# cat hello.txt
你好!
个人文件
[root@www Git文档]# echo "冲突文件" > hello.txt
[root@www Git文档]# cat hello.txt
冲突文件
在分支冲突文件中提交内容:
[root@www Git文档]# git add hello.txt
[root@www Git文档]# git commit -m "提交冲突文件"
[冲突文件 59c6a99] 提交冲突文件
1 file changed, 1 insertion(+), 2 deletions(-)
切换到master:
[root@www Git文档]# git checkout master
切换到分支 'master'
在master中修改hello.txt文件并提交
[root@www Git文档]# git checkout master
切换到分支 'master'
[root@www Git文档]# cat hello.txt
你好!
个人文件
[root@www Git文档]# echo "在master中修改的文件" > hello.txt
[root@www Git文档]# cat hello.txt
在master中修改的文件
[root@www Git文档]# git add hello.txt
[root@www Git文档]# git commit -m "提交master中的冲突文件"
[master 6e74cb2] 提交master中的冲突文件
1 file changed, 1 insertion(+), 2 deletions(-)
现在就是修改了冲突文件和master的文件,那么现在合并分支的情况如下:
[root@www Git文档]# git merge 冲突文件
自动合并 hello.txt
冲突(内容):合并冲突于 hello.txt
自动合并失败,修正冲突然后提交修正的结果。
查询冲突内容:
[root@www Git文档]# cat hello.txt
<<<<<<< HEAD
在master中修改的文件
=======
冲突文件
>>>>>>> 冲突文件
说明:
<<<<<<正在使用的master
分支下的文件内容;不是文件名称
=======
冲突文件的内容;不是冲突文件的名称
>>>>>>受到冲突文件影响的分支结构
手动修改:
[root@www Git文档]# vim hello.txt
在master中修改的文件,删除了冲突文件分支中的内容
[root@www Git文档]# cat hello.txt
在master中修改的文件,删除了冲突文件分支中的内容
提交:
[root@www Git文档]# git commit -m "提交合并冲突分支的内容"
[master 61c116c] 提交合并冲突分支的内容
[root@www Git文档]# git log --graph --pretty=oneline --abbrev-commit
* 61c116c 提交合并冲突分支的内容
|\
| * 59c6a99 提交冲突文件
* | 6e74cb2 提交master中的冲突文件
|/
* 0ddccfc 个人文件类型
* 2c04eee 修改hello.txt文件
* a0e319f 修改名称后的文件
* 9b509cf 提交没有git.c的文件
* 2a678f1 提交新的你好世界文件
* b6cb176 提交你好世界文件
合并分支冲突文件后,就删除冲突文件吧
[root@www Git文档]# git branch -d 冲突文件
已删除分支 冲突文件(曾为 59c6a99)。
[root@www Git文档]# git branch
* master
好了,本文的内容就到这里了