一、什么是Git
Git是一個開源的分布式版本控制系統(tǒng)威彰,可以有效纺铭、高速地處理從很小到非常大的項目版本管理。
二搏恤、Git與SVN對比
- 共同點
都是用來存放代碼和版本控制 - 區(qū)別
a. 工作模式:git是分布式违寿;svn是中心化(集中化)湃交。
b. 使用上:git入門周期長,但目前使用率高藤巢;svn入門簡單搞莺。
c. 分支:git創(chuàng)建和維護分支方便;svn創(chuàng)建和維護分支繁瑣菌瘪。
三腮敌、部署Git
1.1 部署在window上
下載地址[Git - Downloading Package (git-scm.com)](https://git-scm.com/download/win)
根據(jù)系統(tǒng)選擇32位或64位
1.2 部署在macOS上
#需要先安裝brew
$ brew install git
詳細安裝流程傳送門
1.3 部署在linux上
#centos/redhat
yum -y install git
#debian/ubuntu
apt install -y git
這里只介紹常用的linux版本,其它linux系統(tǒng)參考git官網(wǎng)
Git (git-scm.com)
1.4 安裝新版本教程
首先在Github 下載最新版本
這里可以選擇版本和對應(yīng)的壓縮包進行下載(這里可以將鏈接復制到linux中通過wget下載)
在2022年2月14日時(這是一個有趣的日期G卫!弊添!哈哈哈)录淡,最新版為2.35.1
#測試環(huán)境
[root@git~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core) #系統(tǒng)版本
[root@git~]# uname -r
3.10.0-1160.el7.x86_64 #內(nèi)核版本
#下載最新版并解壓
[root@git~]# wget https://github.com/git/git/archive/refs/tags/v2.35.1.tar.gz
[root@git~]# tar xf v2.35.1.tar.gz
#安裝軟件依賴包
[root@git~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
#編譯安裝
[root@git~]# cd git-2.35.1/
[root@git~/git-2.35.1]# make prefix=/usr/local/git all
[root@git~/git-2.35.1]# make prefix=/usr/local/git install
#卸載低版本的git
[root@git~]# rpm -qa git
git-1.8.3.1-23.el7_8.x86_64
[root@git~]# rpm -e perl-Git-1.8.3.1-23.el7_8.noarch git-1.8.3.1-23.el7_8.x86_64 gettext-devel-0.19.8.1-3.el7.x86_64 intltool-0.50.2-7.el7.noarch
#配置環(huán)境變量
[root@git~/git-2.35.1]# vim /etc/profile.d/git.sh
[root@git~/git-2.35.1]# cat /etc/profile.d/git.sh
export PATH=$PATH:/usr/local/git/bin
[root@git~/git-2.35.1]# . /etc/profile.d/git.sh
#測試
[root@git~/git-2.35.1]# git version
git version 2.35.1
四、Git詳解
4.1 Git初使用-開發(fā)階段
#配置用戶
[root@git~]# git config --global user.name 'yunweixiaoyu'
[root@git~]# git config --global user.email 'xxxx@qq.com'
[root@git~]# git config --global color.ui true
[root@git~]# git config --global --list
user.name=yunweixiaoyu
user.email=xxxx@qq.com
color.ui=true
[root@git~]# cat .gitconfig
[user]
name = yunweixiaoyu
email = xxxx@qq.com
[color]
ui = true
#創(chuàng)建項目代碼目錄
[root@git~]# mkdir -p /project/test/app
[root@git~]# cd /project/test/app
#對目錄進行初始化
[root@git/project/test/app]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /project/test/app/.git/
[root@git/project/test/app]# ll .git/
total 16
drwxr-xr-x 2 root root 6 Feb 14 22:41 branches
-rw-r--r-- 1 root root 92 Feb 14 22:41 config
-rw-r--r-- 1 root root 73 Feb 14 22:41 description
-rw-r--r-- 1 root root 23 Feb 14 22:41 HEAD
drwxr-xr-x 2 root root 4096 Feb 14 22:41 hooks
drwxr-xr-x 2 root root 21 Feb 14 22:41 info
drwxr-xr-x 4 root root 30 Feb 14 22:41 objects
drwxr-xr-x 4 root root 31 Feb 14 22:41 refs
#書寫代碼文件
[root@git/project/test/app]# touch live.html
[root@git/project/test/app]# echo "直播功能完成50%" >live.html
[root@git/project/test/app]# cat live.html
直播功能完成50%
#查看狀態(tài)
[root@git/project/test/app]# git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
live.html #此時這里為紅色油坝,因為代碼只存在工作區(qū)嫉戚,沒有添加到暫存區(qū)
nothing added to commit but untracked files present (use "git add" to track)
#添加到暫存區(qū)
[root@git/project/test/app]# git add . #這里也可以書寫為git add live.html
[root@git/project/test/app]# git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: live.html #綠色表示添加到暫存區(qū)
#提交到本地倉庫
[root@git/project/test/app]# git commit -m "直播50%"
[master (root-commit) ae37f5c] 直播50%
1 file changed, 1 insertion(+)
create mode 100644 live.html
[root@git/project/test/app]# git status
On branch master
nothing to commit, working tree clean
#代碼進行升級
[root@git/project/test/app]# vim live.html
[root@git/project/test/app]# cat live.html
直播功能完成70%
[root@git/project/test/app]# git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: live.html
no changes added to commit (use "git add" and/or "git commit -a")
[root@git/project/test/app]# git add .
[root@git/project/test/app]# git commit -m "直播70%"
[master 7787f00] 直播70%
1 file changed, 1 insertion(+), 1 deletion(-)
#查看commit日志(后面回滾用到commit id)
[root@git/project/test/app]# git log
commit 7787f00275ce9353da9af442cf1a9430d9cb9b7d (HEAD -> master)
Author: yunweixiaoyu <xxxx@qq.com>
Date: Mon Feb 14 23:00:49 2022 +0800
直播70%
commit ae37f5c6d5b3251f4af4e407292fd7197cf77324
Author: yunweixiaoyu <xxxx@qq.com>
Date: Mon Feb 14 22:54:38 2022 +0800
直播50%
[root@git/project/test/app]# git re
rebase relink repack request-pull revert
reflog remote replace reset
[root@git/project/test/app]# git reflog
7787f00 (HEAD -> master) HEAD@{0}: commit: 直播70%
ae37f5c HEAD@{1}: commit (initial): 直播50%
#回滾(刪除代碼測試回滾)
[root@git/project/test/app]# rm -rf live.html
[root@git/project/test/app]# ll
total 0
[root@git/project/test/app]# git reset --hard ae37f5c
HEAD is now at ae37f5c 直播50%
[root@git/project/test/app]# ll
total 4
-rw-r--r-- 1 root root 22 Feb 14 23:06 live.html
[root@git/project/test/app]# cat live.html
直播功能完成50%
[root@git/project/test/app]# git log
commit ae37f5c6d5b3251f4af4e407292fd7197cf77324 (HEAD -> master)
Author: yunweixiaoyu <xxxx@qq.com>
Date: Mon Feb 14 22:54:38 2022 +0800
直播50%
[root@git/project/test/app]# git reflog
ae37f5c (HEAD -> master) HEAD@{0}: reset: moving to ae37f5c
7787f00 HEAD@{1}: commit: 直播70%
ae37f5c (HEAD -> master) HEAD@{2}: commit (initial): 直播50%
#注意:當回滾到最開始的版本的時候,后面的版本ID號通過git log無法查看澈圈,此時可以使用git reflog進行查看彬檀,然后進行回滾
4.2 Git區(qū)域與狀態(tài)
4.3 Git命令
命令 | 含義 |
---|---|
git init | 初始化本地倉庫目錄 |
git config --global | 郵箱,用戶名,顏色 |
git add | 提交數(shù)據(jù)到緩沖區(qū)(暫存區(qū)) git add . (所有文件) 或 git add 文件 |
git commit | 把暫存區(qū)的數(shù)據(jù)提交到本地倉庫 git commit -m "標記/說明" |
git status | 顯示工作空間的狀態(tài) |
git reset | 回滾 |
git reset --soft cid(版本號) | 把指定的版本數(shù)據(jù)內(nèi)容下載到暫存區(qū) |
git reset HEAD | 暫存區(qū)--->工作空間(被修改的狀態(tài)) |
git checkout | 文件下載到工作空間并可以使用 git checkout . 或 git checkout 文件 |
git reset --hard | 版本號把本地倉庫指定版本信息數(shù)據(jù)下載到工作目錄中 |
4.4 Git分支
1)什么是分支?
分支是為了將修改記錄的整體流程分叉保存瞬女。分叉后的分支不受其他分支的影響窍帝,所以在同一個遠程倉庫里可以同時進行多個修改。分叉后的分支還可以進行合并诽偷。
默認情況下坤学,項目都有一個主分支master
2)分支案例
#查看項目分支
[root@git/project/test/app]# git branch
* master
#創(chuàng)建分支
[root@git/project/test/app]# git branch shopping
#切換到shopping分支
[root@git/project/test/app]# git checkout shopping
M live.html
Switched to branch 'shopping'
#查看當前分支
[root@git/project/test/app]# git branch
master
* shopping
#修改代碼
[root@git/project/test/app]# cat live.html
直播功能完成50%
購物功能完成70%
#添加到暫存區(qū),上傳本地倉庫
[root@git/project/test/app]# git add .
[root@git/project/test/app]# git commit -m "購物70%"
[shopping 244fb7e] 購物70%
1 file changed, 2 insertions(+), 1 deletion(-)
#此時在shopping分支上查看代碼內(nèi)容
[root@git/project/test/app]# cat live.html
直播功能完成50%
購物功能完成70%
#切換到master上查看代碼內(nèi)容
[root@git/project/test/app]# git checkout master
Switched to branch 'master'
[root@git/project/test/app]# git branch
* master
shopping
[root@git/project/test/app]# cat live.html
直播功能完成70%
#合并分支
[root@git/project/test/app]# git merge shopping -m "直播70%+購物70%"
Updating 7787f00..244fb7e
Fast-forward (no commit created; -m option ignored)
live.html | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[root@git/project/test/app]# cat live.html
直播功能完成50%
購物功能完成70%
3)臨時分支(fixbug/hotfix分支)
如果代碼中出現(xiàn)bug报慕,這時便可以創(chuàng)建分支進行修復深浮,修復完成后進行分支的刪除即可
[root@git/project/test/app]# git branch fixbug
[root@git/project/test/app]# git branch
fixbug
* master
shopping
[root@git/project/test/app]# git checkout fixbug
Switched to branch 'fixbug'
[root@git/project/test/app]# vim live.html
[root@git/project/test/app]# cat live.html
直播功能完成50%
購物功能完成70%
bug已成功修復
[root@git/project/test/app]# git add .
[root@git/project/test/app]# git commit -m "bug成功修復"
[fixbug 0405bc0] bug成功修復
1 file changed, 1 insertion(+)
[root@git/project/test/app]# git checkout master
Switched to branch 'master'
[root@git/project/test/app]# git merge fixbug -m "bug修復完成后與主分支合并"
Updating 244fb7e..0405bc0
Fast-forward (no commit created; -m option ignored)
live.html | 1 +
1 file changed, 1 insertion(+)
[root@git/project/test/app]# cat live.html
直播功能完成50%
購物功能完成70%
bug已成功修復
[root@git/project/test/app]# git branch
fixbug
* master
shopping
#修復并合并后通過git branch -d 刪除指定分支
[root@git/project/test/app]# git branch -d fixbug
Deleted branch fixbug (was 0405bc0).
[root@git/project/test/app]# git branch
* master
shopping
4)分支命令匯總
git分支命令 | 含義 |
---|---|
git branch | 查看分支 |
git branch name | 創(chuàng)建分支 |
git branch -d name | 刪除分支 |
git checkout name | 切換分支 |
git merge name | 將指定分支合并到當前分支 |
git checkout -b name | 創(chuàng)建分支并切換到這個分支 |
注意:表中name表示分支名
4.5 tag標簽
給commit id設(shè)置別名,也就是標簽眠冈,相對方便記憶迹冤。
#添加標簽(默認是對當前最新的commit id進行添加)
[root@git/project/test/app]# git tag -a "v1.0" -m "直播和購物"
#查看當前分支
[root@git/project/test/app]# git tag
v1.0
#對指定版本的commit id進行添加標簽(格式如下)
#git tag -a "標簽名" -m "描述" commitID
[root@git/project/test/app]# git reflog
0405bc0 (HEAD -> master, tag: v1.0) HEAD@{0}: merge fixbug: Fast-forward (no commit created; -m option ignored)
244fb7e (shopping) HEAD@{1}: checkout: moving from fixbug to master
0405bc0 (HEAD -> master, tag: v1.0) HEAD@{2}: commit: bug成功修復
244fb7e (shopping) HEAD@{3}: checkout: moving from master to fixbug
244fb7e (shopping) HEAD@{4}: merge shopping: Fast-forward (no commit created; -m option ignored)
7787f00 HEAD@{5}: checkout: moving from shopping to master
244fb7e (shopping) HEAD@{6}: commit: 購物70%
7787f00 HEAD@{7}: checkout: moving from master to shopping
7787f00 HEAD@{8}: reset: moving to 7787f00
7787f00 HEAD@{9}: reset: moving to 7787f00
ae37f5c HEAD@{10}: reset: moving to ae37f5c
7787f00 HEAD@{11}: commit: 直播70%
ae37f5c HEAD@{12}: commit (initial): 直播50%
[root@git/project/test/app]# git tag -a "v0.1" -m "直播" ae37f5c
[root@git/project/test/app]# git reflog
0405bc0 (HEAD -> master, tag: v1.0) HEAD@{0}: merge fixbug: Fast-forward (no commit created; -m option ignored)
244fb7e (shopping) HEAD@{1}: checkout: moving from fixbug to master
0405bc0 (HEAD -> master, tag: v1.0) HEAD@{2}: commit: bug成功修復
244fb7e (shopping) HEAD@{3}: checkout: moving from master to fixbug
244fb7e (shopping) HEAD@{4}: merge shopping: Fast-forward (no commit created; -m option ignored)
7787f00 HEAD@{5}: checkout: moving from shopping to master
244fb7e (shopping) HEAD@{6}: commit: 購物70%
7787f00 HEAD@{7}: checkout: moving from master to shopping
7787f00 HEAD@{8}: reset: moving to 7787f00
7787f00 HEAD@{9}: reset: moving to 7787f00
ae37f5c (tag: v0.1) HEAD@{10}: reset: moving to ae37f5c
7787f00 HEAD@{11}: commit: 直播70%
ae37f5c (tag: v0.1) HEAD@{12}: commit (initial): 直播50%
#將標簽上傳遠程倉庫
#這里需要先配置好遠程倉庫(配置見4.7小節(jié))
git push origin --tags #上傳所有標簽
git push origin "標簽名" #上傳指定標簽
生產(chǎn)環(huán)境常用的標簽名和含義
1. Alpha 預(yù)覽測試版
2. Beta 公開測試版
3. RC 最終測試版
4. Release 正式發(fā)布版本
5. Final 最終版,正式發(fā)布版的一直表示方法
6. Stable 穩(wěn)定版
4.6 gitignore(了解)
上傳代碼的過程中途事,代碼目錄中的隱藏文件血崭,里面的內(nèi)容默認不會上傳到遠程倉庫,代碼生成的臨時文件(需要排除上傳)诫舅。
需要排除上傳時羽利,可以在代碼的根目錄創(chuàng)建文件.gitignore書寫需要排除的內(nèi)容即可。
這個一般是開發(fā)人員進行書寫刊懈,這里不做過多介紹这弧,如果需要可以參考github上的各類語言的ignore文件
五娃闲、遠程倉庫gitee
這里我們先簡單的介紹一下gitee的使用,將本地倉庫上傳至gitee遠程倉庫中匾浪,后續(xù)我們會搭建開源的gitlab作為遠程倉庫皇帮。
使用gitee前我們需要在gitee官網(wǎng)上注冊gitee賬號,然后進行后續(xù)操作蛋辈。
5.1 gitee上創(chuàng)建倉庫
創(chuàng)建后會顯示如下內(nèi)容
5.2 遠程連接gitee
#全局配置
git config --global user.name "悠然"
git config --global user.email "xxxx@qq.com"
#因為本地git倉庫已經(jīng)創(chuàng)建過了這里就不重新創(chuàng)建了
#添加遠程倉庫
[root@git/project/test/app]# git remote add origin https://gitee.com/yunweixiaoyu/app-live.git
#查看遠程倉庫
[root@git/project/test/app]# git remote -v
origin https://gitee.com/yunweixiaoyu/app-live.git (fetch)
origin https://gitee.com/yunweixiaoyu/app-live.git (push)
#上傳本地倉庫內(nèi)容到遠程倉庫
[root@git/project/test/app]# git push -u origin "master"
Username for 'https://gitee.com': yunweixiaoyu #gitee賬戶名
Password for 'https://yunweixiaoyu@gitee.com': #gitee密碼
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (12/12), 1022 bytes | 1022.00 KiB/s, done.
Total 12 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/yunweixiaoyu/app-live.git
* [new branch] master -> master
branch 'master' set up to track 'origin/master'.
#上傳tag標簽到gitee上
[root@git/project/test/app]# git push origin --tags
Username for 'https://gitee.com': yunweixiaoyu #gitee賬戶名
Password for 'https://yunweixiaoyu@gitee.com': #gitee密碼
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 314 bytes | 314.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.2]
To https://gitee.com/yunweixiaoyu/app-live.git
* [new tag] v0.1 -> v0.1
* [new tag] v1.0 -> v1.0
#下拉代碼到本地倉庫
[root@git/project/test/app]# git pull origin master #或者git fetch origin master
#下載代碼到目錄
[root@git/project/test/app]# git clone https://gitee.com/yunweixiaoyu/app-live.git
Cloning into 'app-live'...
Username for 'https://gitee.com': yunweixiaoyu
Password for 'https://yunweixiaoyu@gitee.com':
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 14 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (14/14), done.
5.3 配置遠程倉庫秘鑰認證
使用遠程倉庫gitee的過程中属拾,我們在上傳和下拉代碼都需要輸入用戶名和密碼,因此我們可以配置秘鑰認證進行簡化操作冷溶。
1)創(chuàng)建密鑰
#生成密鑰對
[root@git~]# 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): #默認回車不創(chuàng)建密碼
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:
SHA256:x6DNxWtF/ObsnwO/GFZT3XNInkbrgj1um56iuPKXwQ4 root@git
The key's randomart image is:
+---[RSA 2048]----+
| .. o |
| . ..+ +o|
| . o ..*.=|
| + + = oo +|
| ..S * ++.o |
| E oo . ++ .|
| o o o+o |
| . .+ ...++o.|
| o+o.. o=. ++|
+----[SHA256]-----+
#查看公鑰渐白,將內(nèi)容復制
[root@git~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9xaoi5RVExW1dEQR22XO+sAsEmvuYO/P+NAubeiwDs4ki4xNRdpsXGLn1k+ZAM3z4NP+qzhITWh8HTh5Gje0C+aI/DqnAIFB9LKCRJVeuLtvUazyjKBA3tW1yTXUa3duAWYc4uqdPPB5WvuvHxtwPDpANZY0GMnyZ3Gh1AND2m6RWEMatowkDRxsacGRYi0l4Fg99T/vKg6rvrEoIYDUm2X/L95Qlv5fLVIW0cR5N6oQ5TR8Zizo5s5ZvPgNCQe2zEr3GvBmHRxUwgMMEZywQfHwrLgjSv07/m2HoZywwygZUE5/mOfvlD4RlZ5a+DLbfK6rAVfho1c3iNkXFCW+R root@git
2)公鑰配置到遠程倉庫
進入設(shè)置
按步驟將公鑰復制即可
3)添加ssh協(xié)議的遠程倉庫
遠程倉庫有兩種訪問方法
- https:每次連接都需要輸入賬戶和密碼
- ssh:密鑰認證,最常用
復制內(nèi)容
#刪除舊的遠程倉庫逞频,重新配置ssh協(xié)議遠程倉庫
root@git/project/test/app]# git remote remove origin
[root@git/project/test/app]# git remote add origin git@gitee.com:yunweixiaoyu/app-live.git
[root@git/project/test/app]# git remote -v
origin git@gitee.com:yunweixiaoyu/app-live.git (fetch)
origin git@gitee.com:yunweixiaoyu/app-live.git (push)
#測試
[root@git~]# git clone git@gitee.com:yunweixiaoyu/app-live.git
Cloning into 'app-live'...
The authenticity of host 'gitee.com (180.97.125.228)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes #第一次使用需要使用yes確認
Warning: Permanently added 'gitee.com,180.97.125.228' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 14 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (14/14), done.