版本控制工具Git工具快速入门-Linux篇
版本控制工具Git工具快速入门-Linux篇
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.版本管理系统的介绍
1>.版本管理系统的特点
1.1>.自动生成备份:
在同一个目录中,如果我们想要备份一个文件,可能就拷贝一份,然后修改拷贝后的版本,当你对该文件修改的次数超过一定频繁时,你会发现当前目录的文件数特别的多,当然如果你每次修改的数据量较大或者源文件本身就特别大,你会也导致当前目录大小过大!而版本管理会帮你把这两个烦恼解决掉。他不会在当前目录做备份,而是在一个隐藏目录做的备份,等你需要的时候他会帮你调出来,当你不需要它时他就会在隐藏目录中安静的带着。
1.2>.随时回滚
当我们在修改一个文件内容时,如果我们忘记修改了那些地方,我们得用之前备份的文件进行比对,然后自己去一一对比,找出其中的差异,然后复原我们想要的数据内容。而版本管理可以显示帮我们颜色高亮显示出当前文件和之前的备份的文件差异。也就是第1.3的特点,知道改动的地方。
1.3>.知道改动的地方
2>.常见版本管理工具
2.1>.SVN
集中式的版本控制系统,只有一个中央数据仓库,如果中央数据仓库挂了或者访问不可访问,所有的使用者无法使用SVN,无法进行提交或备份文件。如果你想部署SVN的话,可以参考我之前分享的笔记:https://www.cnblogs.com/yinzhengjie/p/6154664.html。
2.2>.Git
分布式的版本控制系统,在每个使用者电脑上就有一个完整的数据仓库,没有网络依然可以使用Git。当然为了习惯及团队协作,会将本地数据同步到Git服务器或者GitHub等代码仓库。
2.3>.SVN和Git的区别
第一: 最核心的区别Git是分布式的,而Svn不是分布的。Git跟Svn一样有自己的集中式版本库和Server端,但Git更倾向于分布式开发。 git是分布式的scm,svn是集中式的。(最核心)
第二:Git把内容按元数据方式存储,而SVN是按文件:因为,.git目录是处于你的机器上的一个克隆版的版本库,它拥有中心版本库上所有的东西,例如标签,分支,版本记录等。.git目录的体积大小跟.svn比较,你会发现它们差距很大。
第三:git可离线完成大部分操作,svn则不能。
第四:git有着更优雅的分支和合并实现,Git创建分支要比SVN快的多得多。
第五:git有着更强的撤销修改和修改历史版本的能力。
第六:git速度更快,效率更高。
二.Git安装配置
1>.安装git环境准备
[root@yinzhengjie ~]# systemctl stop firewalld #临时关闭防火墙 [root@yinzhengjie ~]# systemctl disable firewalld #禁用防火墙的服务开机自启 [root@yinzhengjie ~]# [root@yinzhengjie ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@yinzhengjie ~]# [root@yinzhengjie ~]# getenforce #查看当前selinux的安全认证模式 Enforcing [root@yinzhengjie ~]# [root@yinzhengjie ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config #查看配置 [root@yinzhengjie ~]# [root@yinzhengjie ~]# cat /etc/selinux/config | grep SELINUX= | grep -v ^# SELINUX=disabled [root@yinzhengjie ~]#reboot #重启机器 [root@yinzhengjie ~]# [root@yinzhengjie ~]# getenforce Disabled [root@yinzhengjie ~]#
2>.安装git
[root@yinzhengjie ~]# rpm -qa git #查看是否安装git git-1.8.3.1-14.el7_5.x86_64 [root@yinzhengjie ~]# [root@yinzhengjie ~]# yum -y install git #如果上面检查没有输出,说明没有安装,我们执行这条命令安装即可。 Loaded plugins: fastestmirror base | 3.6 kB 00:00:00 elasticsearch-2.x | 2.9 kB 00:00:00 extras | 3.4 kB 00:00:00 mysql-connectors-community | 2.5 kB 00:00:00 mysql-tools-community | 2.5 kB 00:00:00 mysql56-community | 2.5 kB 00:00:00 updates | 3.4 kB 00:00:00 Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.com Package git-1.8.3.1-14.el7_5.x86_64 already installed and latest version Nothing to do [root@yinzhengjie ~]#
3>.Git安装配置
[root@yinzhengjie ~]# git config --global user.name "yinzhengjie" #提交git用户信息,即配置使用者git的用户 [root@yinzhengjie ~]# [root@yinzhengjie ~]# git config --global user.email "y1053419035@qq.com" #提交邮箱,即配置git使用的邮箱 [root@yinzhengjie ~]# [root@yinzhengjie ~]# git config --global color.ui true #显示语法高亮 [root@yinzhengjie ~]# [root@yinzhengjie ~]# git config --list #查看全局配置信息 user.name=yinzhengjie user.email=y1053419035@qq.com color.ui=true [root@yinzhengjie ~]#
三.git的基本使用
1>.初始化Git工作目录
[root@yinzhengjie ~]# mkdir git_data [root@yinzhengjie ~]# cd git_data/ [root@yinzhengjie git_data]# git init #初始化git的工作目录,会生成一个".git"的隐藏目录 Initialized empty Git repository in /root/git_data/.git/ [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# ll -a #查看当前目录是否有".git"隐藏目录 total 4 drwxr-xr-x 3 root root 17 Sep 7 18:15 . dr-xr-x---. 8 root root 4096 Sep 7 18:15 .. drwxr-xr-x 7 root root 111 Sep 7 18:15 .git [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# ll .git/ #存放用户git的配置文件,勿动! total 16 drwxr-xr-x 2 root root 6 Sep 7 18:15 branches -rw-r--r-- 1 root root 92 Sep 7 18:15 config -rw-r--r-- 1 root root 73 Sep 7 18:15 description -rw-r--r-- 1 root root 23 Sep 7 18:15 HEAD drwxr-xr-x 2 root root 4096 Sep 7 18:15 hooks drwxr-xr-x 2 root root 20 Sep 7 18:15 info drwxr-xr-x 4 root root 28 Sep 7 18:15 objects drwxr-xr-x 4 root root 29 Sep 7 18:15 refs [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# yum -y install tree Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.huaweicloud.com Resolving Dependencies --> Running transaction check ---> Package tree.x86_64 0:1.6.0-10.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================= Package Arch Version Repository Size ============================================================================================================================================= Installing: tree x86_64 1.6.0-10.el7 base 46 k Transaction Summary ============================================================================================================================================= Install 1 Package Total download size: 46 k Installed size: 87 k Downloading packages: tree-1.6.0-10.el7.x86_64.rpm | 46 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : tree-1.6.0-10.el7.x86_64 1/1 Verifying : tree-1.6.0-10.el7.x86_64 1/1 Installed: tree.x86_64 0:1.6.0-10.el7 Complete! [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# tree .git/ .git/ ├── branches ├── COMMIT_EDITMSG ├── 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 ├── index ├── info │ └── exclude ├── logs │ ├── HEAD │ └── refs │ └── heads │ └── master ├── objects │ ├── 54 │ │ └── 3b9bebdc6bd5c4b22136034a95dd097a57d3dd │ ├── 71 │ │ └── e18cd26c0a200069d487038a681bd68cc0bec6 │ ├── e6 │ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 │ ├── info │ └── pack └── refs ├── heads │ └── master └── tags 15 directories, 21 files [root@yinzhengjie git_data]#
2>.查看工作区状态
[root@yinzhengjie git_data]# git status #查看当前git的状态 # On branch master #这个表示的是当前所在分支,这里是在主分支(master) # # Initial commit #最初的提交 # nothing to commit (create/copy files and use "git add" to track) #这里提示信息说没有任何提交 [root@yinzhengjie git_data]#
3>.提交数据到git暂存区域
[root@yinzhengjie git_data]# git status #查看git状态 # On branch master # # Initial commit #这里提示没有任何提交 # nothing to commit (create/copy files and use "git add" to track) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# touch README #在工作目录创建一个README文件 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status #再次查看git状态 # On branch master # # Initial commit # # Untracked files: #这里告诉咱们以下有1个文件“README”没有被跟踪。 # (use "git add <file>..." to include in what will be committed) # # README nothing added to commit but untracked files present (use "git add" to track) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git add README #提交到暂存区域 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status #又一次查看git状态 # On branch master # # Initial commit # # Changes to be committed: #这里告诉咱们已经把文件下面的文件给跟踪了 # (use "git rm --cached <file>..." to unstage) # # new file: README # [root@yinzhengjie git_data]#
4>.将暂存区文件提交Git仓库
[root@yinzhengjie git_data]# git status #查看当前git的状态,注意输出信息哟 # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: README # [root@yinzhengjie git_data]# git commit -m 'commit readme file !' #将文件从stage区(暂存区)提交Git仓库(或者是Git数据库), [master (root-commit) 71e18cd] commit readme file ! 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status #再次查看当前git的状态,注意输出信息哟 # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 8 2018 README -rw-r--r-- 1 root root 0 Sep 7 22:21 testfile [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# echo "http://www.cnblogs.com/yinzhengjie" >> README [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# 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 # no changes added to commit (use "git add" and/or "git commit -a") [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git commit -a -m "modified: README file" #将文件提交到git的暂存区后立刻提交到git仓库 [master fdae5a6] modified: README file 1 file changed, 1 insertion(+) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]#
5>.删除暂存区数据
[root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 7 18:24 README [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# touch test [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test nothing added to commit but untracked files present (use "git add" to track) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# rm -rf test [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git status #查看当前git的状态 # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 7 18:24 README [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# touch test [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test nothing added to commit but untracked files present (use "git add" to track) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git add * #将当期目录的所有文件都添加到暂存区 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status #查看当前git的状态 # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: test # [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git rm --cache test #将文件从git暂存区域的追踪列表移除(并不会删除当前工作目录内的数据文件) rm 'test' [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status #查看当前git的状态 # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test nothing added to commit but untracked files present (use "git add" to track) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 7 18:24 README -rw-r--r-- 1 root root 0 Sep 7 18:43 test [root@yinzhengjie git_data]# [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 7 18:24 README -rw-r--r-- 1 root root 0 Sep 7 18:43 test [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test nothing added to commit but untracked files present (use "git add" to track) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git add * [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git rm -f test #将文件数据从git暂存区和工作目录一起删除 rm 'test' [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 7 18:24 README [root@yinzhengjie git_data]#
6>.重命名暂存区数据
[root@yinzhengjie git_data]# touch testfile [root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 8 2018 README -rw-r--r-- 1 root root 0 Sep 7 22:21 testfile [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # testfile nothing added to commit but untracked files present (use "git add" to track) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# mv testfile test [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 8 2018 README -rw-r--r-- 1 root root 0 Sep 7 22:21 test [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test nothing added to commit but untracked files present (use "git add" to track) [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 8 2018 README -rw-r--r-- 1 root root 0 Sep 7 22:21 test [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git add * [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: test # [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git mv test testfile [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# ll total 0 -rw-r--r-- 1 root root 0 Sep 8 2018 README -rw-r--r-- 1 root root 0 Sep 7 22:21 testfile [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: testfile # [root@yinzhengjie git_data]#
7>.查看历史记录
[root@yinzhengjie git_data]# git log #查看提交历史记录,注意,在上面的信息记录的是最近提交的版本 commit 71e18cd26c0a200069d487038a681bd68cc0bec6 #这个长度为40的字符串是对每次提交做一个标记,你可以说它是当前版本的唯一标识。 Author: yinzhengjie <y1053419035@qq.com> #这里是记录谁提交的 Date: Fri Sep 7 18:32:27 2018 -0400 #这里记录的是提交时的时间 modified: README file #这里标识意思是修改后的版本,同理下面的输出信息也一致。 commit e86eded9de3475acb3a8bf2e1b66ce3bdf668287 Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 22:31:17 2018 +0800 first add testfile commit 71e18cd26c0a200069d487038a681bd68cc0bec6 Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 18:32:27 2018 -0400 commit readme file ! [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git log -2 #只查看最近提交的2条记录 commit fdae5a65a8afd50eec0a705ea9510f49a75299fd Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 22:35:53 2018 +0800 modified: README file commit e86eded9de3475acb3a8bf2e1b66ce3bdf668287 Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 22:31:17 2018 +0800 first add testfile [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git log -p -1 #“-p”表示显示每次提交的内容差异,例如我这里是仅查看最近一次差异 commit fdae5a65a8afd50eec0a705ea9510f49a75299fd Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 22:35:53 2018 +0800 modified: README file diff --git a/README b/README index e69de29..8fd20b7 100644 --- a/README +++ b/README @@ -0,0 +1 @@ +http://www.cnblogs.com/yinzhengjie [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git log --stat -2 #“--stat”简要显示每次提交的内容差异,例如仅查看最近一次差异 commit fdae5a65a8afd50eec0a705ea9510f49a75299fd Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 22:35:53 2018 +0800 modified: README file README | 1 + 1 file changed, 1 insertion(+) commit e86eded9de3475acb3a8bf2e1b66ce3bdf668287 Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 22:31:17 2018 +0800 first add testfile testfile | 0 1 file changed, 0 insertions(+), 0 deletions(-) [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git log --pretty=oneline #“--pretty”根据不同的格式展示提交的历史信息 fdae5a65a8afd50eec0a705ea9510f49a75299fd modified: README file e86eded9de3475acb3a8bf2e1b66ce3bdf668287 first add testfile 71e18cd26c0a200069d487038a681bd68cc0bec6 commit readme file ! [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git log --pretty=fuller -2 #以更详细的模式输出提交的历史记录 commit fdae5a65a8afd50eec0a705ea9510f49a75299fd Author: yinzhengjie <y1053419035@qq.com> AuthorDate: Fri Sep 7 22:35:53 2018 +0800 Commit: yinzhengjie <y1053419035@qq.com> CommitDate: Fri Sep 7 22:35:53 2018 +0800 modified: README file commit e86eded9de3475acb3a8bf2e1b66ce3bdf668287 Author: yinzhengjie <y1053419035@qq.com> AuthorDate: Fri Sep 7 22:31:17 2018 +0800 Commit: yinzhengjie <y1053419035@qq.com> CommitDate: Fri Sep 7 22:31:17 2018 +0800 first add testfile [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git log --pretty=format:"%h %cn" #查看当前所有提交记录的简短SHA-1哈希资产与提交的姓名,其它格式见温馨提示。 fdae5a6 yinzhengjie e86eded yinzhengjie 71e18cd yinzhengjie [root@yinzhengjie git_data]# 温馨提示: 还可以使用format参数来指定具体的输出格式,这样非常便于后期编程的提取分析哟,常用的格式有: %s :提交说明。 %cd :提交日期 %an :作者的名字。 %cn :提交者的姓名。 %ce :提交者的电子邮件。 %H :提交对象的完整SHA-1哈希字串 %h :提交对象的简短SHA-1哈希字串 %T :树对象的完整SHA-1哈希字串 %t :树对象的简短SHA-1哈希字串 %P :父对象的完整SHA-1哈希字串 %p :父对象的简短SHA-1哈希字串 %ad :作者的修订时间。
[root@yinzhengjie git_data]# git log --pretty=oneline 71e18cd26c0a200069d487038a681bd68cc0bec6 commit readme file ! [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git reflog #查看未来历史更新点 71e18cd HEAD@{0}: reset: moving to 71e18cd26c0a200069d487038a681bd68cc0bec6 e86eded HEAD@{1}: reset: moving to HEAD^ fdae5a6 HEAD@{2}: commit: modified: README file e86eded HEAD@{3}: commit: first add testfile 71e18cd HEAD@{4}: commit (initial): commit readme file ! [root@yinzhengjie git_data]#
8>.还原历史数据
Git程序中有个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为GIT是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈希字串来区分每一个提交版本。另外默认的HEAD版本指针则会叫做HEAD^^,当然一般会用HEAD~5来表示往上数第五个提交版本。
[root@yinzhengjie git_data]# git log --pretty=oneline fdae5a65a8afd50eec0a705ea9510f49a75299fd modified: README file e86eded9de3475acb3a8bf2e1b66ce3bdf668287 first add testfile 71e18cd26c0a200069d487038a681bd68cc0bec6 commit readme file ! [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git reset --hard HEAD^ #还原历史提交版本上一次,如果想要还原距离当前版本最近的第三个提交版本,可以使用“HEAD^^^”,当然你可也可以使用“HEAD~3” HEAD is now at e86eded first add testfile [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log --pretty=oneline e86eded9de3475acb3a8bf2e1b66ce3bdf668287 first add testfile 71e18cd26c0a200069d487038a681bd68cc0bec6 commit readme file ! [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git log --pretty=oneline e86eded9de3475acb3a8bf2e1b66ce3bdf668287 first add testfile 71e18cd26c0a200069d487038a681bd68cc0bec6 commit readme file ! [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git reset --hard 71e18cd26c0a200069d487038a681bd68cc0bec6 #知道历史还原点的SHA-1值后,就可以还原(注意,如果你值没有写全,系统会自动匹配) HEAD is now at 71e18cd commit readme file ! [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log --pretty=oneline 71e18cd26c0a200069d487038a681bd68cc0bec6 commit readme file ! [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git log --pretty=oneline 71e18cd26c0a200069d487038a681bd68cc0bec6 commit readme file ! [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git reflog 71e18cd HEAD@{0}: reset: moving to 71e18cd26c0a200069d487038a681bd68cc0bec6 e86eded HEAD@{1}: reset: moving to HEAD^ fdae5a6 HEAD@{2}: commit: modified: README file e86eded HEAD@{3}: commit: first add testfile 71e18cd HEAD@{4}: commit (initial): commit readme file ! [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git reset --hard e86eded #通过“git reflog”找到对应的版本后,进行还原操作 HEAD is now at e86eded first add testfile [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log --pretty=oneline e86eded9de3475acb3a8bf2e1b66ce3bdf668287 first add testfile 71e18cd26c0a200069d487038a681bd68cc0bec6 commit readme file ! [root@yinzhengjie git_data]# [root@yinzhengjie git_data]#
9>.标签的使用
前面回滚使用的是一串字符串,又长有难记。为了方便回滚,不用记录一大串字符串我们可以打个标记,回滚的时候使用我们定义的tag标记即可。
[root@yinzhengjie git_data]# git tag v1.0 #给当前提交内容答应标签(方便快速回滚),每次提交都可以打个tag [root@yinzhengjie git_data]# git tag #查看当前所有的标签 v1.0 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git show v1.0 #查看当前v1.0版本的详细信息 commit e86eded9de3475acb3a8bf2e1b66ce3bdf668287 Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 22:31:17 2018 +0800 first add testfile diff --git a/testfile b/testfile new file mode 100644 index 0000000..e69de29 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git tag v1.1 -m "version 1.1 release " #创建带有说明的标签,v1.1表示的是标签名字,-m指定文字说明 [root@yinzhengjie git_data]# git tag v1.0 v1.1 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git tag -d v1.0 #我们为他同一个提交版本设置了两次标签,删除之前的v1.0版本 Deleted tag 'v1.0' (was e86eded) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git tag v1.1 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git show v1.1 tag v1.1 Tagger: yinzhengjie <y1053419035@qq.com> Date: Sat Sep 8 21:12:18 2018 +0800 version 1.1 release commit e86eded9de3475acb3a8bf2e1b66ce3bdf668287 Author: yinzhengjie <y1053419035@qq.com> Date: Fri Sep 7 22:31:17 2018 +0800 first add testfile diff --git a/testfile b/testfile new file mode 100644 index 0000000..e69de29 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git reset --hard v1.1 #利用标签执行回滚操作 HEAD is now at e86eded first add testfile [root@yinzhengjie git_data]#
10>.对比数据
[root@yinzhengjie git_data]# git diff README #如果没有输出,说明README这个文件没有被修改,git diff 可以对比当前文件与仓库已保存文件的区别,知道了对README做了什么修改后,再把它提交到仓库就放心多了 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# echo "尹正杰到此一游" >> README #此时我们修改还README文件 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git diff README #这次有输出提示了,提示很明显 diff --git a/README b/README index e69de29..4447a1b 100644 --- a/README +++ b/README @@ -0,0 +1 @@ +尹正杰到此一游 #注意,这个“+”表示新增的行,后面是新增的内容 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# 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 # no changes added to commit (use "git add" and/or "git commit -a") [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git add README [root@yinzhengjie git_data]# git commit -m "modified: README file" #此处我们将修改后的内容提交 [master ad103fa] modified: README file file changed, 1 insertion(+) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README 尹正杰到此一游 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# > README #这里我们将提交后的内容清空 [root@yinzhengjie git_data]# cat README [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git diff README #你会发现有内容输出 diff --git a/README b/README index 4447a1b..e69de29 100644 --- a/README +++ b/README @@ -1 +0,0 @@ -尹正杰到此一游 #注意,这里的“-”表示删除的意思,即这行内容已经被删除了 [root@yinzhengjie git_data]#
11>.分支结构管理
在实际的项目开发中,尽量保证master分支稳定,仅用于发布新版本,平时不要随便直接修改里面的数据文件。程序员写完代码都会提交到远程仓库,那么远程仓库都是主库(master)吗?很显然,答案是否定的,真正的答案是程序员把每天写的代码的进度都提交到了分支上去了,我在的上家公司是做金融的,我的工作之一就是将开发写的代码通过Jenkins等自动化工具将代码进行打包,然后在交个测试之后,才会把软件包发给技术支持。我依稀的记得当时开发每天会向不同的分支提交代码,我们一天甚至会接到30多次的部署环境的任务.......好了,都过去了,我们说会正题,什么是分支管理?它有什么作用呢?
我们想一个问题,每个部门都会负责不同的功能,比如处理风控的小组就负责风控的开发和优化,负责前端的程序员就负责写好前端,而且每个小组的每一个程序员负责的具体人物也是不同的,一个软件公司上百人开发一个软件这也是在正常不过的事情了,他们协同开发一个软件,每天都需要敲代码,如果每个人每天提交代码的时候都放在一个库里面,那么很有可能就因为某个程序员提交的代码导致整个程序崩溃了,以至于其他部门的同事没法正常工作了,所以,为了解决这个问题,我们需要采取分支管理。具体的工作流程我们可以参考下图(用系统自带的软件画的,我承认是该学习一下美术了):
[root@yinzhengjie git_data]# git branch #查看当前所在的分支 * master #很显然,目前只有一个分支,只有一个master分支 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch yinzhengjie #创建一个名称为yinzhengjie的分支 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch #查看当前所在的分支,我们会发现有新的分支被列举出来了,但是我们依然是在master分支上 * master yinzhengjie [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git branch #查看当前所在分支,很显然,这个“*”好在master分支上,因此我们目前还是在master分支上 * master yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git checkout yinzhengjie #我们切换分支到yinzhengjie上, M README #这个提示表示我们对README文件由修改操作,但是我们并没有提交他 Switched to branch 'yinzhengjie' #这里是提示切换到'yinzhengjie'这个分支了 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch #查看当前所在分支,很显然,这个“*”好在yinzhengjie分支上,因此我们成功从master分支切换到yinengjie分支啦! master * yinzhengjie [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git branch #查看当前所在分支 master * yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git checkout master #切换分支到“master” M README #这里的提示说明有个README文件被修改了但是并没有被提交 Switched to branch 'master' #这行输出内容提示我们已经切换到master分支了 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch #查看当前所在分支,确认是否切换成功 * master yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status #查看当前git状态,发现的确有个文件被修改了,并没有被提交 # 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 # no changes added to commit (use "git add" and/or "git commit -a") [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README #查看README文件内容,发现里面的内容已经被清空了 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git checkout -- README #我们可以通过"git checkout -- <file>..."来还原文件,即让他回到修改之前的样子。 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README 尹正杰到此一游 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status #查看当前git状态,发现一切正常 # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git status # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch * master yinzhengjie [root@yinzhengjie git_data]# git checkout yinzhengjie Switched to branch 'yinzhengjie' [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch master * yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README 尹正杰到此一游 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# echo yinzhengjie >> README [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch yinzhengjie # 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 # no changes added to commit (use "git add" and/or "git commit -a") [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git commit -a -m "this is yinzhengjie branch commit" #往yinzhengjie分支提交新的改动内容 [yinzhengjie ccff8fb] this is yinzhengjie branch commit file changed, 1 insertion(+) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch yinzhengjie nothing to commit, working directory clean [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log -1 #查看yinzhengjie这个分支的log日志 commit ccff8fb272aaf807e92ab0d89e6bf1a98b9afcd2 Author: yinzhengjie <y1053419035@qq.com> Date: Sat Sep 8 22:04:21 2018 +0800 this is yinzhengjie branch commit [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch master * yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git checkout master Switched to branch 'master' [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch * master yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log -1 #查看master这个分支的log日志,你会发现和master并不一致! commit ad103fa6f72aaa154f14a96cfa0570032d434d27 Author: yinzhengjie <y1053419035@qq.com> Date: Sat Sep 8 21:27:14 2018 +0800 modified: README file [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git branch #你想要合并的,必须切换到master分支 * master yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log -1 commit ad103fa6f72aaa154f14a96cfa0570032d434d27 Author: yinzhengjie <y1053419035@qq.com> Date: Sat Sep 8 21:27:14 2018 +0800 modified: README file [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README #合并之前,查看文件内容 尹正杰到此一游 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git merge yinzhengjie #在主分支中合并yinzhengjie这个分支,他会自动合并分支,合并成功后建议删除之前被合并的分支,如何想要在合并后的分支进行进行开发再创建一个就好哟! Updating ad103fa..ccff8fb Fast-forward README | 1 + 1 file changed, 1 insertion(+) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log -1 #查看master当前提交版本是:ccff8fb272aaf807e92ab0d89e6bf1a98b9afcd2 commit ccff8fb272aaf807e92ab0d89e6bf1a98b9afcd2 Author: yinzhengjie <y1053419035@qq.com> Date: Sat Sep 8 22:04:21 2018 +0800 this is yinzhengjie branch commit [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README 尹正杰到此一游 yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git checkout yinzhengjie #切换到yinzhengjie分支 Switched to branch 'yinzhengjie' [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log -1 #查看master当前提交版本是 commit ccff8fb272aaf807e92ab0d89e6bf1a98b9afcd2 Author: yinzhengjie <y1053419035@qq.com> Date: Sat Sep 8 22:04:21 2018 +0800 this is yinzhengjie branch commit [root@yinzhengjie git_data]# [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# git branch #查看当前所有的分支 master * yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git checkout master #切换到主分支 Switched to branch 'master' [root@yinzhengjie git_data]# git branch -d yinzhengjie #确认合并完成后,可以放心的删除yinzhengjie这个分支了 Deleted branch yinzhengjie (was ccff8fb). [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch #再次查看当前所有的分支 * master [root@yinzhengjie git_data]#
[root@yinzhengjie git_data]# ll total 4 -rw-r--r-- 1 root root 34 Sep 8 22:13 README -rw-r--r-- 1 root root 0 Sep 7 23:25 testfile [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README 尹正杰到此一游 yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch yinzhengjie #创建新的分支 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch * master yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# echo master >> README #修改当前分支内容 [root@yinzhengjie git_data]# cat README 尹正杰到此一游 yinzhengjie master [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git commit -a -m "this master commit" #在master分支提交已经修改的README文件 [master ac996e7] this master commit 1 file changed, 1 insertion(+) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git status # On branch master nothing to commit, working directory clean [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log -1 commit ac996e7d3c66e0f4e14fc3febc6d69496dd66574 Author: yinzhengjie <y1053419035@qq.com> Date: Sat Sep 8 22:46:22 2018 +0800 this master commit [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git checkout yinzhengjie Switched to branch 'yinzhengjie' [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git branch master * yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# echo yinzhengjie-branch >> README [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README 尹正杰到此一游 yinzhengjie yinzhengjie-branch [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git commit -a -m "this is yinzhengjie commit" #在yinzhengjie分支提交已经修改的README文件 [yinzhengjie b9048b4] this is yinzhengjie commit 1 file changed, 1 insertion(+) [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git log -1 commit b9048b40b04ffaa9cb3c63cd75f0781aaeba4fe9 Author: yinzhengjie <y1053419035@qq.com> Date: Sat Sep 8 22:53:19 2018 +0800 this is yinzhengjie commit [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git checkout master Switched to branch 'master' [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git merge yinzhengjie #在主分支合并yinzhengjie这个分支,发现失败了 Auto-merging README #自动合并README文件 CONFLICT (content): Merge conflict in README #在合并README文件时冲突 Automatic merge failed; fix conflicts and then commit the result. #自动合并失败,需要你修复这个冲突再提交 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README #此时我们应该查看README文件,发现它吧两个分支的内容写到一起了 尹正杰到此一游 yinzhengjie <<<<<<< HEAD #发现没有?从当前行到下面的三行,是master和yinzhengjie分支的最新版本的内容,你需要手动修改它 master ======= yinzhengjie-branch >>>>>>> yinzhengjie [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# vi README #编辑README文件,对其进行修改,人工选择想要保留的信息 [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# cat README #手动修改结果如下 尹正杰到此一游 yinzhengjie yinzhengjie-branch [root@yinzhengjie git_data]# [root@yinzhengjie git_data]# git commit -a -m "this is master merge commit" #修改完毕后需要往git数据仓库提交 [master 6dcd743] this is master merge commit [root@yinzhengjie git_data]#
12>.GitHub的基本使用
前面我们已经知道git人人都是中心,那么他们怎么交互数据呢?有两种方式:第一,使用GitHub或者码云等公共代码仓库;第二,使用GitLib私有仓库。
详情请参考:https://www.cnblogs.com/yinzhengjie/p/7667388.html
13>.版本控制工具Git工具快速入门-Windows篇
详情请参考:https://www.cnblogs.com/yinzhengjie/p/7212136.html
14>.私有仓库GitLab快速入门篇
详情请参考:https://www.cnblogs.com/yinzhengjie/p/9568657.html
15>.使用pycharm开发代码上传到GitLab和GitHub
详情请参考:https://www.cnblogs.com/yinzhengjie/p/9571238.html
16>.Jenkins部署实战篇
详情请参考:https://www.cnblogs.com/yinzhengjie/p/9581653.html