Git

第1章 Git 概述

Git 是一個免費的、開源的分布式版本控制系統,可以快速高效地處理從小型到大型的各種項目。

Git 易于學習,占地面積小,性能極快。具有廉價的本地庫,方便的暫存區域和多個工作流分支等特性。其性能優于 SubversionCVSPerforce ClearCase 等版本控制工具。

    1. 何為版本控制

版本控制是一種記錄文件內容變化,以便將來查閱特定版本修訂情況的系統。


版本控制其實最重要的是可以記錄文件修改歷史記錄,從而讓用戶能夠查看歷史版本,方便版本切換。

    1. 為什么需要版本控制

個人開發過渡到團隊協作。

1

2

3

aaa

bbb ccc ddd

aaa

bbb222 ccc ddd

aaa

bbb222 ccc ddd444

服務器

aaa

bbb222 ccc ddd

aaa

bbb ccc

ddd444

    1. 版本控制工具
      • 集中式版本控制工具

CVS、SVN(Subversion)VSS……

集中化的版本控制系統諸如 CVS、SVN 等,都有一個單一的集中管理的服務器,保存所有文件的修訂版本,而協同工作的人們都通過客戶端連到這臺服務器,取出最新的文件或者提交更新。多年以來,這已成為版本控制系統的標準做法。

這種做法帶來了許多好處,每個人都可以在一定程度上看到項目中的其他人正在做些什么。而管理員也可以輕松掌控每個開發者的權限,并且管理一個集中化的版本控制系統,要遠比在各個客戶端上維護本地數據庫來得輕松容易。

事分兩面,有好有壞。這么做顯而易見的缺點是中央服務器的單點故障。如果服務器宕機一小時,那么在這一小時內,誰都無法提交更新,也就無法協同工作。

      • 分布式版本控制工具

GitMercurialBazaarDarcs……

像 Git 這種分布式版本控制工具,客戶端提取的不是最新版本的文件快照,而是把代碼倉庫完整地鏡像下來(本地庫)。這樣任何一處協同工作用的文件發生故障,事后都可以用其他客戶端的本地倉庫進行恢復。因為每個客戶端的每一次文件提取操作,實際上都是一次對整個文件倉庫的完整備份。

分布式的版本控制系統出現之后,解決了集中式版本控制系統的缺陷:

  1. 服務器斷網的情況下也可以進行開發(因為版本控制是在本地進行的)

  1. 每個客戶端保存的也都是整個完整的項目(包含歷史記錄,更加安全)

    1. Git 簡史

1991年

2002年

2005年

BitKeeper的東

BitMover公司出于人道主義精神,授權 Linux社區免費使用這個版本控制系統。但要求不能進行破解。

2008年

與開發者進行強有力的約束

Linux社區無法像

商業公司那樣對參

開發SambaAndrew

試圖破解BitKeeper的協議,被BitMover公司發現,要收回Linux區的免費使用權。

……….

GitHub上線

PHP

Ruby

一個月后Linux系統代碼由Git管理

jQuery

主體程序開發完成只用了兩周

Linus自己用C語言開發了一個分布式版本控制系統:Git

商業軟件:BitKeeper

Linus本人手動合并代碼

Linux系統版本控制歷史

?

    1. Git 工作機制

歷史版本

git commit

臨時存儲

git add

寫代碼

工作區

暫存區

本地庫

?

    1. Git 和代碼托管中心

代碼托管中心是基于網絡服務器的遠程代碼倉庫,一般我們簡單稱為遠程庫

      • 局域網
        • GitLab
      • 互聯網
        • GitHub(外網)
        • Gitee 碼云(國內網站)

第2章 Git 安裝

官網地址: https://git-scm.com/

查看GNU 協議,可以直接點擊下一步。

選擇Git 安裝位置,要求是非中文并且沒有空格的目錄,然后下一步。

文本框:

?

Git 選項配置,推薦默認設置,然后下一步。

文本框:

?

Git 安裝目錄名,不用修改,直接點擊下一步。

文本框:

?

Git 的默認編輯器,建議使用默認的 Vim 編輯器,然后點擊下一步。

文本框:

?

默認分支名設置,選擇讓Git 決定,分支名默認為 master,下一步。

文本框:

?

修改Git 的環境變量,選第一個,不修改環境變量,只在Git Bash 里使用Git。

文本框:

?

選擇后臺客戶端連接協議,選默認值 OpenSSL,然后下一步。

文本框:

?

配置 Git 文件的行末換行符,Windows 使用 CRLF,Linux 使用 LF,選擇第一個自動轉換,然后繼續下一步。

選擇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. 設置用戶簽名

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 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(或其他代碼托管中心)的賬號沒有任何關系。

    1. 初始化本地庫

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720

$ git init

Initialized empty Git repository in D:/Git-Space/SH0720/.git/

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

$ ll -a total 4

drwxr-xr-x 1 Layne 197609 0 11 25 14:07 ./

drwxr-xr-x 1 Layne 197609 0 11 25 14:07 ../

drwxr-xr-x 1 Layne 197609 0 11 25 14:07 .git/??? .git 初始化的效果,生成 git

1 )基本語法 git? init 2 )案例實操

3)結果查看

?

    1. 查看本地庫狀態

1基本語法

git status 2)案例實操

      1. 首次查看(工作區沒有任何文件)

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)

文本框: 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)

?

      1. 新增文件(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) $ 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!

?

      1. 再次查看(檢測到未追蹤的文件)

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 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)

?

    1. 添加暫存區
      1. 將工作區的文件添加到暫存區

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.

1 )基本語法 git add 文件名 2 )案例實操

      1. 查看狀態(檢測到暫存區有新文件)

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

文本框: 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

?

    1. 提交本地庫
      1. 將暫存區的文件提交到本地庫
  1. 基本語法

git commit -m "日志信息" 文件名

  1. 案例實操

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 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

?

      1. 查看狀態(沒有文件需要提交)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

$ git status

On branch master

nothing to commit, working tree clean

文本框: Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git status On branch master nothing to commit, working tree clean

?

    1. 修改文件(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) $ 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!

?

      1. 查看狀態(檢測到工作區有文件被修改)

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 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")

?

      1. 將修改的文件再次添加暫存區

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 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.

?

      1. 查看狀態(工作區的修改添加到了暫存區)

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

文本框: 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. 歷史版本
      1. 查看歷史版本

1基本語法

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

git reflog?? 查看版本信息 git log???? 查看版本詳細信息 2 )案例實操

      1. 版本穿梭
  1. 基本語法

git reset --hard 版本號

  1. 案例實操

--首先查看當前的歷史記錄,可以看到當前是在 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!

文本框: --首先查看當前的歷史記錄,可以看到當前是在 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!

文本框: 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分支]

服務器

部署

服務器

部署

提交

用戶

運維

測試

?

    1. 什么是分支

hot-fix

master

feature-blue

feature-game


在版本控制過程中,同時推進多個任務,為每個任務,我們就可以創建每個任務的單獨分支。使用分支意味著程序員可以把自己的工作從開發主線上分離開來,開發自己分支的時候,不會影響主線分支的運行。對于初學者而言,分支可以簡單理解為副本,一個分支就是一個單獨的副本。(分支底層其實也是指針的引用)

    1. 分支的好處

同時并行推進多個功能開發,提高開發效率。

各個分支在開發過程中,如果某一個分支開發失敗,不會對其他分支有任何影響。失敗的分支刪除重新開始即可。

    1. 分支的操作

命令名稱

git branch 分支名

創建分支

git branch -v

查看分支

git checkout 分支名

切換分支

git merge 分支名

把指定的分支合并到當前分支上

      1. 查看分支

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

$ git branch -v

* master 087a1a7 my third commit *代表當前所在的分區)

1 )基本語法 git branch -v 2 )案例實操

      1. 創建分支
  1. 基本語法

git branch 分支名

  1. 案例實操

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

文本框: 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

?

      1. 修改分支

--maste 分支上做修改

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

$ vim hello.txt

--添加暫存區

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

文本框: --在 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!

      1. 切換分支
  1. 基本語法

git checkout 分支名

  1. 案例實操

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!

文本框: 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

      1. 合并分支
  1. 基本語法

git merge 分支名

  1. 案例實操 在 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.

文本框: 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.

?

      1. 產生沖突

沖突產生的表現:后面狀態為 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")

文本框: 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")

?

      1. 解決沖突
  1. 編輯有沖突的文件,刪除特殊符號,決定要使用的內容

特殊符號:<<<<<<< HEAD 當前分支的代碼 =======? 合并過來的代碼 >>>>>>> hot-fix

hello git! hello atguigu! 2222222222222 hello git! hello atguigu! 3333333333333 hello git! hello atguigu!

文本框: 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

文本框: 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

  1. 添加到暫存區

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)

$ git add hello.txt

文本框: Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING) $ git add hello.txt

?

  1. 執行提交(注意:此時使用 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)

$

文本框: 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) $

?

    1. 創建分支和切換分支圖解

first

second

third

fourth

master

head

?

master、hot-fix 其實都是指向具體版本記錄的指針。當前所在的分支,其實是由 HEAD

決定的。所以創建分支的本質就是多創建一個指針。

HEAD 如果指向 master,那么我們現在就在master 分支上。

HEAD 如果執行 hotfix,那么我們現在就在hotfix 分支上。

所以切換分支的本質就是移動HEAD 指針。

第 5 章 Git 團隊協作機制

    1. 代碼托管中心

      pull

      push

      clone

      push

      本地庫

      本地庫

      遠程庫

      團隊內協作

    1. 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

注:此三個賬號為講師使用賬號,同學請自行注冊,然后三個同學為一組進行團隊協作!

    1. 創建遠程倉庫

?

    1. 遠程倉庫操作

命令名稱

git remote -v

查看當前所有遠程地址別名

git remote add 別名 遠程地址

起別名

git push 別名 分支

推送本地分支上的內容到遠程倉庫

git clone 遠程地址

將遠程倉庫的內容克隆到本地

git pull 遠程庫地址別名 遠程分支名

將遠程倉庫對于分支最新內容拉下來后與

當前本地分支直接合并

      1. 創建遠程倉庫別名

1基本語法

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

$ git remote -v

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

$ git remote add ori https://github.com/atguiguyueyue/git-shTest.git

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)

$ git remote -v

ori??? https://github.com/atguiguyueyue/git-shTest.git (fetch) ori??? https://github.com/atguiguyueyue/git-shTest.git (push)

git remote -v 查看當前所有遠程地址別名 git remote add? 別名? 遠程地址 2 )案例實操

https://github.com/atguiguyueyue/git-shTest.git

這個地址在創建完遠程倉庫后生成的連接,如圖所示紅框中

?

      1. 推送本地分支到遠程倉庫
  1. 基本語法

git push 別名 分支

  1. 案例實操

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

文本框: 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 創建的遠程倉庫。

?

      1. 克隆遠程倉庫到本地
  1. 基本語法

git clone 遠程地址

  1. 案例實操

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.

文本框: 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、創建別名

      1. 邀請加入團隊
  1. 選擇邀請合作者

  1. 填入想要合作的人

?

3 ) 復 制 地 址 并 通 過 微 信 釘 釘 等 方 式 發 送 給 該 用 戶 , 復 制 內 容 如 下 :

https://github.com/atguiguyueyue/git-shTest/invitations

?

  1. atguigulinghuchong 這個賬號中的地址欄復制收到邀請的鏈接,點擊接受邀請。

  1. 成功之后可以在 atguigulinghuchong 這個賬號上看到 git-Test 的遠程倉庫。

?

  1. 令狐沖可以修改內容并 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

文本框: (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

  1. 回到 atguiguyueyue GitHub 遠程倉庫中可以看到,最后一次是 lhc 提交的。

?

      1. 拉取遠程庫內容
  1. 基本語法

git pull 遠程庫地址別名 遠程分支名

  1. 案例實操

--將遠程倉庫對于分支最新內容拉下來后與當前本地分支直接合并 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

    1. 跨團隊協作
  1. 將遠程倉庫的地址復制發給邀請跨團隊協作的人,比如東方不敗。

  1. 在東方不敗的 GitHub 賬號里的地址欄復制收到的鏈接,然后點擊 Fork 將項目叉到自己的本地倉庫。

叉入中

?

叉成功后可以看到當前倉庫信息。

  1. 東方不敗就可以在線編輯叉取過來的文件。

?

  1. 編輯完畢后,填寫描述信息并點擊左下角綠色按鈕提交。

?

  1. 接下來點擊上方的 Pull 請求,并創建一個新的請求。

?

?

  1. 回到岳岳 GitHub 賬號可以看到有一個 Pull request 請求。

?

進入到聊天室,可以討論代碼相關內容。

?

  1. 如果代碼沒有問題,可以點擊 Merge pull reque 合并代碼。

?

    1. 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'

文本框: --進入當前用戶的家目錄 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

    1. 配置 Git 忽略文件
  1. Eclipse 特定文件

?

  1. IDEA 特定文件

  1. Maven 工程的 target 目錄

?

問題 1:為什么要忽略他們?

答:與項目的實際功能無關,不參與服務器上部署運行。把它們忽略掉能夠屏蔽 IDE 工具之間的差異。

問題 2:怎么忽略?

  1. 創建忽略規則文件xxxx.ignore(前綴名隨便起,建議是 git.ignore

這個文件的存放位置原則上在哪里都可以,為了便于讓~/.gitconfig 文件引用,建議也放在用戶家目錄下

git.ignore 文件模版內容如下:

# Compiled class file

*.class

文本框: # 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

  1. 在.gitconfig 文件中引用忽略配置文件(此文件在Windows 的家目錄中)

[user]

name = Layne

email = Layne@atguigu.com [core]

excludesfile = C:/Users/asus/git.ignore

注意:這里要使用“正斜線(/”,不要使用“反斜線(\)”

文本框: [user] name = Layne email = Layne@atguigu.com [core] excludesfile = C:/Users/asus/git.ignore 注意:這里要使用“正斜線(/)”,不要使用“反斜線(\)”

?

    1. 定位 Git 程序

?

    1. 初始化本地庫

?

選擇要創建Git 本地倉庫的工程。

    1. 添加到暫存區

右鍵點擊項目選擇Git -> Add 將項目添加到暫存區。

    1. 提交到本地庫

?

    1. 切換版本

在 IDEA 的左下角,點擊 Version Control,然后點擊Log 查看版本

?

右鍵選擇要切換的版本,然后在菜單里點擊Checkout Revision。

?

    1. 創建分支

選擇Git,在Repository 里面,點擊 Branches 按鈕。

?

在彈出的Git Branches 框里,點擊 New Branch 按鈕。

?

填寫分支名稱,創建 hot-fix 分支。

?

然后再 IDEA 的右下角看到 hot-fix,說明分支創建成功,并且當前已經切換成 hot-fix 分

?

    1. 切換分支

在 IDEA 窗口的右下角,切換到 master 分支。

然后在 IDEA 窗口的右下角看到了 master,說明 master 分支切換成功。

?

    1. 合并分支

在 IDEA 窗口的右下角,將 hot-fix 分支合并到當前master 分支。

?

如果代碼沒有沖突,分支直接合并成功,分支合并成功以后,代碼自動提交,無需手動提交本地庫。

    1. 解決沖突


如圖所示,如果 master 分支和 hot-fix 分支都修改了代碼,在合并分支的時候就會發生沖突。

我們現在站在 master 分支上合并hot-fix 分支,就會發生代碼沖突。

?

點擊Conflicts 框里的 Merge 按鈕,進行手動合并代碼。

手動合并完代碼以后,點擊右下角的 Apply 按鈕。

?

代碼沖突解決,自動提交本地庫。

?

第 8 章 IDEA 集成 GitHub

    1. 設置 GitHub 賬號

?

如果出現 401 等情況連接不上的,是因為網絡原因,可以使用以下方式連接:

?

然后去 GitHub 賬戶上設置 token。

?????????

?

點擊生成token。

?

復制紅框中的字符串到idea 中。

?

點擊登錄。

    1. 分享工程到 GitHub

?

來到GitHub 中發現已經幫我們創建好了 gitTest 的遠程倉庫。

?

    1. push? 推送本地庫到遠程庫

右鍵點擊項目,可以將當前分支的內容 push 到 GitHub 的遠程倉庫中。

?

?

注意:push 是將本地庫代碼推送到遠程庫,如果本地庫代碼跟遠程庫代碼版本不一致, push 的操作是會被拒絕的。也就是說,要想 push 成功,一定要保證本地庫的版本要比遠程庫的版本高!因此一個成熟的程序員在動手改本地代碼之前,一定會先檢查下遠程庫跟本地代碼的區別!如果本地的代碼版本已經落后,切記要先 pull 拉取一下遠程庫的代碼,將本地代碼更新到最新以后,然后再修改,提交,推送!

    1. pull 拉取遠程庫到本地庫

右鍵點擊項目,可以將遠程倉庫的內容 pull 到本地倉庫。

?

注意:pull 是拉取遠端倉庫代碼到本地,如果遠程庫代碼和本地庫代碼不一致,會自動合并,如果自動合并失敗,還會涉及到手動解決沖突的問題。

    1. clone 克隆遠程庫到本地

?

為 clone 下來的項目創建一個工程,然后點擊 Next。

文本框:

?

第 9 章 國內代碼托管中心-碼云

    1. 簡介

眾所周知,GitHub 服務器在國外,使用 GitHub 作為項目托管網站,如果網速不好的話,嚴重影響使用體驗,甚至會出現登錄不上的情況。針對這個情況,大家也可以使用國內的項目托管網站-碼云。

碼云是開源中國推出的基于 Git 的代碼托管服務中心,網址是 https://gitee.com/ ,使用方式跟 GitHub 一樣,而且它還是一個中文網站,如果你英文不是很好它是最好的選擇。

    1. 碼云帳號注冊和登錄

進入碼云官網地址:Gitee - 基于 Git 的代碼托管和研發協作平臺,點擊注冊 Gitee

輸入個人信息,進行注冊即可。

?

帳號注冊成功以后,直接登錄。

登錄以后,就可以看到碼云官網首頁了。

?

    1. 碼云創建遠程庫

點擊首頁右上角的加號,選擇下面的新建倉庫

填寫倉庫名稱,路徑和選擇是否開源(共開庫或私有庫)

?

最后根據需求選擇分支模型,然后點擊創建按鈕。

遠程庫創建好以后,就可以看到 HTTPS 和 SSH 的鏈接。

?

    1. IDEA 集成碼云
      1. IDEA 安裝碼云插

Idea 默認不帶碼云插件,我們第一步要安裝Gitee 插件。

如圖所示,在 Idea 插件商店搜索 Gitee,然后點擊右側的 Install 按鈕。

Idea 鏈接碼云和鏈接GitHub 幾乎一樣,安裝成功后,重啟 Idea。

?

Idea 重啟以后在 Version Control 設置里面看到Gitee,說明碼云插件安裝成功。

然后在碼云插件里面添加碼云帳號,我們就可以用 Idea 連接碼云了。

?

      1. IDEA 連接碼云

Idea 連接碼云和連接 GitHub 幾乎一樣,首先在 Idea 里面創建一個工程,初始化 git 工程,然后將代碼添加到暫存區,提交到本地庫,這些步驟上面已經講過,此處不再贅述。

        • 將本地代碼 push 到碼云遠程庫

?

自定義遠程庫鏈接。

?

給遠程庫鏈接定義個 name,然后再 URL 里面填入碼云遠程庫的 HTTPS 鏈接即可。碼

云服務器在國內,用HTTPS 鏈接即可,沒必要用SSH 免密鏈接。

?

然后選擇定義好的遠程鏈接,點擊 Push 即可。

?

看到提示就說明 Push 遠程庫成功。

去碼云遠程庫查看代碼。

?

只要碼云遠程庫鏈接定義好以后,對碼云遠程庫進行 pull 和 clone 的操作和 Github 一致,此處不再贅述。

    1. 碼云復制 GitHub 項目

碼云提供了直接復制 GitHub 項目的功能,方便我們做項目的遷移和下載。具體操作如下:

將 GitHub 的遠程庫HTTPS 鏈接復制過來,點擊創建按鈕即可。

?

如果GitHub 項目更新了以后,在碼云項目端可以手動重新同步,進行更新!

?

第 10 章 自建代碼托管平臺-GitLab

    1. 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 等組織使用。

    1. GitLab 官網地址

官網地址:https://about.gitlab.com/

安裝說明:https://about.gitlab.com/installation/

    1. GitLab 安裝
      1. 服務器準備

準備一個系統為 CentOS7 以上版本的服務器,要求內存 4G,磁盤 50G。關閉防火墻,并且配置好主機名和 IP,保證服務器可以上網。

此教程使用虛擬機:主機名:gitlab-server??? IP 地址:192.168.6.200

      1. 安裝包準備

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

文本框: https://packages.gitlab.com/gitlab/gitlab- ce/packages/el/7/gitlab-ce-13.10.2-ce.0.el7.x86_64.rpm

?

注:資料里提供了此rpm 包,直接將此包上傳到服務器/opt/module 目錄下即可。

      1. 編寫安裝腳本

安裝 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]# 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

?

sudo lokkit -s http -s ssh sudo yum install -y postfix sudo service postfix start sudo chkconfig postfix on

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab- ce/script.rpm.sh | sudo bash

sudo EXTERNAL_URL="http://gitlab.example.com" yum -y install gitlab- ce

給腳本增加執行權限

[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

文本框: [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%]

。 。 。 。 。 。

文本框: [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%] 。 。 。 。 。 。

?

      1. 初始化 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!

文本框: [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!

?

      1. 啟動 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

文本框: [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

文本框: 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

      1. 使用瀏覽器訪問 GitLab

使用主機名或者IP 地址即可訪問GitLab 服務。需要提前配一下 windows 的hosts 文件。

?

首次登陸之前,需要修改下 GitLab 提供的 root 賬戶的密碼,要求 8 位以上,包含大小寫子母和特殊符號。因此我們修改密碼為 Atguigu.123456

然后使用修改后的密碼登錄 GitLab。

?

GitLab 登錄成功。

?

      1. GitLab 創建遠程庫

?

      1. IDEA 集成 GitLab
        • 1)安裝 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 和碼云一致,此處不再贅述。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/207560.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/207560.shtml
英文地址,請注明出處:http://en.pswp.cn/news/207560.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

系統設計之數據庫

為您的項目選擇正確的數據庫是一項復雜的任務。許多數據庫選項都適合不同的用例&#xff0c;很快就會導致決策疲勞。 我們希望這份備忘單提供高級指導&#xff0c;以找到符合您項目需求的正確服務并避免潛在的陷阱。 注意&#xff1a;Google 關于其數據庫用例的文檔有限。盡管…

軟件測試卷王的自述,我難道真的很卷?

前言 前段時間去面試了一個公司&#xff0c;成功拿到了offer&#xff0c;薪資也從12k漲到了18k&#xff0c;對于工作都還沒兩年的我來說&#xff0c;還是比較滿意的&#xff0c;畢竟一些工作3、4年的可能還沒我高。 我可能就是大家說的卷王&#xff0c;感覺自己年輕&#xff…

北郵22級信通院數電:Verilog-FPGA(12)第十二周實驗(2)彩虹呼吸燈(bug已解決 更新至3.0)

北郵22信通一枚~ 跟隨課程進度更新北郵信通院數字系統設計的筆記、代碼和文章 持續關注作者 迎接數電實驗學習~ 獲取更多文章&#xff0c;請訪問專欄&#xff1a; 北郵22級信通院數電實驗_青山如墨雨如畫的博客-CSDN博客 目錄 一.代碼部分 1.1一些更新和講解 1.2改正后的…

解密HubSpot CMS Hub:構建引人入勝的企業網站!

在數字化時代&#xff0c;網站是企業與客戶互動的重要窗口。為了在競爭激烈的市場中脫穎而出&#xff0c;企業需要一個現代化、用戶友好且高度可定制的網站。而HubSpot CMS Hub作為一款領先的內容管理系統&#xff0c;為企業提供了獨特的優勢&#xff0c;讓網站建設變得更加輕松…

Private Set Intersection from Pseudorandom CorrelationGenerators 最快PSI!導覽解讀

目錄 一、概述 二、相關介紹 三、性能對比 四、技術細節 1.KKRT 2.Pseudorandom Correlation Generators 3.A New sVOLE-Based BaRK-OPRF 4.BaRK-OPRF 五、總結 參考文獻 一、概述 這篇文章的主要脈絡和核心思想是探討如何利用偽隨機相關生成器&#xff08;PCG&#…

【AI】以大廠PaaS為例,看人工智能技術方案服務能力的方向(2/2)

目錄 三、解決方案 3.1 人臉身份驗證 3.2 圖像審核&#xff08;暴恐、色情等&#xff09; 3.3 人臉會場簽到 3.4 機器人視覺 3.5 視頻審核 3.6 電商圖文詳情生成 3.7 智能客服 接上回&#xff1a; 【AI】以大廠PaaS為例&#xff0c;看人工智能技術方案服務能力的方向&…

Mybatis實用教程之XML實現動態sql

系列文章目錄 1、mybatis簡介及數據庫連接池 2、mybatis中selectOne的使用 3、mybatis簡單使用 4、mybatis中resultMap結果集的使用 Mybatis實用教程之XML實現動態sql 系列文章目錄前言1. 動態條件查詢2. 動態更新語句3. 動態插入語句4、其他標簽的使用 前言 當編寫 MyBatis 中…

力扣labuladong——一刷day67

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄 前言一、力扣582.殺掉進程二、力扣536.從字符串生成二叉樹 前言 二叉樹的遞歸分為「遍歷」和「分解問題」兩種思維模式&#xff0c;這道題需要用到「遍歷」的思維模…

麒麟系統進入救援模式或者是crtl D界面排查方法

如出現以下圖片的情況可能需要修復磁盤&#xff1a; V10GFB-desktop&#xff1a; 開機后發現一致卡在此界面&#xff1a; 按esc鍵后有以下報錯信息說明在/etc/fstab里面編寫的外掛磁盤的命令有問題 解決方法如下&#xff1a;進入單用戶模式對/etc/fstab進行修改&#xff1a; …

springboot-mongodb-連接配置

文章目錄 配置Maven依賴URL格式單節點配置示例副本集&#xff08;含連接池配置&#xff09; 配置Maven依賴 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId></dependenc…

智能優化算法應用:基于侏儒貓鼬算法無線傳感器網絡(WSN)覆蓋優化 - 附代碼

智能優化算法應用&#xff1a;基于侏儒貓鼬算法無線傳感器網絡(WSN)覆蓋優化 - 附代碼 文章目錄 智能優化算法應用&#xff1a;基于侏儒貓鼬算法無線傳感器網絡(WSN)覆蓋優化 - 附代碼1.無線傳感網絡節點模型2.覆蓋數學模型及分析3.侏儒貓鼬算法4.實驗參數設定5.算法結果6.參考…

facebook廣告運營技巧

在Facebook上進行廣告運營需要一定的技巧和策略。以下是一些建議&#xff1a; 明確目標&#xff1a;在創建廣告之前&#xff0c;你需要明確你的目標。這可能包括增加品牌知名度、提高網站流量、增加銷售等等。明確目標后&#xff0c;你可以將廣告與這些目標相結合&#xff0c;…

【五分鐘】熟悉python列表和元組的異同點【看這篇夠用!建議收藏】

引言 Python&#xff0c;是一種廣泛應用于數據科學、機器學習等領域的高級編程語言&#xff0c;支持多種豐富多樣的數據類型&#xff0c;其中包括列表和元組。盡管這兩種數據結構都可用于存儲多個值&#xff0c;但它們在功能和特性上存在著明顯的差異。在接下來的博客中&#…

天津大數據培訓機構品牌 數據分析師的發展方向

大數據專業還是有一定難度的&#xff0c;畢竟大數據開發技術所包含的編程技術知識是比較雜且多的如果是計算機專業的學生或者自身有一定基礎的人學&#xff0c;相對來說會比較容易&#xff0c;但對于零基礎小伙伴學習來說&#xff0c;想要學習大數據&#xff0c;難度還是很高的…

3D Web可視化平臺助力Aras開發PLM系統:提供數據訪問、可視化和發布功能

HOOPS中文網慧都科技是HOOPS全套產品中國地區指定授權經銷商&#xff0c;提供3D軟件開發工具HOOPS售賣、試用、中文試用指導服務、中文技術支持。http://techsoft3d.evget.com/ Aras是一個面向數字化工業應用的開放性平臺&#xff0c;幫助世界領先的復雜互聯產品制造商轉變其產…

大三上實訓內容

項目一&#xff1a;爬取天氣預報數據 【內容】 在中國天氣網(http://www.weather.com.cn)中輸入城市的名稱&#xff0c;例如輸入信陽&#xff0c;進入http://www.weather.com.cn/weather1d/101180601.shtml#input 的網頁顯示信陽的天氣預報&#xff0c;其中101180601是信陽的…

SpringCloud面試題——Nacos

一&#xff1a;什么是Nacos&#xff1f; 二&#xff1a;服務心跳與服務注冊原理&#xff1f; 在spring容器啟動的時候&#xff0c;nacos客戶端會進行兩步操作。 向nacos服務端發送心跳向nacos服務端注冊當前服務 服務心跳 客戶端在啟動的時候&#xff0c;會開啟一個心跳線程…

私域運營:掌控用戶,領航變革

隨著互聯網技術的迅速進步&#xff0c;眾多電商平臺如雨后春筍般涌現。盡管淘寶、京東等第三方平臺在流量和銷售額方面占據了絕對優勢&#xff0c;但私域流量運營的興起也引發了廣泛關注。盡管尚處于初級階段&#xff0c;但私域運營已成為當前最熱門的話題之一。 私域運營指的…

HttpComponents: 概述

文章目錄 1. 概述2. 生態位 1. 概述 早期的Java想要實現HTTP客戶端需要借助URL/URLConnection或者自己手動從Socket開始編碼&#xff0c;需要處理大量HTTP協議的具體細節&#xff0c;不但繁瑣還容易出錯。 Apache Commons HttpClient的誕生就是為了解決這個問題&#xff0c;它…

高德地圖畫漸變線

高德地圖畫漸變線&#xff0c;思路是將線和顏色均分為多個小線段和小顏色&#xff0c;實現漸變&#xff0c;類似于下圖。 如果需要多段線&#xff0c;自己循環拼一下就可以了&#xff0c;方法返回多個小線段組成的polyline數組。 /** 高德地圖畫漸變線* author: liyun* params…