版本控制系统-Git
Posted jiawei2527
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了版本控制系统-Git相关的知识,希望对你有一定的参考价值。
一. 版本控制系统简介
• 版本控制系统是用来管理一些大型的修改比较频繁的程序或者文件的管理工具
• 版本控制系统可以自动备份文件或程序到服务器,当我们需要的时候可以回溯到指定版本的文件
二. 常见的版本控制系统
1. SVN
• SVN是一个集中式的版本控制系统,只有一个中央仓库
• 如果中央仓库挂了或者不可访问,所有使用者都无法使用SVN,无法进行提交或者备份
• SVN把所有数据按文件储存,把所有的元数据信息隐藏在一个类似.svn的.cvs的文件夹里
2. GIT
• GIT是分布式版本控制系统,在每个使用者的电脑上都有一个数据仓库,没有网络依然可以使用GIT
• GIT的操作是先把代码先提交到本地仓库,确认代码无误后再同步到服务器
• GIT把数据按元数据保存,所有保存的数据都计算了SHA-1哈希算法,更能保证文件完整性
• GIT没有全局的版本号而SVN有全局版本号
三. 部署GIT
1. 安装git
[root@cache01 ~]# yum install git -y
[root@cache01 ~]# rpm -qa |grep -w "git"
git-1.8.3.1-23.el7_8.x86_64
2. 配置Git
1) git config # git配置命令帮助
git config --global user.name "learn git"
git config --global user.email "962532419@qq.com"
2) git config --list 查看git的配置
[root@cache01 ~]# git config --list
user.name=learn git
user.email=962532419@qq.com
3).Git配置分为三个方面:系统配置、全局(用户)配置、仓库配置
系统配置:--system 会在/etc下生成.gitconfig文件
用户配置:--global 会在用户家目录下生成.gitconfig文件
仓库配置:--local 会在git仓库目录下生成.gitconfig文件
3. 初始化工作目录
1). 新建空间文件夹 mkdir /server/git_test
[root@cache01 ~]# mkdir /server/git_test
2). 进入目录,执行git init对空目录初始化
[root@cache01 ~]# cd /server/git_test/
[root@cache01 git_test]# git init
Initialized empty Git repository in /server/git_test/.git/
3). 进入.git文件夹,查看目录结构
四. Git的四个区域
1. 工作目录
• 工作目录就是本地自己创建并初始化的git目录
2. 暂存区域
• 第一次提交数据git会在工作目录生成index文件,文件里所记录的是位于暂存区的文件
3. 本地仓库
• 工作目录中的object文件夹是本地仓库
4. 远程仓库
• 通过网络连接的数据库称为远程仓库
五. GIT的四种状态
Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged
Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件
Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改
Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified
六、git的基本操作
1. 查看命令
1)git status
# 查看工作目录当前同步状态
a. 状态一:本地仓库、暂存区和工作目录数据一致
[root@cache01 git_test]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
b. 状态二:工作目录有文件未同步
[root@cache01 git_test]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a.txt
# b.txt
# c.txt
# d.txt
nothing added to commit but untracked files present (use "git add" to track)
c. 状态三:暂存区有文件未同步
[root@cache01 git_test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a.txt
#
2)git diff
# 对比工作目录数据和暂存区数据差异
参数列表:
--cached 对比暂存区数据和本地仓库数据差异
a. 对比工作目录数据和暂存区数据差异
[root@cache01 git_test]# ll
total 0
-rw-r--r-- 1 root root 0 Jun 20 18:36 a.txt
-rw-r--r-- 1 root root 0 Jun 20 18:36 b.txt
-rw-r--r-- 1 root root 0 Jun 20 18:36 c.txt
-rw-r--r-- 1 root root 0 Jun 20 18:36 d.txt
[root@cache01 git_test]# echo "hello wolrd" >>a.txt
[root@cache01 git_test]# git diff
diff --git a/a.txt b/a.txt
index e69de29..e00f89a 100644
--- a/a.txt
+++ b/a.txt
@@ -0,0 +1 @@
+hello wolrd
b. 对比暂存区数据和本地仓库数据差异
[root@cache01 git_test]# git add a.txt
[root@cache01 git_test]# git diff
[root@cache01 git_test]# git diff --cached
diff --git a/a.txt b/a.txt
index e69de29..e00f89a 100644
--- a/a.txt
+++ b/a.txt
@@ -0,0 +1 @@
+hello wolrd
3)git log
# 查询当前commit信息
参数列表:
--oneline 简略显示当前commit信息
--oneline --decorate 查看当前分支信息
-p 详细显示当前分支信息
--reflog 查看历史commit记录
a. 查询当前commit信息
[root@cache01 git_test]# git log
commit d376e3a554c16079cd71b0809fc45ff42cd924aa
Author: learn git <962532419@qq.com>
Date: Sat Jun 20 18:48:07 2020 +0800
modified a.txt v1.0
commit c8a06f3f35e2f3df93842ef5cafa54772ac46198
Author: learn git <962532419@qq.com>
Date: Sat Jun 20 18:40:15 2020 +0800
commit some files
b. 简略显示当前commit信息
[root@cache01 git_test]# git log --oneline
d376e3a modified a.txt v1.0
c8a06f3 commit some files
c. 查看当前分支信息
[root@cache01 git_test]# git log --oneline --decorate
d376e3a (HEAD, master) modified a.txt v1.0
c8a06f3 commit some files
d. 查看详细的commit信息
[root@cache01 git_test]# git log --oneline -p
d376e3a modified a.txt v1.0
diff --git a/a.txt b/a.txt
index e69de29..e00f89a 100644
--- a/a.txt
+++ b/a.txt
@@ -0,0 +1 @@
+hello wolrd
c8a06f3 commit some files
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..e69de29
diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..e69de29
diff --git a/c.txt b/c.txt
new file mode 100644
index 0000000..e69de29
diff --git a/d.txt b/d.txt
new file mode 100644
index 0000000..e69de29
e. 查看历史commit记录
[root@cache01 git_test]# git reflog
ce4c70b HEAD@{0}: commit: modified a.txt v1.1
d376e3a HEAD@{1}: commit: modified a.txt v1.0
c8a06f3 HEAD@{2}: commit (initial): commit some files
2. 添加命令
1). git add
# 提交工作目录数据到暂存区
参数列表:
<filename> 把某个文件提交到暂存区
. 把所有文件都提交到暂存区
[root@cache01 git_test]# git add a.txt
[root@cache01 git_test]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b.txt
# c.txt
# d.txt
[root@cache01 git_test]# git add .
[root@cache01 git_test]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: a.txt
# new file: b.txt
# new file: c.txt
# new file: d.txt
#
2). git commit
# 提交暂存区数据到本地仓库
参数列表:
-m "<info>" 给提交的数据添加简短说明
[root@cache01 git_test]# git commit -m "commit some files"
[master (root-commit) c8a06f3] commit some files
4 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
create mode 100644 c.txt
create mode 100644 d.txt
[root@cache01 git_test]# git status
# On branch master
nothing to commit, working directory clean
3. 删除命令
git rm
# 删除已提交到暂存区的数据
参数列表:
--cached 删除暂存区数据
-f 强制删除暂存区和工作目录数据
[root@cache01 git_test]# git rm --cached b.txt
rm ‘b.txt‘
[root@cache01 git_test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: b.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b.txt
[root@cache01 git_test]# git rm -f c.txt
rm ‘c.txt‘
[root@cache01 git_test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: b.txt
# deleted: c.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b.txt
[root@cache01 git_test]# ll
total 8
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 4 Jun 20 18:58 b.txt
-rw-r--r-- 1 root root 0 Jun 20 18:36 d.txt
4. 修改命令
1). git mv
# 修改文件名并同步到暂存区
参数列表:
<old-filename> <new-filename> 要修改的旧文件名->目标文件名称
修改暂存区的文件名
[root@cache01 git_test]# git mv b.txt b1.txt
[root@cache01 git_test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: b.txt
# new file: b1.txt
#
[root@cache01 git_test]# git status
# On branch master
nothing to commit, working directory clean
[root@cache01 git_test]# git mv d.txt d1.txt
[root@cache01 git_test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: d.txt -> d1.txt
#
[root@cache01 git_test]# ll
total 8
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 4 Jun 20 18:58 b1.txt
-rw-r--r-- 1 root root 0 Jun 20 19:09 c.txt
-rw-r--r-- 1 root root 0 Jun 20 18:36 d1.txt
2). git checkout
# 检出文件或切换分支
参数列表:
-- <filename> 把暂存区的文件覆盖工作目录
[root@cache01 git_test]# git status
# On branch master
nothing to commit, working directory clean
[root@cache01 git_test]# ll
total 8
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 4 Jun 20 18:58 b1.txt
-rw-r--r-- 1 root root 0 Jun 20 19:16 c.txt
-rw-r--r-- 1 root root 0 Jun 20 19:16 d.txt
[root@cache01 git_test]# echo "ccc hello" >>c.txt
[root@cache01 git_test]# 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: c.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@cache01 git_test]# cat c.txt
ccc hello
[root@cache01 git_test]# git checkout -- c.txt
[root@cache01 git_test]# git status
# On branch master
nothing to commit, working directory clean
[root@cache01 git_test]# cat c.txt
[root@cache01 git_test]#
3). git reset
# 覆盖错误文件
参数列表:
HEAD <filename> 把本地仓库的文件覆盖到暂存区
--hard <filename> 恢复本地仓库到历史版本
a. 把本地仓库的文件覆盖到暂存区
[root@cache01 git_test]# git status
# On branch master
nothing to commit, working directory clean
[root@cache01 git_test]# ll
total 8
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 4 Jun 20 18:58 b1.txt
-rw-r--r-- 1 root root 0 Jun 20 19:17 c.txt
-rw-r--r-- 1 root root 0 Jun 20 19:16 d.txt
[root@cache01 git_test]# echo "ccc hello" >>c.txt
[root@cache01 git_test]# 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: c.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@cache01 git_test]# git add .
[root@cache01 git_test]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: c.txt
#
[root@cache01 git_test]# git reset HEAD c.txt
Unstaged changes after reset:
M c.txt
[root@cache01 git_test]# 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: c.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@cache01 git_test]# git checkout -- c.txt
[root@cache01 git_test]# git status
# On branch master
nothing to commit, working directory clean
b. 恢复本地仓库到历史版本
[root@cache01 git_test]# git reflog
cd494aa HEAD@{0}: commit: rename b1.txt v1.1
ce4c70b HEAD@{1}: commit: modified a.txt v1.1
d376e3a HEAD@{2}: commit: modified a.txt v1.0
c8a06f3 HEAD@{3}: commit (initial): commit some files
[root@cache01 git_test]# git reset --hard ce4c70b
HEAD is now at ce4c70b modified a.txt v1.1
[root@cache01 git_test]# git reflog
ce4c70b HEAD@{0}: reset: moving to ce4c70b
cd494aa HEAD@{1}: commit: rename b1.txt v1.1
ce4c70b HEAD@{2}: commit: modified a.txt v1.1
d376e3a HEAD@{3}: commit: modified a.txt v1.0
c8a06f3 HEAD@{4}: commit (initial): commit some files
[root@cache01 git_test]# ll
total 4
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 0 Jun 20 19:26 b.txt
-rw-r--r-- 1 root root 0 Jun 20 19:24 c.txt
-rw-r--r-- 1 root root 0 Jun 20 19:16 d.txt
[root@cache01 git_test]# git reset --hard cd494aa
HEAD is now at cd494aa rename b1.txt v1.1
[root@cache01 git_test]# ll
total 8
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 4 Jun 20 19:29 b1.txt
-rw-r--r-- 1 root root 0 Jun 20 19:24 c.txt
-rw-r--r-- 1 root root 0 Jun 20 19:16 d.txt
[root@cache01 git_test]#
七. GIT分支命令
在实际的项目开发中,尽量保证master分支稳定,仅用于发布新版本,平时不要随便直接修改里面的数据文件。干活都在dev分支上。每个人从dev分支创建自己个人分支,开发完合并到dev分支,最后dev分支合并到master分支。
1. 查看分支
①. git branch
# 查看分支
[root@cache01 git_test]# git branch
* master
②. git show
参数列表:
<tagname> 查看已打比标签的commit的信息
③. git tag
# 查看tag标签列表
2. 添加分支
①. git branch
# 添加分支
参数列表:
<branchname> 添加新的分支
[root@cache01 git_test]# git branch
* master
[root@cache01 git_test]# git branch dev
[root@cache01 git_test]# git branch
dev
* master
3. 修改分支
①. git checkout
# 检出文件或切换分支
[root@cache01 git_test]# git branch
dev
* master
[root@cache01 git_test]# git checkout dev
Switched to branch ‘dev‘
[root@cache01 git_test]# git branch
* dev
master
②. git merge
# 合并分支
语法: git merge xxx to xxx
如果想把linux分支的工作成果合并到master分支上,先切换到master分支
git merge linux #→合并Linux分支至master
[root@cache01 git_test]# git branch
dev
* master
[root@cache01 git_test]# ll
total 12
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 9 Jun 20 20:21 b.txt
-rw-r--r-- 1 root root 24 Jun 20 20:10 c.txt
[root@cache01 git_test]# git checkout linux
Switched to branch ‘linux‘
[root@cache01 git_test]# git branch
dev
* linux
master
[root@cache01 git_test]# git log --oneline --decorate
39e149a (HEAD, linux) modified b.txt v1.2
a76f986 (master) commit c.txt and modified c.txt
838c85f (dev) modified c.txt on branch dev
48e1eaa delete c.txt and d.txt
c461e21 delete b1.txt
b1f19e2 commit b.txt to branch dev
cd494aa rename b1.txt v1.1
ce4c70b modified a.txt v1.1
d376e3a modified a.txt v1.0
c8a06f3 commit some files
[root@cache01 git_test]# git checkout master
Switched to branch ‘master‘
[root@cache01 git_test]# git merge linux
Updating a76f986..39e149a
Fast-forward
b.txt | 1 +
1 file changed, 1 insertion(+)
[root@cache01 git_test]# ll
total 12
-rw-r--r-- 1 root root 27 Jun 20 18:53 a.txt
-rw-r--r-- 1 root root 26 Jun 20 20:22 b.txt
-rw-r--r-- 1 root root 24 Jun 20 20:10 c.txt
[root@cache01 git_test]# git log --oneline --decorate
39e149a (HEAD, master, linux) modified b.txt v1.2
a76f986 commit c.txt and modified c.txt
838c85f (dev) modified c.txt on branch dev
48e1eaa delete c.txt and d.txt
c461e21 delete b1.txt
b1f19e2 commit b.txt to branch dev
cd494aa rename b1.txt v1.1
ce4c70b modified a.txt v1.1
d376e3a modified a.txt v1.0
c8a06f3 commit some files
③. git tag
# 给分支打标签
[root@cache01 git_test]# git tag -a v1.0 a76f986
[root@cache01 git_test]# git show v1.0
tag v1.0
Tagger: learn git <962532419@qq.com>
Date: Sat Jun 20 20:34:15 2020 +0800
commit on master branch v1.0
commit a76f986f6468a94e3578127f67fd1b7efaf17e4c
Merge: 48e1eaa 838c85f
Author: learn git <962532419@qq.com>
Date: Sat Jun 20 20:10:37 2020 +0800
commit c.txt and modified c.txt
diff --cc c.txt
index 0000000,3b18e51..10bda51
mode 000000,100644..100644
--- a/c.txt
+++ b/c.txt
@@@ -1,0 -1,1 +1,2 @@@
+ hello world
++hello world
4. 删除分支
①. git branch
参数列表:
-d <branchname> 删除指定的分支
[root@cache01 git_test]# git branch -d dev
Deleted branch dev (was 838c85f).
[root@cache01 git_test]# git branch
linux
* master
②. git tag
参数列表:
-d <num> 删除已打的标签
[root@cache01 git_test]# git tag
v1.0
[root@cache01 git_test]# git log --oneline --decorate
39e149a (HEAD, master, linux) modified b.txt v1.2
a76f986 (tag: v1.0) commit c.txt and modified c.txt
838c85f modified c.txt on branch dev
48e1eaa delete c.txt and d.txt
c461e21 delete b1.txt
b1f19e2 commit b.txt to branch dev
cd494aa rename b1.txt v1.1
ce4c70b modified a.txt v1.1
d376e3a modified a.txt v1.0
c8a06f3 commit some files
[root@cache01 git_test]# git tag -d v1.0
Deleted tag ‘v1.0‘ (was 233781b)
[root@cache01 git_test]# git tag
[root@cache01 git_test]# git log --oneline --decorate
39e149a (HEAD, master, linux) modified b.txt v1.2
a76f986 commit c.txt and modified c.txt
838c85f modified c.txt on branch dev
48e1eaa delete c.txt and d.txt
c461e21 delete b1.txt
b1f19e2 commit b.txt to branch dev
cd494aa rename b1.txt v1.1
ce4c70b modified a.txt v1.1
d376e3a modified a.txt v1.0
c8a06f3 commit some files
以上是关于版本控制系统-Git的主要内容,如果未能解决你的问题,请参考以下文章