Git詳解與gitlab使用
第1章 版本控制系統都能幹什麽?
自動生成備份
知道改動的地方
隨時回滾
第2章 常見的版本控制系統
2.1 git
是一個分布式版本控制系統,在每個使用者的電腦上有一個完整的數據倉庫,沒有網絡依然可以使用git,當然為了習慣及團隊協作,會將本地數據同步到git服務器或者github等代碼倉庫
常見版本管理工具:
2.2 SVN
集中式的版本控制系統,只有一個中央數據倉庫,如果中央數據倉庫掛了或者不可訪問,所有的使用者無法使用SVN,無法提交或者備份文件
第3章 環境準備
3.1 git默認最小化安裝的時候就已經有了
[root@gitlab ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@gitlab ~]# rpm -qa git
git-1.8.3.1-5.el7.x86_64
[root@gitlab ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
[root@gitlab ~]# getenforce
Disabled
3.2 Git全局配置:
git config --global user.name "dy"
git config --global user.email "[email protected]"
git config --global color.ui true
git config –list
第4章 git常用命令
4.1 git命令常用參數匯總:
命令 | 命令說明 |
add | 添加文件內容至索引 |
bisect | 通過二分查找定位引入 bug 的變更 |
branch | 列出、創建或刪除分支 |
checkout | 檢出一個分支或路徑到工作區 |
clone | 克隆一個版本庫到一個新目錄 |
commit | 記錄變更到版本庫 |
diff | 顯示提交之間、提交和工作區之間等的差異 |
fetch | 從另外一個版本庫下載對象和引用 |
grep | 輸出和模式匹配的行 |
init | 創建一個空的 |
Git | 版本庫或重新初始化一個已存在的版本庫 |
log | 顯示提交日誌 |
merge | 合並兩個或更多開發歷史 |
mv | 移動或重命名一個文件、目錄或符號鏈接 |
pull | 獲取並合並另外的版本庫或一個本地分支 |
push | 更新遠程引用和相關的對象 |
rebase | 本地提交轉移至更新後的上遊分支中 |
reset | 重置當前HEAD到指定狀態 |
rm | 從工作區和索引中刪除文件 |
show | 顯示各種類型的對象 |
status | 顯示工作區狀態 |
tag | 創建、列出、刪除或校驗一個GPG簽名的 tag 對象 |
4.2 常規操作示意圖:
1.1 創建git數據倉庫
[root@gitlab ~]# mkdir git_data
[root@gitlab ~]# ll
total 4
-rw-------. 1 root root 1347 Mar 12 11:09 anaconda-ks.cfg
drwxr-xr-x 2 root root 6 Mar 20 20:40 git_data
1.2 初始化git目錄
[root@gitlab git_data]# git init
Initialized empty Git repository in /root/git_data/.git/
1.3 查看git當前工作狀態
[root@gitlab git_data]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
1.4 創建數據-提交數據
[root@gitlab git_data]# touch README
[root@gitlab git_data]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 20 20:47 README
[root@gitlab git_data]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (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)
1.5 把文件上傳的暫存區
[root@gitlab git_data]# git add README
[root@gitlab git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: README
1.6 把暫存區的文件提交的版本庫,-m是對文件的說明信息
[root@gitlab git_data]# git commit -m 'jiangboyang'
[master (root-commit) bb1dc0b] jiangboyang
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README
1.7 如果修改了已經上傳到倉庫的文件後,可以一步上傳到倉庫
說明:這裏的一步上傳到倉庫是指已經存在倉庫中的文件,修改後,支持這樣的操作
[root@gitlab git_data]# echo daya >>test
[root@gitlab 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: test
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gitlab git_data]# git commit -a -m "xiugaiwenjian"
[master 3bd1ed4] xiugaiwenjian
1 file changed, 1 insertion(+)
[root@gitlab git_data]# git status
# On branch master
nothing to commit, working directory clean
1.8 刪除暫存區的數據
[root@gitlab git_data]# git reset HEAD test2
[root@gitlab git_data]# git rm --cached test
rm 'test'
[root@gitlab git_data]# git rm -f test2 刪除暫存區文件並且刪除源文件
rm 'test2'
[root@gitlab git_data]# ll
total 4
-rw-r--r-- 1 root root 5 Mar 20 20:16 test
1.9 重命名暫存區文件
[root@gitlab git_data]# git mv test test.txt
[root@gitlab git_data]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 20 20:47 README
-rw-r--r-- 1 root root 0 Mar 20 21:01 test.txt
[root@gitlab git_data]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: test.txt
1.10 查看歷史記錄
查看歷史提交記錄
[root@gitlab git_data]# git log
commit bb1dc0bd080f1c2de3fc42a1d3e4a6e16d716422
Author: dy <[email protected]>
Date: Tue Mar 20 20:51:34 2018 +0800
jiangboyang
1.11 查看最近幾條提交記錄
[root@gitlab git_data]# git log -3
commit bb1dc0bd080f1c2de3fc42a1d3e4a6e16d716422
Author: dy <[email protected]>
Date: Tue Mar 20 20:51:34 2018 +0800
jiangboyang
git log #→查看提交歷史記錄
git log -2 #→查看最近幾條記錄
git log -p -1 #→-p顯示每次提交的內容差異,例如僅查看最近一次差異
git log --stat -2 #→--stat簡要顯示數據增改行數,這樣能夠看到提交中修改過的內容,對文件添加或移動的行數,並在最後列出所有增減行的概要信息
git log --pretty=oneline #→--pretty根據不同的格式展示提交的歷史信息
git log --pretty=fuller -2 #→以更詳細的模式輸出提交的歷史記錄
git log --pretty=fomat:"%h %cn" #→查看當前所有提交記錄的簡短SHA-1哈希字串與提交者的姓名,其他格式見備註。
#→還可以使用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 | 作者的修訂時間。 |
1.12 還原未來數據
什麽是未來數據?就是還原到歷史數據了,但是你後悔了,想撤銷更改,但是git log找不到這個版本了
git reflog ---查看未來歷史的更新點
1.13 還原歷史數據
[root@gitlab git_data]# git log 利用git log查看版本號
commit 3bd1ed4424eb5e66cfc5ce855228a3547e5bef47
Author: dy <[email protected]>
Date: Tue Mar 20 20:16:46 2018 +0800
xiugaiwenjian
commit 90a7e9ce4469fda8712c0a9cdb39693bd34b92c5
Author: dy <[email protected]>
Date: Tue Mar 20 20:10:09 2018 +0800
ceshiwenjian_2018.3.21
[root@gitlab git_data]# ll
total 4
-rw-r--r-- 1 root root 5 Mar 20 20:16 test
[root@gitlab git_data]# git reset --hard 90a7e9ce
HEAD is now at 90a7e9c ceshiwenjian_2018.3.21
[root@gitlab git_data]# cat test
[root@gitlab git_data]# git reset --hard 3bd1ed4424e
HEAD is now at 3bd1ed4 xiugaiwenjian
[root@gitlab git_data]# cat test
daya
1.14 標簽使用
[root@gitlab git_data]# git tag v200217 給當前提交內容打一個標簽,每次提交都可以打tag
[root@gitlab git_data]# git tag 查看當前所有標簽
v200217
[root@gitlab git_data]# git show v200217 查看當前標簽詳細信息
commit 5f499e63e4db0ce040e0527467a085fe644519b1
Author: dy <[email protected]>
Date: Wed Mar 21 13:19:56 2018 +0800
xiugai
diff --git a/jiang.txt b/jiang.txt
index ba9c1ad..c5e58fc 100644
--- a/jiang.txt
+++ b/jiang.txt
@@ -1 +1,2 @@
nihao
+888
[root@gitlab git_data]# git reset --hard 8c44f2f
HEAD is now at 8c44f2f 777
[root@gitlab git_data]# cat jiang.txt
nihao
[root@gitlab git_data]# git reset --hard v200217
HEAD is now at 5f499e6 xiugai
[root@gitlab git_data]# cat jiang.txt
nihao
888
1.15 對比數據
diff命令可以對比當前文件與倉庫已保存文件的區別,從而知道對文件做了哪些修改
[root@gitlab git_data]# cat jiang.txt
nihao
888
[root@gitlab git_data]# echo 666 >>jiang.txt
[root@gitlab git_data]# git diff jiang.txt
diff --git a/jiang.txt b/jiang.txt
index c5e58fc..a232fb5 100644
--- a/jiang.txt
+++ b/jiang.txt
@@ -1,2 +1,3 @@
nihao
888
+666
[root@gitlab git_data]#
第2章 分支結構
[root@gitlab git_data]# git branch 查看當前系統所有分支
* master
[root@gitlab git_data]# git branch linux 創建分支
[root@gitlab git_data]# git branch
linux
* master
[root@gitlab git_data]# git checkout linux 切換分支
Switched to branch 'linux'
[root@gitlab git_data]# git branch
* linux
master
[root@gitlab git_data]# git branch -D linux 刪除分支
Deleted branch linux (was 5f499e6).
2.1 代碼合並:
[root@gitlab git_data]# git merge linux
Updating 90a7e9c..84a289b
Fast-forward
test | 1 +
1 file changed, 1 insertion(+)
2.1.1 合並分支時的沖突問題:
[root@gitlab git_data]# git branch
linux
* master
[root@gitlab git_data]# cat test
linux branch
[root@gitlab git_data]# echo nihao >>test
[root@gitlab git_data]# git commit -a -m "nihao"
[root@gitlab git_data]# git checkout linux
Switched to branch 'linux'
[root@gitlab git_data]# echo buhao >>test
[root@gitlab git_data]# git commit -a -m "buhao"
[linux c21fd3c] buhao
1 file changed, 1 insertion(+)
[root@gitlab git_data]# git status
# On branch linux
nothing to commit, working directory clean
[root@gitlab git_data]# git branch
* linux
master
[root@gitlab git_data]# git checkout master
Switched to branch 'master'
[root@gitlab git_data]# git merge linux
Auto-merging test
CONFLICT (content): Merge conflict in test
Automatic merge failed; fix conflicts and then commit the result.
[root@gitlab git_data]# vim test 手動解決沖突,只保留想要保留的數據然後保存
[root@gitlab git_data]# git commit -a -m "hebingshibai"
[master e5092ee] hebingshibai
[root@gitlab git_data]# cat test
linux branch
nihao
第3章 windows客戶端使用
windows 上git軟件網站 https://git-for-windows.github.io
軟件下載地址:https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/Git-2.15.1.2-64-bit.exe 軟件安裝默認即可。
第4章 搭建gitlab私有倉庫
4.1 安裝gitlab,軟件比較大,註意內容空間
yum -y localinstall gitlab-ce-9.1.4-ce.0.el7.x86_64.rpm
gitlab-ctl reconfigure #→初始化,就執行一次
gitlab-ctl status
4.2 在瀏覽器中輸入ip地址進行訪問,我這裏ip是10.0.0.63
1.1 定義項目名稱
1.1 創建完成後會提示沒有ssh密鑰:
在服務端生成密鑰對,復制公鑰內容粘貼到網頁上即可
[root@gitlab ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
84:76:c5:d0:b0:7a:28:b0:e2:0e:12:7c:d7:cf:4d:a4 root@gitlab
The key's randomart image is:
+--[ RSA 2048]----+
| o=. |
| . oo |
| . o + . |
|. o ..= o |
|o.....o.SE . |
|.o. .. .o o |
|o. o . |
|+ |
| . |
+-----------------+
[root@gitlab ~]# cd /root/.ssh/
[root@gitlab .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAGOae1O+UBTUPJNIIgOTdgB0KXT26HhZgh5JFRgau6BifEI34goNMYxNQS5pHiSO6GdHbk+wSi5ZB3Xl9nWYL29zbtSC7TDWEoPlz/FCbk4LXylFF+20MXt0hu+NsBS8xkMk0uyIt4ELEfZ8KO/Ki2zT6aFUJrqmkqxnn9hQyoiOPZv0ewQEYHfgUnXlGkA21arIOL3fMuaLoGcuyeiTEbL2H60nG8N3kC3B/4EcUs18P9rqAKv2A2tMsHoQyzfTRNSHHf1bWnc28oZ4KcQrdIfOQkLQCXMF6Vb9HWmJ01xCdwMiTbcGTQnkudr8bmeJitNnlqIqoZ2sCYsHf52gR root@gitlab
1.1 上傳文件到gitlab私有倉庫:
[root@gitlab ~]# cd 43team
[root@gitlab 43team]# touch README.md
[root@gitlab 43team]# git add README.md
[root@gitlab 43team]# git commit -m "add README"
[master (root-commit) 9429222] add README
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
[root@gitlab 43team]# git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 208 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:root/43team.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
第2章 什麽是github?
github是一個git版本庫的托管服務,是目前全球最大的軟件倉庫,擁有上百萬的開發者用戶,也是軟件開發和尋找資源的最佳途徑,github不僅可以托管各種git版本倉庫,還擁有更美觀的web界面,您的代碼文件可以被任何人克隆,使得開發者為開源項貢獻代碼變得更加容易,當然也可以付費購買私有庫
Git詳解與gitlab使用