使用Git與GitHub協同開發並搭建私有GitLab代碼托管服務器
[TOC]
Git的發展史
Linus
在1991年創建了開源的Linux
,從此全世界的工程師參與了Linux
的開發,期初Linus
是通過手動diff
的方式進行代碼審核和合並的,後來BitKeeper
的東家BitMover
公司出於人道主義精神,授權Linux
社區免費使用這個版本控制系統。安定團結的大好局面在2005年就被打破了,原因是Linux社區牛人聚集,不免沾染了一些梁山好漢的江湖習氣。開發Samba的Andrew
試圖破解BitKeepe
r的協議(這麽幹的其實也不只他一個),被BitMover
公司發現了(監控工作做得不錯!),於是BitMover
公司怒了,要收回Linux社區的免費使用權。Linus
Git
!一個月之內,Linux
系統的源碼已經由Git
管理了!如果當時BitMover
公司專門為Linux
發布一個社區版,也許這家公司也就名垂青史了,可是當時這家公司思想境界沒那麽高而且Linus
也那麽牛!
Ubuntu安裝Git
niko@niko-notebook:~$ sudo apt install git -y
Windows安裝Git
基本就是一路Next最後Finsh。
同一團隊協作開發
假定存在用戶A和用戶B,用戶A創建了一個遠程倉庫,然後遠程倉庫clone到本地,進行開發完成以後push到遠程倉庫。同時用戶B也在開發這個項目,首先用戶B將遠程倉庫clone到本地進行開發,完成後push到遠程倉庫,同時用戶A再pull遠程倉庫代碼同步項目。
跨團隊協作開發
假定存在兩個開發團隊A和B,開發團隊A的每個成員都擁有一個本地版本庫,但是需要開發團隊B協助開發,那麽開發團隊會先fork一個遠程倉庫,接著開發團隊B的成員分別clone副本遠程倉庫到本地進行開發,然後push到副本遠程倉庫。完成後,開發團隊B向開發團隊A發出一個pull request請求,項目通過審核之後開發團隊A再merge副本庫到原始遠程庫,完成功能合並。
使用Git命令行
-
使用
git init
命令初始化一個新的項目niko@niko-notebook:~$ git init StudyGit Initialized empty Git repository in /home/niko/StudyGit/.git/
-
使用
git init
命令開始管理一個現有的項目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管理一個項目的時候會創建一下文件和目錄
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個區,分別是工作區、暫存區、本地庫、遠程庫。我們項目開發就是在工作區工作,然後使用git add
命令將工作區的代碼提交到暫存區,使用git commit
命令將暫存區的文件提交到本地庫。Git的有一個代碼托管中心是GitHub
,同樣國內還有Google Code
,碼雲
等。
-
設置Git簽名
項目級別(本地)
niko@niko-notebook:~/StydyGit$ git config user.name niko niko@niko-notebook:~/StydyGit$ git config user.email [email protected]
系統級別(全局)
niko@niko-notebook:~/StydyGit$ git config --global user.name xvge niko@niko-notebook:~/StydyGit$ git config --global user.email [email protected]
簽名優先級
就近原則:項目優先級高於系統優先級
項目級別的配置信息存放到項目目錄的
.git/config
文件中:niko@niko-notebook:~/StydyGit$ cat .git/config
[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [user] name = niko email = [email protected]
項目級別的配置信息存放到系統用戶目錄的
.gitconfig
文件中:niko@niko-notebook:~/StydyGit$ cat ~/.gitconfig
[user] name = xvge email = [email protected]
-
查看當前工作區的狀態(
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)
當創建一個新文件的時候,這個新的文件處於未被跟蹤的狀態:
niko@niko-notebook:~/StydyGit$ echo "Hello, Git." > file.txt # 創建一個文件 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)
-
將工作區的文件提交到暫存區,使得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
-
將暫存區的文件提交到本地庫(
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 <[email protected]> Date: Mon Sep 24 12:34:38 2018 +0800 modifiy file.txt # 提交的註釋信息 commit 5f9adfeade857255ef647f855899965274a5fe95 # 40為16進制SHA-256值 Author: niko <[email protected]> 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 <[email protected]> Date: Mon Sep 24 12:40:27 2018 +0800 modifiy file.txt commit 17acb2424f04c7ec0298e0c59039d19bce8369b6 Author: niko <[email protected]> Date: Mon Sep 24 12:39:08 2018 +0800 add file3.txt commit 80cbf7f5c44e0507439bd97a8976d28b746f4f45 Author: niko <[email protected]> Date: Mon Sep 24 12:38:26 2018 +0800 add file2.txt commit e6442a41c339e0fd6d01656cbea24775471ee1ed Author: niko <[email protected]> Date: Mon Sep 24 12:34:38 2018 +0800 modifiy file.txt commit 5f9adfeade857255ef647f855899965274a5fe95 Author: niko <[email protected]> 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
)參數:
git reset --soft 回退本地庫
git reset --mixed 默認,回退暫存區、本地庫
git reset --hard 回退暫存區、工作區、本地庫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文件已經刪除 file.txt file3.txt niko@niko-notebook:~/StydyGit$ git status # 查看文件跟蹤狀態 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 . # 跟蹤當前狀態 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文件已經存在 file.txt file2.txt file3.txt niko@niko-notebook:~/StydyGit$ git status # 查看當前跟蹤狀態 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
比較工作區與暫存區的不同。使用
git diff --cached
比較暫存區與本地庫的不同。
分支管理
分支可以並行推進項目的開發,開發的某一個功能如果失敗不會影響項目整體。
-
查看分支(
git branch -v
)niko@niko-notebook:~/StydyGit$ git branch -v * master bcf8ce2 first reset
-
創建分支(
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 # 查看分支狀態 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 . # 添加到當前分支的跟蹤狀態 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 # 查看主分支狀態 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 # 產生幾個新版本 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
- 合並代碼時產生了沖突
# 修改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遠程倉庫交互
- 創建遠程倉庫
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://[email protected]‘: 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 # 創建新項目
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
的區別是,前者會創建新分支,需要合並操作,但是更加安全;後者直接修改本地代碼。
跨團隊協作
- 遠程克隆倉庫
-
團隊拉取遠程倉庫修改並推送
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://[email protected]‘: 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
- 發起
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 # 如果開啟了防火墻執行
systemctl reload firewalld # 如果開啟了防火墻執行
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/
使用Git與GitHub協同開發並搭建私有GitLab代碼托管服務器