Git的發(fā)展史
Linus
在1991年創(chuàng)建了開源的Linux
,從此全世界的工程師參與了Linux
的開發(fā),期初Linus
是通過手動diff
的方式進行代碼審核和合并的,后來BitKeeper
的東家BitMover
公司出于人道主義精神,授權Linux
社區(qū)免費使用這個版本控制系統(tǒng)。安定團結(jié)的大好局面在2005年就被打破了,原因是Linux社區(qū)牛人聚集,不免沾染了一些梁山好漢的江湖習氣。開發(fā)Samba的Andrew
試圖破解BitKeepe
r的協(xié)議(這么干的其實也不只他一個),被BitMover
公司發(fā)現(xiàn)了(監(jiān)控工作做得不錯?。谑?code>BitMover公司怒了,要收回Linux社區(qū)的免費使用權。Linus
花了兩周時間自己用C寫了一個分布式版本控制系統(tǒng),這就是Git
!一個月之內(nèi),Linux
系統(tǒng)的源碼已經(jīng)由Git
管理了!如果當時BitMover
公司專門為Linux
發(fā)布一個社區(qū)版,也許這家公司也就名垂青史了,可是當時這家公司思想境界沒那么高而且Linus
也那么牛!
Ubuntu安裝Git
niko@niko-notebook:~$ sudo apt install git -y
Windows安裝Git
基本就是一路Next最后Finsh。
同一團隊協(xié)作開發(fā)
假定存在用戶A和用戶B,用戶A創(chuàng)建了一個遠程倉庫,然后遠程倉庫clone到本地,進行開發(fā)完成以后push到遠程倉庫。同時用戶B也在開發(fā)這個項目,首先用戶B將遠程倉庫clone到本地進行開發(fā),完成后push到遠程倉庫,同時用戶A再pull遠程倉庫代碼同步項目。
跨團隊協(xié)作開發(fā)
假定存在兩個開發(fā)團隊A和B,開發(fā)團隊A的每個成員都擁有一個本地版本庫,但是需要開發(fā)團隊B協(xié)助開發(fā),那么開發(fā)團隊會先fork一個遠程倉庫,接著開發(fā)團隊B的成員分別clone副本遠程倉庫到本地進行開發(fā),然后push到副本遠程倉庫。完成后,開發(fā)團隊B向開發(fā)團隊A發(fā)出一個pull request請求,項目通過審核之后開發(fā)團隊A再merge副本庫到原始遠程庫,完成功能合并。
使用Git命令行
-
使用
git init
命令初始化一個新的項目
niko@niko-notebook:~$ git init StudyGit
Initialized empty Git repository in /home/niko/StudyGit/.git/
-
使用
git init
命令開始管理一個現(xiàn)有的項目
niko@niko-notebook:~$ mkdir StydyGit
niko@niko-notebook:~$ cd StydyGit/
niko@niko-notebook:~/StydyGit$ git init
Initialized empty Git repository in /home/niko/StydyGit/.git/
-
使用Git管理一個項目的時候會創(chuàng)建一下文件和目錄
niko@niko-notebook:~$ tree StudyGit -a
StudyGit
└── .git
├── HEAD
├── branches
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── prepare-commit-msg.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
10 directories, 15 files
在Git中有4個區(qū),分別是工作區(qū)、暫存區(qū)、本地庫、遠程庫。我們項目開發(fā)就是在工作區(qū)工作,然后使用git add
命令將工作區(qū)的代碼提交到暫存區(qū),使用git commit
命令將暫存區(qū)的文件提交到本地庫。Git的有一個代碼托管中心是GitHub
,同樣國內(nèi)還有Google Code
,碼云
等。
-
設置Git簽名
項目級別(本地)
niko@niko-notebook:~/StydyGit$ git config user.name niko
niko@niko-notebook:~/StydyGit$ git config user.email niko@163.com系統(tǒng)級別(全局)
niko@niko-notebook:~/StydyGit$ git config –global user.name xvge
niko@niko-notebook:~/StydyGit$ git config –global user.email xvge@163.com簽名優(yōu)先級
就近原則:項目優(yōu)先級高于系統(tǒng)優(yōu)先級
項目級別的配置信息存放到項目目錄的
.git/config
文件中:
niko@niko-notebook:~/StydyGit$ cat .git/config[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
name = niko
email = niko@163.com
-
項目級別的配置信息存放到系統(tǒng)用戶目錄的
.gitconfig
文件中:
niko@niko-notebook:~/StydyGit$ cat ~/.gitconfig[user]
name = xvge
email = xvge@163.com
-
查看當前工作區(qū)的狀態(tài)(
git status
)niko@niko-notebook:~/StydyGit$ git status On branch master # 當前所處的分支 No commits yet nothing to commit (create/copy files and use "git add" to track)
當創(chuàng)建一個新文件的時候,這個新的文件處于未被跟蹤的狀態(tài):
niko@niko-notebook:~/StydyGit$ echo "Hello, Git." > file.txt # 創(chuàng)建一個文件 niko@niko-notebook:~/StydyGit$ git status On branch master No commits yet Untracked files: # 列出未被追蹤的文件 (use "git add <file>..." to include in what will be committed) file.txt nothing added to commit but untracked files present (use "git add" to track)
-
將工作區(qū)的文件提交到暫存區(qū),使得Git可以跟蹤新文件(
git add
)niko@niko-notebook:~/StydyGit$ git add . niko@niko-notebook:~/StydyGit$ git status On branch master No commits yet Changes to be committed: # 列出更改的文件 (use "git rm --cached <file>..." to unstage) new file: file.txt
-
將暫存區(qū)的文件提交到本地庫(
git commit
)niko@niko-notebook:~/StydyGit$ git commit -m "add file.txt" [master (root-commit) 5f9adfe] add file.txt 1 file changed, 1 insertion(+) create mode 100644 file.txt
-
查看提交日志(
git log
)niko@niko-notebook:~/StydyGit$ git log commit e6442a41c339e0fd6d01656cbea24775471ee1ed (HEAD -> master) # HEAD表示當前版本 Author: niko <niko@163.com> Date: Mon Sep 24 12:34:38 2018 +0800 modifiy file.txt # 提交的注釋信息 commit 5f9adfeade857255ef647f855899965274a5fe95 # 40為16進制SHA-256值 Author: niko <niko@163.com> Date: Mon Sep 24 12:16:20 2018 +0800 add file.txt
-
查看提交日志(
git log
)多次修改后的
git log
niko@niko-notebook:~/StydyGit$ git log commit b4a54420dcea8d480b80797e032d8ed74b430a97 (HEAD -> master) Author: niko <niko@163.com> Date: Mon Sep 24 12:40:27 2018 +0800 modifiy file.txt commit 17acb2424f04c7ec0298e0c59039d19bce8369b6 Author: niko <niko@163.com> Date: Mon Sep 24 12:39:08 2018 +0800 add file3.txt commit 80cbf7f5c44e0507439bd97a8976d28b746f4f45 Author: niko <niko@163.com> Date: Mon Sep 24 12:38:26 2018 +0800 add file2.txt commit e6442a41c339e0fd6d01656cbea24775471ee1ed Author: niko <niko@163.com> Date: Mon Sep 24 12:34:38 2018 +0800 modifiy file.txt commit 5f9adfeade857255ef647f855899965274a5fe95 Author: niko <niko@163.com> Date: Mon Sep 24 12:16:20 2018 +0800 add file.txt
使用
git log --pretty=oneline
niko@niko-notebook:~/StydyGit$ git log --pretty=oneline b4a54420dcea8d480b80797e032d8ed74b430a97 (HEAD -> master) modifiy file.txt 17acb2424f04c7ec0298e0c59039d19bce8369b6 add file3.txt 80cbf7f5c44e0507439bd97a8976d28b746f4f45 add file2.txt e6442a41c339e0fd6d01656cbea24775471ee1ed modifiy file.txt 5f9adfeade857255ef647f855899965274a5fe95 add file.txt
使用
git log --oneline
niko@niko-notebook:~/StydyGit$ git log --oneline b4a5442 (HEAD -> master) modifiy file.txt 17acb24 add file3.txt 80cbf7f add file2.txt e6442a4 modifiy file.txt 5f9adfe add file.txt
使用
git reflog
niko@niko-notebook:~/StydyGit$ git reflog b4a5442 (HEAD -> master) HEAD@{0}: commit: modifiy file.txt 17acb24 HEAD@{1}: commit: add file3.txt 80cbf7f HEAD@{2}: commit: add file2.txt e6442a4 HEAD@{3}: commit: modifiy file.txt 5f9adfe HEAD@{4}: commit (initial): add file.txt
-
版本回退(
git reset
)參數(shù):
git reset –soft 回退本地庫
git reset –mixed 默認,回退暫存區(qū)、本地庫
git reset –hard 回退暫存區(qū)、工作區(qū)、本地庫niko@niko-notebook:~/StydyGit$ git reset 80cbf7f
git reset --soft
圖示
git reset --mixed
圖示
git reset --hard
圖示
-
找回文件(
git reset
)
niko@niko-notebook:~/StydyGit$ ls # 查看file2.txt文件是否存在
file.txt file2.txt file3.txt
niko@niko-notebook:~/StydyGit$ rm -rf file2.txt # 刪除file2.txt文件
niko@niko-notebook:~/StydyGit$ ls # 確保file2.txt文件已經(jīng)刪除
file.txt file3.txt
niko@niko-notebook:~/StydyGit$ git status # 查看文件跟蹤狀態(tài)
On branch master
Changes not staged for commit:
(use “git add/rm <file>…” to update what will be committed)
(use “git checkout — <file>…” to discard changes in working directory)
deleted: file2.txt
no changes added to commit (use “git add” and/or “git commit -a”)
niko@niko-notebook:~/StydyGit$ git add . # 跟蹤當前狀態(tài)
niko@niko-notebook:~/StydyGit$ git commit -m “del file2.txt” # 將代碼提交到本地庫
[master db6e563] del file2.txt
1 file changed, 1 deletion(-)
delete mode 100644 file2.txt
niko@niko-notebook:~/StydyGit$ git reflog # 查看形成一個版本信息db6e563
db6e563 (HEAD -> master) HEAD@{0}: commit: del file2.txt
bcf8ce2 HEAD@{1}: commit: first reset
80cbf7f HEAD@{2}: reset: moving to 80cbf7f
b4a5442 HEAD@{3}: commit: modifiy file.txt
17acb24 HEAD@{4}: commit: add file3.txt
80cbf7f HEAD@{5}: commit: add file2.txt
e6442a4 HEAD@{6}: commit: modifiy file.txt
5f9adfe HEAD@{7}: commit (initial): add file.txt
niko@niko-notebook:~/StydyGit$ git reset –hard bcf8ce2 # 回退到上一個版本
HEAD is now at bcf8ce2 first reset
niko@niko-notebook:~/StydyGit$ ls # file2.txt文件已經(jīng)存在
file.txt file2.txt file3.txt
niko@niko-notebook:~/StydyGit$ git status # 查看當前跟蹤狀態(tài)
On branch master
nothing to commit, working tree clean
niko@niko-notebook:~/StydyGit$ git reflog # 生成一個版本信息
bcf8ce2 (HEAD -> master) HEAD@{0}: reset: moving to bcf8ce2
db6e563 HEAD@{1}: commit: del file2.txt
bcf8ce2 (HEAD -> master) HEAD@{2}: commit: first reset
80cbf7f HEAD@{3}: reset: moving to 80cbf7f
b4a5442 HEAD@{4}: commit: modifiy file.txt
17acb24 HEAD@{5}: commit: add file3.txt
80cbf7f HEAD@{6}: commit: add file2.txt
e6442a4 HEAD@{7}: commit: modifiy file.txt
5f9adfe HEAD@{8}: commit (initial): add file.txt
git diff
使用git diff –staged比較工作區(qū)與暫存區(qū)的不同。
使用git diff –cached比較暫存區(qū)與本地庫的不同。
分支管理
分支可以并行推進項目的開發(fā),開發(fā)的某一個功能如果失敗不會影響項目整體。
-
查看分支(
git branch -v
)niko@niko-notebook:~/StydyGit$ git branch -v * master bcf8ce2 first reset
-
創(chuàng)建分支(
git branch branchName
)niko@niko-notebook:~/StydyGit$ git branch hot_fix
-
切換分支(
git checkout branchName
)niko@niko-notebook:~/StydyGit$ git checkout hot_fix Switched to branch 'hot_fix' niko@niko-notebook:~/StydyGit$ git branch -v # 星號所在分支就是當前分支 * hot_fix bcf8ce2 first reset master bcf8ce2 first reset
-
合并分支(
git merge branchName
)
niko@niko-notebook:~/StydyGit$ vim file3.txt # 修改文件
niko@niko-notebook:~/StydyGit$ git status # 查看分支狀態(tài)
On branch hot_fix
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: file3.txt
no changes added to commit (use “git add” and/or “git commit -a”)
niko@niko-notebook:~/StydyGit$ git add . # 添加到當前分支的跟蹤狀態(tài)
niko@niko-notebook:~/StydyGit$ git commit -m “fix bug by xvge” # 提交代碼
[hot_fix 40376b9] fix bug by xvge
1 file changed, 1 insertion(+)
niko@niko-notebook:~/StydyGit$ git checkout master # 切換到主分支
Switched to branch ‘master’
niko@niko-notebook:~/StydyGit$ git merge hot_fix # 將hot_fix合并到master
Updating bcf8ce2..40376b9
Fast-forward
file3.txt | 1 +
1 file changed, 1 insertion(+)
niko@niko-notebook:~/StydyGit$ git stataus # 查看主分支狀態(tài)
git: ‘stataus’ is not a git command. See ‘git –help’.
The most similar command is
status
niko@niko-notebook:~/StydyGit$ git status
On branch master
nothing to commit, working tree clean
niko@niko-notebook:~/StydyGit$ git reflog # 產(chǎn)生幾個新版本
40376b9 (HEAD -> master, hot_fix) HEAD@{0}: merge hot_fix: Fast-forward
bcf8ce2 HEAD@{1}: checkout: moving from hot_fix to master
40376b9 (HEAD -> master, hot_fix) HEAD@{2}: commit: fix bug by xvge
bcf8ce2 HEAD@{3}: checkout: moving from master to hot_fix
bcf8ce2 HEAD@{4}: reset: moving to bcf8ce2
db6e563 HEAD@{5}: commit: del file2.txt
bcf8ce2 HEAD@{6}: commit: first reset
80cbf7f HEAD@{7}: reset: moving to 80cbf7f
b4a5442 HEAD@{8}: commit: modifiy file.txt
17acb24 HEAD@{9}: commit: add file3.txt
80cbf7f HEAD@{10}: commit: add file2.txt
e6442a4 HEAD@{11}: commit: modifiy file.txt
5f9adfe HEAD@{12}: commit (initial): add file.txt
- 合并代碼時產(chǎn)生了沖突
# 修改master分支代碼
niko@niko-notebook:~/StydyGit$ git branch -v
hot_fix 40376b9 fix bug by xvge
* master 40376b9 fix bug by xvge
niko@niko-notebook:~/StydyGit$ vim file3.txt
niko@niko-notebook:~/StydyGit$ git add .
niko@niko-notebook:~/StydyGit$ git commit -m “add festure by master”
[master cbd7ce1] add festure by master
1 file changed, 1 insertion(+)
# 修改hot_fix分支代碼
niko@niko-notebook:~/StydyGit$ git branch -v
* hot_fix 40376b9 fix bug by xvge
master 40376b9 fix bug by xvge
niko@niko-notebook:~/StydyGit$ vim file3.txt
niko@niko-notebook:~/StydyGit$ git add .
niko@niko-notebook:~/StydyGit$ git commit -m “add feature by fix”
[hot_fix 6cceae3] add feature by fix
1 file changed, 1 insertion(+)
# 將master合并到hot_fix上
niko@niko-notebook:~/StydyGit$ git branch -v
* hot_fix 6cceae3 add feature by fix
master cbd7ce1 add festure by master
niko@niko-notebook:~/StydyGit$ git merge master
Auto-merging file3.txt
CONFLICT (content): Merge conflict in file3.txt
Automatic merge failed; fix conflicts and then commit the result.
# 解決沖突
niko@niko-notebook:~/StydyGit$ vim file3.txt
Hi, FullStackDev.
fix this bug by xvge.
<<<<<<< HEAD
add festure by fix.
=======
add feature by master.
>>>>>>> master
# 提×××并
niko@niko-notebook:~/StydyGit$ git add .
niko@niko-notebook:~/StydyGit$ git commit -m “merge code by conflict”
[hot_fix 088f6c5] merge code by conflict
Git與GitHub遠程倉庫交互
- 創(chuàng)建遠程倉庫
github.com
,如果勾選Initialize this repository with a README
選項可能首次推送失敗。
-
將GitHub地址保存到本地
niko@niko-notebook:~/StydyGit$ git remote add origin https://github.com/xvGe/StudyGit.git
-
查看本地保存了什么地址
niko@niko-notebook:~/StydyGit$ git remote -v
origin https://github.com/xvGe/StudyGit.git (fetch)
origin https://github.com/xvGe/StudyGit.git (push)
-
將本地庫推送到遠程庫
niko@niko-notebook:~/StydyGit$ git push origin master
Username for ‘https://github.com’: xvGe
Password for ‘https://xvGe@github.com’:
Counting objects: 19, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (19/19), 1.44 KiB | 52.00 KiB/s, done.
Total 19 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
remote:
remote: Create a pull request for ‘master’ on GitHub by visiting:
remote: https://github.com/xvGe/StudyGit/pull/new/master
remote:
To https://github.com/xvGe/StudyGit.git
* [new branch] master -> master
- 克隆遠程庫(
git clone
)
niko@niko-notebook:~/StydyGit$ cd ../
niko@niko-notebook:~$ rm -rf StydyGit/
niko@niko-notebook:~$ git clone https://github.com/xvGe/StudyGit.git
Cloning into ‘StudyGit’…
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 19 (delta 2), reused 19 (delta 2), pack-reused 0
Unpacking objects: 100% (19/19), done.
- 拉取遠程庫(
git pull
和git fetch
)
niko@niko-notebook:~$ git init StudyGit2 # 創(chuàng)建新項目
Initialized empty Git repository in /home/niko/StudyGit2/.git/
niko@niko-notebook:~$ cd StudyGit2/ # 進入項目目錄
niko@niko-notebook:~/StudyGit2$ git remote add origin https://github.com/xvGe/StudyGit.git#添加地址
niko@niko-notebook:~/StudyGit2$ git fetch origin master # 拉取項目
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 19 (delta 2), reused 19 (delta 2), pack-reused 0
Unpacking objects: 100% (19/19), done.
From https://github.com/xvGe/StudyGit
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
niko@niko-notebook:~/StudyGit2$ git branch -a # 查看所有分支
remotes/origin/master
niko@niko-notebook:~/StudyGit2$ git branch -r # 查看遠程分支
origin/master
niko@niko-notebook:~/StudyGit2$ git merge origin/master # 合并分支
niko@niko-notebook:~/StudyGit2$ ls
file.txt file2.txt file3.txt
niko@niko-notebook:~/StudyGit2$ cd ../ # 返回上層
niko@niko-notebook:~$ git init StudyGit3 # 初始化一個項目
Initialized empty Git repository in /home/niko/StudyGit3/.git/
niko@niko-notebook:~$ cd StudyGit3/ # 進入項目目錄
niko@niko-notebook:~/StudyGit3$ git pull https://github.com/xvGe/StudyGit.git # 拉取項目
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 19 (delta 2), reused 19 (delta 2), pack-reused 0
Unpacking objects: 100% (19/19), done.
From https://github.com/xvGe/StudyGit
* branch HEAD -> FETCH_HEAD
niko@niko-notebook:~/StudyGit3$ ls
file.txt file2.txt file3.txt
git fetch
和git pull
的區(qū)別是,前者會創(chuàng)建新分支,需要合并操作,但是更加安全;后者直接修改本地代碼。
跨團隊協(xié)作
- 遠程克隆倉庫
-
團隊拉取遠程倉庫修改并推送
niko@niko-notebook:~$ git init StudyGitHub
Initialized empty Git repository in /home/niko/StudyGitHub/.git/
niko@niko-notebook:~$ cd StudyGitHub/
niko@niko-notebook:~/StudyGitHub$ git pull https://github.com/xv-niko/StudyGit.git
niko@niko-notebook:~/StudyGitHub$ git remote add origin https://github.com/xv-niko/StudyGit.git
niko@niko-notebook:~/StudyGitHub$ ls
file.txt file2.txt file3.txt
niko@niko-notebook:~/StudyGitHub$ echo “Desgin by B” >> file3.txt
niko@niko-notebook:~/StudyGitHub$ git add .
niko@niko-notebook:~/StudyGitHub$ git commit -m “add code”
[master 09003b9] add code
1 file changed, 1 insertion(+)
niko@niko-notebook:~/StudyGitHub$ git push origin master
Username for ‘https://github.com’: xv-niko
Password for ‘https://xv-niko@github.com’:
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 274 bytes | 68.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/xv-niko/StudyGit.git
cbd7ce1..09003b9 master -> master
- 發(fā)起
pull requests
- 項目所有者團隊查看請求
- 合并代碼
-
所有者團隊合并到本地
niko@niko-notebook:~/StudyGitHub$ cd ../StudyGit
niko@niko-notebook:~/StudyGit$ git pull origin master
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/xvGe/StudyGit
* branch master -> FETCH_HEAD
cbd7ce1..0ca0905 master -> origin/master
Updating cbd7ce1..0ca0905
Fast-forward
file3.txt | 1 +
1 file changed, 1 insertion(+)
niko@niko-notebook:~/StudyGit$ cat file3.txt
Hi, FullStackDev.
fix this bug by xvge.
add feature by master.
Desgin by B
Git工作流
- 集中式工作流
- Git Flow(常用)
- Forking工作流
部署GitLab代碼托管服務器
yum clean all && yum repolist all
yum install -y curl policycoreutils-Python openssh-server
systemctl enable sshd
systemctl start sshd
firewall-cmd –permanent –add-service=http # 如果開啟了防火墻執(zhí)行
systemctl reload firewalld # 如果開啟了防火墻執(zhí)行
yum install postfix -y
systemctl enable postfix
systemctl start postfix
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | bash
EXTERNAL_URL=”http://gitlab.example.com” yum install -y gitlab-ce
gitlab-ctl reconfigure # 配置服務器
gitlab-ctl start # 啟動服務器
訪問服務器地址即可,GitLab服務器搭建指南:https://about.gitlab.com/installation/