版本控制工具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