第五节、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 "[email protected]"
#配置git语法高亮显示
[root@docker ~]# git config --global color.ui true
[root@docker ~]# cat ~/.gitconfig 
[user]
    name = hexu
    email = [email protected]
[color]
    ui = true
#查看git版本信息
[root@docker ~]# git --version
git version 1.8.3.1
#列举当前用户配置信息
[root@docker ~]# git config --global --list
user.name=hexu
[email protected]
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 <[email protected]>
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 ..." 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 ..." 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 ..." 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  [email protected]:XXXX

你可能感兴趣的:(第五节、Git相关操作)