第五节、Git相关操作

5.1、Centos7下安装Git

安装的前提是已配置阿里源以及扩展epel源

[root@docker ~]# yum install -y git
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repository contrib is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                    | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                        | 3.5 kB  00:00:00     
epel                                                                                                                    | 4.7 kB  00:00:00     
extras                                                                                                                  | 2.9 kB  00:00:00     
updates                                                                                                                 | 2.9 kB  00:00:00     
(1/3): docker-ce-stable/7/x86_64/primary_db                                                                             |  82 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                                                           | 1.0 MB  00:00:00     
(3/3): epel/x86_64/primary_db                                                                                           | 7.0 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package git.x86_64 0:1.8.3.1-23.el7_8 will be installed
--> Processing Dependency: perl-Git = 1.8.3.1-23.el7_8 for package: git-1.8.3.1-23.el7_8.x86_64
--> Processing Dependency: rsync for package: git-1.8.3.1-23.el7_8.x86_64
--> Processing Dependency: perl(Term::ReadKey) for package: git-1.8.3.1-23.el7_8.x86_64
--> Processing Dependency: perl(Git) for package: git-1.8.3.1-23.el7_8.x86_64
--> Processing Dependency: perl(Error) for package: git-1.8.3.1-23.el7_8.x86_64
--> Running transaction check
---> Package perl-Error.noarch 1:0.17020-2.el7 will be installed
---> Package perl-Git.noarch 0:1.8.3.1-23.el7_8 will be installed
---> Package perl-TermReadKey.x86_64 0:2.30-20.el7 will be installed
---> Package rsync.x86_64 0:3.1.2-11.el7_9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================
 Package                               Arch                        Version                                  Repository                    Size
===============================================================================================================================================
Installing:
 git                                   x86_64                      1.8.3.1-23.el7_8                         base                         4.4 M
Installing for dependencies:
 perl-Error                            noarch                      1:0.17020-2.el7                          base                          32 k
 perl-Git                              noarch                      1.8.3.1-23.el7_8                         base                          56 k
 perl-TermReadKey                      x86_64                      2.30-20.el7                              base                          31 k
 rsync                                 x86_64                      3.1.2-11.el7_9                           updates                      408 k

Transaction Summary
===============================================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 4.9 M
Installed size: 23 M
Downloading packages:
(1/5): perl-Error-0.17020-2.el7.noarch.rpm                                                                              |  32 kB  00:00:00     
(2/5): perl-Git-1.8.3.1-23.el7_8.noarch.rpm                                                                             |  56 kB  00:00:00     
(3/5): perl-TermReadKey-2.30-20.el7.x86_64.rpm                                                                          |  31 kB  00:00:00     
(4/5): rsync-3.1.2-11.el7_9.x86_64.rpm                                                                                  | 408 kB  00:00:00     
(5/5): git-1.8.3.1-23.el7_8.x86_64.rpm                                                                                  | 4.4 MB  00:00:00     
-----------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                          5.9 MB/s | 4.9 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:perl-Error-0.17020-2.el7.noarch                                                                                           1/5 
  Installing : perl-TermReadKey-2.30-20.el7.x86_64                                                                                         2/5 
  Installing : rsync-3.1.2-11.el7_9.x86_64                                                                                                 3/5 
  Installing : perl-Git-1.8.3.1-23.el7_8.noarch                                                                                            4/5 
  Installing : git-1.8.3.1-23.el7_8.x86_64                                                                                                 5/5 
  Verifying  : git-1.8.3.1-23.el7_8.x86_64                                                                                                 1/5 
  Verifying  : 1:perl-Error-0.17020-2.el7.noarch                                                                                           2/5 
  Verifying  : rsync-3.1.2-11.el7_9.x86_64                                                                                                 3/5 
  Verifying  : perl-Git-1.8.3.1-23.el7_8.noarch                                                                                            4/5 
  Verifying  : perl-TermReadKey-2.30-20.el7.x86_64                                                                                         5/5 

Installed:
  git.x86_64 0:1.8.3.1-23.el7_8                                                                                                                

Dependency Installed:
  perl-Error.noarch 1:0.17020-2.el7  perl-Git.noarch 0:1.8.3.1-23.el7_8  perl-TermReadKey.x86_64 0:2.30-20.el7  rsync.x86_64 0:3.1.2-11.el7_9 

Complete!

5.2、修改环境变量定制Git环境

#使用如下命令配置环境变量,信息会写入不同的文件中
git config  --global XXX
--system   针对任意登录用户,git配置信息写入/etc/gitconfig
--global    全局只针对当前登录用户生效,git配置写入~/.gitconfig (用的最多)
--local    本地,仅针对某个文件生效  /learn/database/.git/config

配置用户信息

#配置用户名
[root@docker ~]# git config --global user.name "hexu"
#配置邮箱
[root@docker ~]# git config --global user.email "345654857@qq.com"
#配置git语法高亮显示
[root@docker ~]# git config --global color.ui true
[root@docker ~]# cat ~/.gitconfig 
[user]
    name = hexu
    email = 345654857@qq.com
[color]
    ui = true
#查看git版本信息
[root@docker ~]# git --version
git version 1.8.3.1
#列举当前用户配置信息
[root@docker ~]# git config --global --list
user.name=hexu
user.email=345654857@qq.com
color.ui=true

5.3、Git的四个工作区域

01a42c8e926c9e2d1a0a5774be1c824.png

3941f7442289ea1e58f53663dfc6d71.png

a20a624339ae44921f738ed77c20ba6.png

5.4、初始化Git本地仓库

创建目录初始化为git本地仓库目录(存在.git隐藏目录就是本地仓库)

[root@docker ~]# mkdir hello_git
[root@docker ~]# git init hello_git/
Initialized empty Git repository in /root/hello_git/.git/
[root@docker ~]# cd hello_git/
[root@docker hello_git]# ll -a
total 0
drwxr-xr-x  3 root root  18 Sep 12 23:29 .
dr-xr-x---. 4 root root 198 Sep 12 23:29 ..
drwxr-xr-x  7 root root 119 Sep 12 23:29 .git
[root@docker hello_git]# cd .git/
[root@docker .git]# tree
.
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

9 directories, 13 files

5.5、Git stash临时空间

f4c05b0a2dc335a5ec0bfd89ba98f7f.png
#在工作区创建文件
[root@docker hello_git]# touch he.txt
#将创建文件提交暂存区
[root@docker hello_git]# git add .
#将暂存区信息提交本地仓库
[root@docker hello_git]# git commit -m "first submit"
[master (root-commit) 259186e] first submit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 he.txt
#查看提交记录
[root@docker hello_git]# git log
commit 259186e30f269d1c164b2cb00046ab9d92525b68
Author: hexu <345654857@qq.com>
Date:   Mon Sep 12 23:45:43 2022 +0800

    first submit
#创建第二个未追踪文件second.git.txt
[root@docker hello_git]# echo "learn more" > second.git.txt
#以下提示表示该文件未追踪
[root@docker hello_git]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   second.git.txt
nothing added to commit but untracked files present (use "git add" to track)
#将文件提交暂存区
[root@docker hello_git]# git add .
[root@docker hello_git]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   second.git.txt
#将暂存区文件临时转存的stash临时区并加注释
[root@docker hello_git]# git stash save  "save  second.git.txt"
Saved working directory and index state On master: save  second.git.txt
HEAD is now at 259186e first submit
#此时检查仓库目录下已无法找到 second.git.txt
[root@docker hello_git]# ls
he.txt
#列举临时区文件
[root@docker hello_git]# git stash list
stash@{0}: On master: save second.git.txt
 git stash pop    #将临时区最新进度恢复
 git stash pop  stash@{0}  #根据ID从临时区恢复
 git stash clear  #清空全部存储的临时区数据
 git stash drop  stash@{0}
[root@docker hello_git]# git stash pop
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   second.git.txt
#
Dropped refs/stash@{0} (10657f1158bf852253a300df39d8b8a1f228365f)
[root@docker hello_git]# ls
he.txt  second.git.txt

5.6、Git创建分支并解决分支冲突

当主线master与分支上存在同名文件并同时从暂存区提交时会发生分支冲突,git会自动将同名文件合并,在合并的文件中标识出不同内容,需要手动审核合并后重新提交文件

#step1、在主分支上创建文件并提交
#确认当前处于master主干上
[root@docker hello_git]# git branch
* master
#创建test.txt文件添加暂存区后并提交本地仓库
[root@docker hello_git]# touch testchongtu.txt
[root@docker hello_git]# git add .
[root@docker hello_git]# git commit -m "creat"
[master 8c5c11f] creat
 2 files changed, 1 insertion(+)
 create mode 100644 second.git.txt
 create mode 100644 testchongtu.txt
[root@docker hello_git]# git log --oneline
8c5c11f creat
259186e first submit
#往文件中增加内容后添加暂存区并提交本地仓库
[root@docker hello_git]# echo "11111" >testchongtu.txt 
[root@docker hello_git]# git add .
[root@docker hello_git]# git commit -m "change content"
[master e5fb80f] change content
 1 file changed, 1 insertion(+)
[root@docker hello_git]# git log --oneline
e5fb80f change content
8c5c11f creat
259186e first submit
#step2创建分支并在分支上创建同名文件并提交
#创建分支hexu
[root@docker hello_git]# git branch hexu
#切换到分支并检查星号处在当前分支前面
[root@docker hello_git]# git checkout hexu
Switched to branch 'hexu'
[root@docker hello_git]# git branch 
* hexu
  master
#切回master主干并删除分支
[root@docker hello_git]# git checkout master
Switched to branch 'master'
[root@docker hello_git]# git branch 
  hexu
* master
[root@docker hello_git]# git branch -d hexu
Deleted branch hexu (was e5fb80f).
[root@docker hello_git]# git branch 
* master
#创建分支并直接切换到分支
[root@docker hello_git]# git checkout -b hexu
Switched to a new branch 'hexu'
[root@docker hello_git]# git branch
* hexu
  master
[root@docker hello_git]#  touch testchongtu.txt
[root@docker hello_git]# git add .
[root@docker hello_git]# git commit -m "creat branch" 
# On branch hexu
nothing to commit, working directory clean
[root@docker hello_git]# echo "i am your brother
" >testchongtu.txt 
[root@docker hello_git]# git add .
[root@docker hello_git]# git commit -m "change content branch" 
[hexu dcc4274] change content branch
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@docker hello_git]# cat testchongtu.txt 
i am your brother

#在分支上查看git log
[root@docker hello_git]# git log --oneline
dcc4274 change content branch #分支的提交记录
e5fb80f change content             #master的提交记录
8c5c11f creat
259186e first submit
#切回master查看文件内容(与分支不同)
[root@docker hello_git]# git checkout master
Switched to branch 'master'
[root@docker hello_git]# ls
he.txt  second.git.txt  testchongtu.txt
[root@docker hello_git]# cat testchongtu.txt 
11111
#step3 进行分支合并会出现提示,git会合并两个文件
[root@docker hello_git]# git merge hexu
Auto-merging testchongtu.txt
CONFLICT (content): Merge conflict in testchongtu.txt
Automatic merge failed; fix conflicts and then commit the result.
[root@docker hello_git]# cat testchongtu.txt 
<<<<<<< HEAD
11111
=======
i am your brother
>>>>>>> hexu
#step4手动处理文件后重新提交
[root@docker hello_git]# cat testchongtu.txt 
11111
i am your brother
[root@docker hello_git]# git add .
[root@docker hello_git]# git commit -m "merge finish"
[master 8b77f42] merge finish
[root@docker hello_git]# git log --oneline
8b77f42 merge finish
ef64681 second change content branch
b834cb0 second change
dcc4274 change content branch
e5fb80f change content
8c5c11f creat
259186e first submit

5.7、Git版本回退以及切换

#step1 检查测试文件内容以及修改信息
[root@docker hello_git]# git log --oneline
8b77f42 merge finish
ef64681 second change content branch
b834cb0 second change
dcc4274 change content branch
e5fb80f change content
8c5c11f creat
259186e first submit
[root@docker hello_git]# cat testchongtu.txt 
11111
i am your brother
#step2 返回以前版本通过ID
[root@docker hello_git]# git reset --hard dcc4274
HEAD is now at dcc4274 change content branch
[root@docker hello_git]# cat testchongtu.txt 
2222
[root@docker hello_git]# git log --oneline
dcc4274 change content branch
e5fb80f change content
8c5c11f creat
259186e first submit
#补充返回上一个版本
[root@docker hello_git]# git reset --hard HEAD^
[root@docker hello_git]# git reset --hard HEAD^^
#step3 查看变化日志切换版本
[root@docker hello_git]# git reflog
dcc4274 HEAD@{0}: reset: moving to dcc4274
8b77f42 HEAD@{1}: commit (merge): merge finish
b834cb0 HEAD@{2}: checkout: moving from hexu to master
ef64681 HEAD@{3}: commit: second change content branch
dcc4274 HEAD@{4}: checkout: moving from master to hexu
b834cb0 HEAD@{5}: commit: second change
dcc4274 HEAD@{6}: checkout: moving from hexu to master
dcc4274 HEAD@{7}: checkout: moving from master to hexu
dcc4274 HEAD@{8}: merge hexu: Fast-forward
e5fb80f HEAD@{9}: checkout: moving from hexu to master
dcc4274 HEAD@{10}: commit: change content branch
e5fb80f HEAD@{11}: checkout: moving from master to hexu
e5fb80f HEAD@{12}: checkout: moving from hexu to master
e5fb80f HEAD@{13}: checkout: moving from master to hexu
e5fb80f HEAD@{14}: commit: change content
8c5c11f HEAD@{15}: commit: creat
259186e HEAD@{16}: commit (initial): first submit
#可以通过该命令查看所有变动并获取对应ID,之后就能通过git reset  --hard  ID命令来切换

5.8、Git向远程仓库推送以及拉取更新

#这是在本地添加暂存区并提交本地仓库后再向云端仓库推送的步骤
[root@docker hello_git]# git add .
[root@docker hello_git]# git commit -m "提交本地仓库"
[root@docker hello_git]# git push -u origin  master
#从云端拉取
[root@docker hello_git]#  git pull origin master
#这里的origin是别名已经定义过云端的URL
git remote origin  git@gitee.com:XXXX
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,222评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,455评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,720评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,568评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,696评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,879评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,028评论 3 409
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,773评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,220评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,550评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,697评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,360评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,002评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,782评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,010评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,433评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,587评论 2 350

推荐阅读更多精彩内容