第1章 Git 概述
Git 是一個免費的、開源的分布式版本控制系統,可以快速高效地處理從小型到大型的各種項目。
Git 易于學習,占地面積小,性能極快。 它具有廉價的本地庫,方便的暫存區域和多個工作流分支等特性。其性能優于 Subversion、CVS、Perforce 和 ClearCase 等版本控制工具。
-
- 何為版本控制
版本控制是一種記錄文件內容變化,以便將來查閱特定版本修訂情況的系統。
|
版本控制其實最重要的是可以記錄文件修改歷史記錄,從而讓用戶能夠查看歷史版本,方便版本切換。
-
- 為什么需要版本控制
個人開發過渡到團隊協作。
1 |
2 |
3 |
aaa bbb ccc ddd |
aaa bbb222 ccc ddd |
aaa bbb222 ccc ddd444 |
… |
服務器 |
aaa bbb222 ccc ddd |
aaa bbb ccc ddd444 |
-
- 版本控制工具
- 集中式版本控制工具
- 版本控制工具
CVS、SVN(Subversion)、VSS……
集中化的版本控制系統諸如 CVS、SVN 等,都有一個單一的集中管理的服務器,保存所有文件的修訂版本,而協同工作的人們都通過客戶端連到這臺服務器,取出最新的文件或者提交更新。多年以來,這已成為版本控制系統的標準做法。
這種做法帶來了許多好處,每個人都可以在一定程度上看到項目中的其他人正在做些什么。而管理員也可以輕松掌控每個開發者的權限,并且管理一個集中化的版本控制系統,要遠比在各個客戶端上維護本地數據庫來得輕松容易。
事分兩面,有好有壞。這么做顯而易見的缺點是中央服務器的單點故障。如果服務器宕機一小時,那么在這一小時內,誰都無法提交更新,也就無法協同工作。
-
-
- 分布式版本控制工具
-
Git、Mercurial、Bazaar、Darcs……
像 Git 這種分布式版本控制工具,客戶端提取的不是最新版本的文件快照,而是把代碼倉庫完整地鏡像下來(本地庫)。這樣任何一處協同工作用的文件發生故障,事后都可以用其他客戶端的本地倉庫進行恢復。因為每個客戶端的每一次文件提取操作,實際上都是一次對整個文件倉庫的完整備份。
分布式的版本控制系統出現之后,解決了集中式版本控制系統的缺陷:
- 服務器斷網的情況下也可以進行開發(因為版本控制是在本地進行的)
- 每個客戶端保存的也都是整個完整的項目(包含歷史記錄,更加安全)
-
- Git 簡史
1991年 |
2002年 |
2005年 |
BitKeeper的東家 BitMover公司出于人道主義精神,授權 Linux社區免費使用這個版本控制系統。但要求不能進行破解。 |
2008年 |
與開發者進行強有力的約束 |
Linux社區無法像 商業公司那樣對參 |
開發Samba的Andrew 試圖破解BitKeeper的協議,被BitMover公司發現,要收回Linux社區的免費使用權。 |
………. |
GitHub上線 |
PHP |
Ruby |
一個月后Linux系統代碼由Git管理 |
jQuery |
主體程序開發完成只用了兩周 |
Linus自己用C語言開發了一個分布式版本控制系統:Git |
商業軟件:BitKeeper |
Linus本人手動合并代碼 |
Linux系統版本控制歷史 |
|
-
- Git 工作機制
歷史版本 |
git commit |
臨時存儲 |
git add |
寫代碼 |
工作區 |
暫存區 |
本地庫 |
|
-
- Git 和代碼托管中心
代碼托管中心是基于網絡服務器的遠程代碼倉庫,一般我們簡單稱為遠程庫。
-
-
- 局域網
- GitLab
- 互聯網
- GitHub(外網)
- Gitee 碼云(國內網站)
- 局域網
-
第2章 Git 安裝
官網地址: https://git-scm.com/
查看GNU 協議,可以直接點擊下一步。
選擇Git 安裝位置,要求是非中文并且沒有空格的目錄,然后下一步。
|
|
Git 選項配置,推薦默認設置,然后下一步。
|
|
Git 安裝目錄名,不用修改,直接點擊下一步。
|
|
Git 的默認編輯器,建議使用默認的 Vim 編輯器,然后點擊下一步。
|
|
默認分支名設置,選擇讓Git 決定,分支名默認為 master,下一步。
|
|
修改Git 的環境變量,選第一個,不修改環境變量,只在Git Bash 里使用Git。
|
|
選擇后臺客戶端連接協議,選默認值 OpenSSL,然后下一步。
|
|
|
選擇Git 終端類型,選擇默認的Git Bash 終端,然后繼續下一步。
選擇Git pull 合并的模式,選擇默認,然后下一步。
|
|
選擇Git 的憑據管理器,選擇默認的跨平臺的憑據管理器,然后下一步。
|
|
其他配置,選擇默認設置,然后下一步。
|
|
實驗室功能,技術還不成熟,有已知的 bug,不要勾選,然后點擊右下角的 Install
按鈕,開始安裝Git。
|
|
點擊 Finsh 按鈕,Git 安裝成功!
右鍵任意位置,在右鍵菜單里選擇Git Bash Here 即可打開 Git Bash 命令行終端。
|
|
在 Git Bash 終端里輸入 git --version 查看 git 版本,如圖所示,說明Git 安裝成功。
第 3 章 Git 常用命令
命令名稱 | 作用 |
git config --global user.name 用戶名 | 設置用戶簽名 |
git config --global user.email 郵箱 | 設置用戶簽名 |
git init | 初始化本地庫 |
git status | 查看本地庫狀態 |
git add 文件名 | 添加到暫存區 |
git commit -m "日志信息" 文件名 | 提交到本地庫 |
git reflog | 查看歷史記錄 |
git reset --hard 版本號 | 版本穿梭 |
-
- 設置用戶簽名
1)基本語法
git config --global user.name 用戶名 git config --global user.email 郵箱
案例實操???????????????????????????????????
全局范圍的簽名設置:
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)??????????
$ git config --global user.name Layne Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git config --global user.email Layne@atguigu.com Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ cat ~/.gitconfig [user] name = Layne email = Layne@atguigu.com |
說明:
簽名的作用是區分不同操作者身份。用戶的簽名信息在每一個版本的提交信息中能夠看到,以此確認本次提交是誰做的。Git 首次安裝必須設置一下用戶簽名,否則無法提交代碼。
※注意:這里設置用戶簽名和將來登錄 GitHub(或其他代碼托管中心)的賬號沒有任何關系。
-
- 初始化本地庫
|
3)結果查看
|
-
- 查看本地庫狀態
1)基本語法
git status 2)案例實操
-
-
- 首次查看(工作區沒有任何文件)
-
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track) |
|
-
-
- 新增文件(hello.txt)
-
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ vim hello.txt hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! |
|
-
-
- 再次查看(檢測到未追蹤的文件)
-
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) hello.txt nothing added to commit but untracked files present (use "git add" to track) |
|
-
- 添加暫存區
- 將工作區的文件添加到暫存區
- 添加暫存區
|
-
-
- 查看狀態(檢測到暫存區有新文件)
-
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file:? hello.txt |
|
-
- 提交本地庫
- 將暫存區的文件提交到本地庫
- 提交本地庫
- 基本語法
git commit -m "日志信息" 文件名
- 案例實操
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git commit -m "my first commit" hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory. [master (root-commit) 86366fa] my first commit 1 file changed, 16 insertions(+) create mode 100644 hello.txt |
|
-
-
- 查看狀態(沒有文件需要提交)
-
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git status On branch master nothing to commit, working tree clean |
|
-
- 修改文件(hello.txt)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ vim hello.txt hello git! hello atguigu! 2222222222222 hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! |
|
-
-
- 查看狀態(檢測到工作區有文件被修改)
-
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ 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:? hello.txt no changes added to commit (use "git add" and/or "git commit -a") |
|
-
-
- 將修改的文件再次添加暫存區
-
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git add hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory. |
|
-
-
- 查看狀態(工作區的修改添加到了暫存區)
-
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified:? hello.txt |
|
-
- 歷史版本
- 查看歷史版本
- 歷史版本
1)基本語法
|
-
-
- 版本穿梭
-
- 基本語法
git reset --hard 版本號
- 案例實操
--首先查看當前的歷史記錄,可以看到當前是在 087a1a7 這個版本 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git reflog 087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit ca8ded6 HEAD@{1}: commit: my second commit 86366fa HEAD@{2}: commit (initial): my first commit --切換到 86366fa 版本,也就是我們第一次提交的版本 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git reset --hard 86366fa HEAD is now at 86366fa my first commit --切換完畢之后再查看歷史記錄,當前成功切換到了 86366fa 版本 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git reflog 86366fa (HEAD -> master) HEAD@{0}: reset: moving to 86366fa 087a1a7 HEAD@{1}: commit: my third commit ca8ded6 HEAD@{2}: commit: my second commit 86366fa (HEAD -> master) HEAD@{3}: commit (initial): my first commit --然后查看文件 hello.txt,發現文件內容已經變化 $ cat hello.txt hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! |
|
hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! |
Git 切換版本,底層其實是移動的 HEAD 指針,具體原理如下圖所示。
head |
master |
first |
|
第 4 章 Git 分支操作
線上 [master] |
生產 [dev分支] |
服務器 |
部署 |
服務器 |
部署 |
提交 |
用戶 |
運維 |
測試 |
|
-
- 什么是分支
hot-fix master |
feature-blue |
feature-game |
|
在版本控制過程中,同時推進多個任務,為每個任務,我們就可以創建每個任務的單獨分支。使用分支意味著程序員可以把自己的工作從開發主線上分離開來,開發自己分支的時候,不會影響主線分支的運行。對于初學者而言,分支可以簡單理解為副本,一個分支就是一個單獨的副本。(分支底層其實也是指針的引用)
-
- 分支的好處
同時并行推進多個功能開發,提高開發效率。
各個分支在開發過程中,如果某一個分支開發失敗,不會對其他分支有任何影響。失敗的分支刪除重新開始即可。
-
- 分支的操作
命令名稱 | 作用 |
git branch 分支名 | 創建分支 |
git branch -v | 查看分支 |
git checkout 分支名 | 切換分支 |
git merge 分支名 | 把指定的分支合并到當前分支上 |
-
-
- 查看分支
-
|
-
-
- 創建分支
-
- 基本語法
git branch 分支名
- 案例實操
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git branch hot-fix Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git branch -v hot-fix 087a1a7 my third commit (剛創建的新的分支,并將主分支 master 的內容復制了一份) * master 087a1a7 my third commit |
|
-
-
- 修改分支
-
--在 maste 分支上做修改 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ vim hello.txt --添加暫存區 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) |
|
$ git add hello.txt
--提交本地庫
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git commit -m "my forth commit" hello.txt
[master f363b4c] my forth commit
1 file changed, 1 insertion(+), 1 deletion(-)
--查看分支
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git branch -v
hot-fix 087a1a7 my third commit (hot-fix 分支并未做任何改變)
* master f363b4c my forth commit (當前 master 分支已更新為最新一次提交的版本)
--查看 master 分支上的文件內容
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ cat hello.txt
hello git! hello atguigu! 2222222222222 hello git! hello atguigu! 3333333333333 hello git! hello atguigu!
hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu!
hello git! hello atguigu! master test hello git! hello atguigu!
-
-
- 切換分支
-
- 基本語法
git checkout 分支名
- 案例實操
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git checkout hot-fix Switched to branch 'hot-fix' --發現當先分支已由 master 改為 hot-fix Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix) $ --查看 hot-fix 分支上的文件內容發現與 master 分支上的內容不同 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix) $ cat hello.txt hello git! hello atguigu! 2222222222222 hello git! hello atguigu! 3333333333333 hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! |
|
—————————————————————————————
hello git! hello atguigu!
hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu!
--在 hot-fix 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ cat hello.txt
hello git! hello atguigu! 2222222222222 hello git! hello atguigu! 3333333333333 hello git! hello atguigu!
hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu!
hello git! hello atguigu! hot-fix test
--添加暫存區
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ git add hello.txt
--提交本地庫
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix)
$ git commit -m "hot-fix commit" hello.txt
-
-
- 合并分支
-
- 基本語法
git merge 分支名
- 案例實操 在 master 分支上合并 hot-fix 分支
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git merge hot-fix Auto-merging hello.txt CONFLICT (content): Merge conflict in hello.txt Automatic merge failed; fix conflicts and then commit the result. |
|
-
-
- 產生沖突
-
沖突產生的表現:后面狀態為 MERGING
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)???
$ cat hello.txt hello git! hello atguigu! 2222222222222 hello git! hello atguigu! 3333333333333 hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! |
<<<<<<< HEAD hello git! hello atguigu! master test hello git! hello atguigu! ======= hello git! hello atguigu! hello git! hello atguigu! hot-fix test >>>>>>> hot-fix |
hello | git! | hello | atguigu! | |
hello | git! | hello | atguigu! | |
hello | git! | hello | atguigu! | |
hello | git! | hello | atguigu! | |
hello | git! | hello | atguigu! | |
hello | git! | hello | atguigu! | |
hello | git! | hello | atguigu! | |
hello | git! | hello | atguigu! | |
hello | git! | hello | atguigu! |
沖突產生的原因:
合并分支時,兩個分支在同一個文件的同一個位置有兩套完全不同的修改。Git 無法替我們決定使用哪一個。必須人為決定新代碼內容。
查看狀態(檢測到有文件有兩處修改)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING) $ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified:? hello.txt no changes added to commit (use "git add" and/or "git commit -a") |
|
-
-
- 解決沖突
-
- 編輯有沖突的文件,刪除特殊符號,決定要使用的內容
特殊符號:<<<<<<< HEAD 當前分支的代碼 =======? 合并過來的代碼 >>>>>>> hot-fix
hello git! hello atguigu! 2222222222222 hello git! hello atguigu! 3333333333333 hello git! hello atguigu! |
|
hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! master test hello git! hello atguigu! hot-fix test |
- 添加到暫存區
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING) $ git add hello.txt |
|
- 執行提交(注意:此時使用 git commit 命令時不能帶文件名)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING) $ git commit -m "merge hot-fix" [master 69ff88d] merge hot-fix --發現后面 MERGING 消失,變為正常 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ |
|
-
- 創建分支和切換分支圖解
first |
second |
third |
fourth |
master |
head |
|
master、hot-fix 其實都是指向具體版本記錄的指針。當前所在的分支,其實是由 HEAD
決定的。所以創建分支的本質就是多創建一個指針。
HEAD 如果指向 master,那么我們現在就在master 分支上。
HEAD 如果執行 hotfix,那么我們現在就在hotfix 分支上。
所以切換分支的本質就是移動HEAD 指針。
第 5 章 Git 團隊協作機制
-
-
代碼托管中心
pull
push
clone
push
本地庫
本地庫
遠程庫
團隊內協作
-
-
-
merge
遠程庫
[岳不群]
審核??? Pull request
fork
代碼托管中心
遠程庫
[東方不敗]
pull
clone
push
本地庫
本地庫
本地庫
跨團隊協作
-
第 6 章 GitHub 操作
GitHub 網址:https://github.com/
Ps:全球最大同性交友網站,技術宅男的天堂,新世界的大門,你還在等什么?
賬號 | 姓名 | 驗證郵箱 |
atguiguyueyue | 岳不群 | atguiguyueyue@aliyun.com |
atguigulinghuchong | 令狐沖 | atguigulinghuchong@163.com |
atguigudongfang1 | 東方不敗 | atguigudongfang@163.com |
注:此三個賬號為講師使用賬號,同學請自行注冊,然后三個同學為一組進行團隊協作!
-
- 創建遠程倉庫
|
-
- 遠程倉庫操作
命令名稱 | 作用 |
git remote -v | 查看當前所有遠程地址別名 |
git remote add 別名 遠程地址 | 起別名 |
git push 別名 分支 | 推送本地分支上的內容到遠程倉庫 |
git clone 遠程地址 | 將遠程倉庫的內容克隆到本地 |
git pull 遠程庫地址別名 遠程分支名 | 將遠程倉庫對于分支最新內容拉下來后與 當前本地分支直接合并 |
-
-
- 創建遠程倉庫別名
-
1)基本語法
|
https://github.com/atguiguyueyue/git-shTest.git
這個地址在創建完遠程倉庫后生成的連接,如圖所示紅框中
|
-
-
- 推送本地分支到遠程倉庫
-
- 基本語法
git push 別名 分支
- 案例實操
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git push ori master Logon failed, use ctrl+c to cancel basic credential prompt. Username for 'https://github.com': atguiguyueyue Counting objects: 3, done. Delta compression using up to 12 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To? https://github.com/atguiguyueyue/git-shTest.git * [new branch]???? master -> master |
|
此時發現已將我們master 分支上的內容推送到GitHub 創建的遠程倉庫。
|
-
-
- 克隆遠程倉庫到本地
-
- 基本語法
git clone 遠程地址
- 案例實操
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/pro-linghuchong $ git clone https://github.com/atguiguyueyue/git-shTest.git Cloning into 'git-shTest'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. |
|
https://github.com/atguiguyueyue/git-shTest.git
這個地址為遠程倉庫地址,克隆結果:初始化本地倉庫
--創建遠程倉庫別名 Layne@LAPTOP-Layne (master) $ git remote -v |
MINGW64 |
/d/Git-Space/pro-linghuchong/git-shTest |
origin https://github.com/atguiguyueyue/git-shTest.git (fetch) origin https://github.com/atguiguyueyue/git-shTest.git (push) |
|
小結:clone 會做如下操作。1、拉取代碼。2、初始化本地倉庫。3、創建別名
-
-
- 邀請加入團隊
-
- 選擇邀請合作者
- 填入想要合作的人
|
3 ) 復 制 地 址 并 通 過 微 信 釘 釘 等 方 式 發 送 給 該 用 戶 , 復 制 內 容 如 下 :
https://github.com/atguiguyueyue/git-shTest/invitations
|
- 在 atguigulinghuchong 這個賬號中的地址欄復制收到邀請的鏈接,點擊接受邀請。
- 成功之后可以在 atguigulinghuchong 這個賬號上看到 git-Test 的遠程倉庫。
|
- 令狐沖可以修改內容并 push 到遠程倉庫。
--編輯 clone 下來的文件 Layne@LAPTOP-Layne?? MINGW64?? /d/Git-Space/pro-linghuchong/git-shTest (master) $ vim hello.txt |
Layne@LAPTOP-Layne (master) $ cat hello.txt |
MINGW64 |
/d/Git-Space/pro-linghuchong/git-shTest |
hello git! hello atguigu! 2222222222222 hello git! hello atguigu! 33333333333333 hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! 我是最帥的,比岳不群還帥 hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! master test hello git! hello atguigu! hot-fix test --將編輯好的文件添加到暫存區 Layne@LAPTOP-Layne? MINGW64? /d/Git-Space/pro-linghuchong/git-shTest |
|
(master) $ git add hello.txt --將暫存區的文件上傳到本地庫 Layne@LAPTOP-Layne?? MINGW64?? /d/Git-Space/pro-linghuchong/git-shTest (master) $ git commit -m "lhc commit" hello.txt [master 5dabe6b] lhc commit 1 file changed, 1 insertion(+), 1 deletion(-) --將本地庫的內容 push 到遠程倉庫 Layne@LAPTOP-Layne?? MINGW64?? /d/Git-Space/pro-linghuchong/git-shTest (master) $ git push origin master Logon failed, use ctrl+c to cancel basic credential prompt. Username for 'https://github.com': atguigulinghuchong Counting objects: 3, done. Delta compression using up to 12 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 309 bytes | 309.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/atguiguyueyue/git-shTest.git 7cb4d02..5dabe6b master -> master |
- 回到 atguiguyueyue 的 GitHub 遠程倉庫中可以看到,最后一次是 lhc 提交的。
|
-
-
- 拉取遠程庫內容
-
- 基本語法
git pull 遠程庫地址別名 遠程分支名
- 案例實操
--將遠程倉庫對于分支最新內容拉下來后與當前本地分支直接合并 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git pull ori master
remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 Unpacking objects: 100% (3/3), done.
From? https://github.com/atguiguyueyue/git-shTest
* branch????????? master??? -> FETCH_HEAD 7cb4d02..5dabe6b master???????????????????? -> ori/master
Updating 7cb4d02..5dabe6b Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-) Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ cat hello.txt
hello git! hello atguigu! 2222222222222 hello git! hello atguigu! 33333333333333 hello git! hello atguigu!
hello git! hello atguigu!
hello git! hello atguigu! 我是最帥的,比岳不群還帥
hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu! hello git! hello atguigu!
hello git! hello atguigu! master test hello git! hello atguigu! hot-fix test
-
- 跨團隊協作
- 將遠程倉庫的地址復制發給邀請跨團隊協作的人,比如東方不敗。
-
在東方不敗的 GitHub 賬號里的地址欄復制收到的鏈接,然后點擊 Fork 將項目叉到自己的本地倉庫。
叉入中…
|
叉成功后可以看到當前倉庫信息。
- 東方不敗就可以在線編輯叉取過來的文件。
|
- 編輯完畢后,填寫描述信息并點擊左下角綠色按鈕提交。
|
- 接下來點擊上方的 Pull 請求,并創建一個新的請求。
|
|
- 回到岳岳 GitHub 賬號可以看到有一個 Pull request 請求。
|
進入到聊天室,可以討論代碼相關內容。
|
- 如果代碼沒有問題,可以點擊 Merge pull reque 合并代碼。
|
-
- SSH 免密登錄
我們可以看到遠程倉庫中還有一個 SSH 的地址,因此我們也可以使用 SSH 進行訪問。
|
具體操作如下:
--進入當前用戶的家目錄 Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ cd --刪除.ssh 目錄 Layne@LAPTOP-Layne MINGW64 ~ $ rm -rvf .ssh removed '.ssh/known_hosts' |
|
removed directory '.ssh'
--運行命令生成.ssh 秘鑰目錄[注意:這里-C 這個參數是大寫的 C] Layne@LAPTOP-Layne MINGW64 ~
$ ssh-keygen -t rsa -C atguiguyueyue@aliyun.com Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Layne/.ssh/id_rsa): Created directory '/c/Users/Layne/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Layne/.ssh/id_rsa. Your public key has been saved in /c/Users/Layne/.ssh/id_rsa.pub. The key fingerprint is: SHA256:7CPfRLITKcYDhaqpEDeok7Atvwh2reRmpxxOC6dkY44 atguiguyueyue@aliyun.com
The key's randomart image is:
+---[RSA 2048]--- +
|??? ..???????? |
|?? ..???????? |
| . ..????????? |
|+ + o . .????? |
|oO . = S .???? |
|X . .. + =????? |
|+@ * .. = .???? |
|X.&o+. o =????? |
|Eo+Oo?? . .???? |
+----[SHA256]----- +
--進入.ssh 目錄查看文件列表
Layne@LAPTOP-Layne MINGW64 ~
$ cd .ssh
Layne@LAPTOP-Layne MINGW64 ~/.ssh
$ ll -a total 21
drwxr-xr-x 1 Layne 197609??? 0 11 月 25 19:27 ./
drwxr-xr-x 1 Layne 197609??? 0 11 月 25 19:27 ../
-rw-r--r-- 1 Layne 197609 1679 11 月 25 19:27 id_rsa
-rw-r--r-- 1 Layne 197609 406 11 月 25 19:27 id_rsa.pub
--查看 id_rsa.pub 文件內容
Layne@LAPTOP-Layne MINGW64 ~/.ssh
$ cat id_rsa.pub ssh-rsa
AAAAB3NzaC1yc2EAAAADAQABAAABAQDRXRsk9Ohtg1AXLltsuNRAGBsx3ypE1O1Rkdzpm l1woa6y6G62lZri3XtCH0F7GQvnMvQtPISJFXXWo+jFHZmqYQa/6kOIMv2sszcoj2Qtwl lGXTPn/4T2h/cHjSHfc+ks8OYP7OWOOefpOCbYY/7DWYrl89k7nQlfd+A1FV/vQmcsa1L P5ihqjpjms2CoUUen8kZHbjwHBAHQHWRE+Vc371MG/dwINvCi8n7ibI86o2k0dW0+8SL+ svPV/Y0G9m+RAqgec8b9U6DcSSAMH5uq4UWfnAcUNagb/aJQLytrH0pLa8nMv3XdSGNNo AGBFeW2+K81XrmkP27FrLI6lDef atguiguyueyue@aliyun.com
復制 id_rsa.pub 文件內容,登錄GitHub,點擊用戶頭像→Settings→SSH and GPG keys
|
接下來再往遠程倉庫push 東西的時候使用 SSH 連接就不需要登錄了。
第 7 章 IDEA 集成 Git
-
- 配置 Git 忽略文件
- Eclipse 特定文件
|
- IDEA 特定文件
- Maven 工程的 target 目錄
|
問題 1:為什么要忽略他們?
答:與項目的實際功能無關,不參與服務器上部署運行。把它們忽略掉能夠屏蔽 IDE 工具之間的差異。
問題 2:怎么忽略?
- 創建忽略規則文件xxxx.ignore(前綴名隨便起,建議是 git.ignore)
這個文件的存放位置原則上在哪里都可以,為了便于讓~/.gitconfig 文件引用,建議也放在用戶家目錄下
git.ignore 文件模版內容如下:
# Compiled class file *.class |
|
# Log file *.log # BlueJ files *.ctxt # Mobile Tools for Java (J2ME) .mtj.tmp/ # Package Files # *.jar *.war *.nar *.ear *.zip *.tar.gz *.rar |
# |
virtual |
machine |
crash |
logs, |
see |
http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* .classpath .project .settings target .idea *.iml |
- 在.gitconfig 文件中引用忽略配置文件(此文件在Windows 的家目錄中)
[user] name = Layne email = Layne@atguigu.com [core] excludesfile = C:/Users/asus/git.ignore 注意:這里要使用“正斜線(/)”,不要使用“反斜線(\)” |
|
-
- 定位 Git 程序
|
-
- 初始化本地庫
|
選擇要創建Git 本地倉庫的工程。
-
- 添加到暫存區
右鍵點擊項目選擇Git -> Add 將項目添加到暫存區。
-
- 提交到本地庫
|
-
- 切換版本
在 IDEA 的左下角,點擊 Version Control,然后點擊Log 查看版本
|
右鍵選擇要切換的版本,然后在菜單里點擊Checkout Revision。
|
-
- 創建分支
選擇Git,在Repository 里面,點擊 Branches 按鈕。
|
在彈出的Git Branches 框里,點擊 New Branch 按鈕。
|
填寫分支名稱,創建 hot-fix 分支。
|
然后再 IDEA 的右下角看到 hot-fix,說明分支創建成功,并且當前已經切換成 hot-fix 分
支
|
-
- 切換分支
在 IDEA 窗口的右下角,切換到 master 分支。
然后在 IDEA 窗口的右下角看到了 master,說明 master 分支切換成功。
|
-
- 合并分支
在 IDEA 窗口的右下角,將 hot-fix 分支合并到當前master 分支。
|
如果代碼沒有沖突,分支直接合并成功,分支合并成功以后,代碼自動提交,無需手動提交本地庫。
-
- 解決沖突
|
如圖所示,如果 master 分支和 hot-fix 分支都修改了代碼,在合并分支的時候就會發生沖突。
我們現在站在 master 分支上合并hot-fix 分支,就會發生代碼沖突。
|
點擊Conflicts 框里的 Merge 按鈕,進行手動合并代碼。
手動合并完代碼以后,點擊右下角的 Apply 按鈕。
|
代碼沖突解決,自動提交本地庫。
|
第 8 章 IDEA 集成 GitHub
-
- 設置 GitHub 賬號
|
如果出現 401 等情況連接不上的,是因為網絡原因,可以使用以下方式連接:
|
然后去 GitHub 賬戶上設置 token。
?????????
|
點擊生成token。
|
復制紅框中的字符串到idea 中。
|
點擊登錄。
-
- 分享工程到 GitHub
|
來到GitHub 中發現已經幫我們創建好了 gitTest 的遠程倉庫。
|
-
- push? 推送本地庫到遠程庫
右鍵點擊項目,可以將當前分支的內容 push 到 GitHub 的遠程倉庫中。
|
|
注意:push 是將本地庫代碼推送到遠程庫,如果本地庫代碼跟遠程庫代碼版本不一致, push 的操作是會被拒絕的。也就是說,要想 push 成功,一定要保證本地庫的版本要比遠程庫的版本高!因此一個成熟的程序員在動手改本地代碼之前,一定會先檢查下遠程庫跟本地代碼的區別!如果本地的代碼版本已經落后,切記要先 pull 拉取一下遠程庫的代碼,將本地代碼更新到最新以后,然后再修改,提交,推送!
-
- pull 拉取遠程庫到本地庫
右鍵點擊項目,可以將遠程倉庫的內容 pull 到本地倉庫。
|
注意:pull 是拉取遠端倉庫代碼到本地,如果遠程庫代碼和本地庫代碼不一致,會自動合并,如果自動合并失敗,還會涉及到手動解決沖突的問題。
-
- clone 克隆遠程庫到本地
|
為 clone 下來的項目創建一個工程,然后點擊 Next。
|
|
第 9 章 國內代碼托管中心-碼云
-
- 簡介
眾所周知,GitHub 服務器在國外,使用 GitHub 作為項目托管網站,如果網速不好的話,嚴重影響使用體驗,甚至會出現登錄不上的情況。針對這個情況,大家也可以使用國內的項目托管網站-碼云。
碼云是開源中國推出的基于 Git 的代碼托管服務中心,網址是 https://gitee.com/ ,使用方式跟 GitHub 一樣,而且它還是一個中文網站,如果你英文不是很好它是最好的選擇。
-
- 碼云帳號注冊和登錄
進入碼云官網地址:Gitee - 基于 Git 的代碼托管和研發協作平臺,點擊注冊 Gitee
輸入個人信息,進行注冊即可。
|
帳號注冊成功以后,直接登錄。
登錄以后,就可以看到碼云官網首頁了。
|
-
- 碼云創建遠程庫
點擊首頁右上角的加號,選擇下面的新建倉庫
填寫倉庫名稱,路徑和選擇是否開源(共開庫或私有庫)
|
最后根據需求選擇分支模型,然后點擊創建按鈕。
遠程庫創建好以后,就可以看到 HTTPS 和 SSH 的鏈接。
|
-
- IDEA 集成碼云
- IDEA 安裝碼云插件
- IDEA 集成碼云
Idea 默認不帶碼云插件,我們第一步要安裝Gitee 插件。
如圖所示,在 Idea 插件商店搜索 Gitee,然后點擊右側的 Install 按鈕。
Idea 鏈接碼云和鏈接GitHub 幾乎一樣,安裝成功后,重啟 Idea。
|
Idea 重啟以后在 Version Control 設置里面看到Gitee,說明碼云插件安裝成功。
然后在碼云插件里面添加碼云帳號,我們就可以用 Idea 連接碼云了。
|
-
-
- IDEA 連接碼云
-
Idea 連接碼云和連接 GitHub 幾乎一樣,首先在 Idea 里面創建一個工程,初始化 git 工程,然后將代碼添加到暫存區,提交到本地庫,這些步驟上面已經講過,此處不再贅述。
-
-
-
- 將本地代碼 push 到碼云遠程庫
-
-
|
自定義遠程庫鏈接。
|
給遠程庫鏈接定義個 name,然后再 URL 里面填入碼云遠程庫的 HTTPS 鏈接即可。碼
云服務器在國內,用HTTPS 鏈接即可,沒必要用SSH 免密鏈接。
|
然后選擇定義好的遠程鏈接,點擊 Push 即可。
|
看到提示就說明 Push 遠程庫成功。
去碼云遠程庫查看代碼。
|
只要碼云遠程庫鏈接定義好以后,對碼云遠程庫進行 pull 和 clone 的操作和 Github 一致,此處不再贅述。
-
- 碼云復制 GitHub 項目
碼云提供了直接復制 GitHub 項目的功能,方便我們做項目的遷移和下載。具體操作如下:
將 GitHub 的遠程庫HTTPS 鏈接復制過來,點擊創建按鈕即可。
|
如果GitHub 項目更新了以后,在碼云項目端可以手動重新同步,進行更新!
|
第 10 章 自建代碼托管平臺-GitLab
-
- GitLab 簡介
GitLab 是由GitLabInc.開發,使用 MIT 許可證的基于網絡的 Git 倉庫管理工具,且具有 wiki 和 issue 跟蹤功能。使用 Git 作為代碼管理工具,并在此基礎上搭建起來的web 服務。 GitLab 由烏克蘭程序員 DmitriyZaporozhets 和 ValerySizov 開發,它使用 Ruby 語言寫
成。后來,一些部分用 Go 語言重寫。截止 2018 年 5 月,該公司約有 290 名團隊成員,以及 2000 多名開源貢獻者。GitLab 被 IBM,Sony,JülichResearchCenter,NASA,Alibaba, Invincea,O’ReillyMedia,Leibniz-Rechenzentrum(LRZ),CERN,SpaceX 等組織使用。
-
- GitLab 官網地址
官網地址:https://about.gitlab.com/
安裝說明:https://about.gitlab.com/installation/
-
- GitLab 安裝
- 服務器準備
- GitLab 安裝
準備一個系統為 CentOS7 以上版本的服務器,要求內存 4G,磁盤 50G。關閉防火墻,并且配置好主機名和 IP,保證服務器可以上網。
此教程使用虛擬機:主機名:gitlab-server??? IP 地址:192.168.6.200
-
-
- 安裝包準備
-
Yum 在線安裝 gitlab- ce 時,需要下載幾百M 的安裝文件,非常耗時,所以最好提前把所需RPM 包下載到本地,然后使用離線 rpm 的方式安裝。
下載地址:
https://packages.gitlab.com/gitlab/gitlab- ce/packages/el/7/gitlab-ce-13.10.2-ce.0.el7.x86_64.rpm |
|
注:資料里提供了此rpm 包,直接將此包上傳到服務器/opt/module 目錄下即可。
-
-
- 編寫安裝腳本
-
安裝 gitlab 步驟比較繁瑣,因此我們可以參考官網編寫 gitlab 的安裝腳本。
[root@gitlab-server module]# vim gitlab-install.sh sudo rpm -ivh /opt/module/gitlab-ce-13.10.2-ce.0.el7.x86_64.rpm sudo yum install -y curl policycoreutils-python openssh-server cronie |
|
|
[root@gitlab-server module]# chmod +x gitlab-install.sh [root@gitlab-server module]# ll 總用量 403104 -rw-r--r--. 1 root root 412774002 4 月 7 15:47 gitlab-ce-13.10.2- ce.0.el7.x86_64.rpm -rwxr-xr-x. 1 root root????? 416 4 月?? 7 15:49 gitlab-install.sh |
|
然后執行該腳本,開始安裝 gitlab-ce。注意一定要保證服務器可以上網。
[root@gitlab-server module]# ./gitlab-install.sh 警告:/opt/module/gitlab-ce-13.10.2-ce.0.el7.x86_64.rpm: 頭 V4 RSA/SHA1 Signature, 密鑰 ID f27eab47: NOKEY 準備中...??????????????????? ################################# [100%] 正在升級/安裝... 1:gitlab-ce-13.10.2-ce.0.el7 ################################# [100%] 。 。 。 。 。 。 |
|
-
-
- 初始化 GitLab 服務
-
執行以下命令初始化 GitLab 服務,過程大概需要幾分鐘,耐心等待…
[root@gitlab-server module]# gitlab-ctl reconfigure 。 。 。 。 。 。 Running handlers: Running handlers complete Chef Client finished, 425/608 resources updated in 03 minutes 08 seconds gitlab Reconfigured! |
|
-
-
- 啟動 GitLab 服務
-
執行以下命令啟動 GitLab 服務,如需停止,執行 gitlab-ctl stop
[root@gitlab-server module]# gitlab-ctl start ok: run: alertmanager: (pid 6812) 134s ok: run: gitaly: (pid 6740) 135s ok: run: gitlab-monitor: (pid 6765) 135s |
|
ok: run: gitlab-workhorse: (pid 6722) 136s ok: run: logrotate: (pid 5994) 197s ok: run: nginx: (pid 5930) 203s ok: run: node-exporter: (pid 6234) 185s ok: run: postgres-exporter: (pid 6834) 133s ok: run: postgresql: (pid 5456) 257s ok: run: prometheus: (pid 6777) 134s ok: run: redis: (pid 5327) 263s ok: run: redis-exporter: (pid 6391) 173s ok: run: sidekiq: (pid 5797) 215s ok: run: unicorn: (pid 5728) 221s |
-
-
- 使用瀏覽器訪問 GitLab
-
使用主機名或者IP 地址即可訪問GitLab 服務。需要提前配一下 windows 的hosts 文件。
|
首次登陸之前,需要修改下 GitLab 提供的 root 賬戶的密碼,要求 8 位以上,包含大小寫子母和特殊符號。因此我們修改密碼為 Atguigu.123456
然后使用修改后的密碼登錄 GitLab。
|
GitLab 登錄成功。
|
-
-
- GitLab 創建遠程庫
-
|
-
-
- IDEA 集成 GitLab
- 1)安裝 GitLab 插件
- IDEA 集成 GitLab
-
-
-
-
- 2)設置GitLab 插件
-
-
|
|
-
-
-
- 3)push 本地代碼到GitLab 遠程庫
-
-
自定義遠程連接
|
注意:gitlab 網頁上復制過來的連接是:http://gitlab.example.com/root/git-test.git,需要手動修改為:http://gitlab-server/root/git-test.git
選擇 gitlab 遠程連接,進行 push。
首次向連接 gitlab,需要登錄帳號和密碼,用 root 帳號和我們修改的密碼登錄即可。
|
代碼 Push 成功。
只要 GitLab 的遠程庫連接定義好以后,對 GitLab 遠程庫進行 pull 和 clone 的操作和
Github 和碼云一致,此處不再贅述。