學習筆記-01

原視頻

版本控制介紹

集中式版本控制
  • 問題:單點故障
分布式版本控制

Git安裝

video


Git結構

image.png

Git和代碼托管中心

代碼托管中心的任務:維護遠程庫

  • 局域網環(huán)境下
    • GitLab服務器
  • 外網環(huán)境下
    • Github
    • 碼云

本地庫和遠程庫

image.png

image.png

Git命令行操作

  1. 本地庫初始化
  • 命令:git init
  • 注意:.git目錄中存放的是本地庫相關的子目錄和文件蛛勉,不要刪除,也不要胡亂修改。
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace
$ mkdir EmptyDemo

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace
$ cd EmptyDemo/

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo
$ git init
Initialized empty Git repository in E:/IdeaProjects/workspace/EmptyDemo/.git/

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ ll .git/
total 7
-rw-r--r-- 1 lenovo 197609 130 7月   1 10:06 config
-rw-r--r-- 1 lenovo 197609  73 7月   1 10:06 description
-rw-r--r-- 1 lenovo 197609  23 7月   1 10:06 HEAD
drwxr-xr-x 1 lenovo 197609   0 7月   1 10:06 hooks/
drwxr-xr-x 1 lenovo 197609   0 7月   1 10:06 info/
drwxr-xr-x 1 lenovo 197609   0 7月   1 10:06 objects/
drwxr-xr-x 1 lenovo 197609   0 7月   1 10:06 refs/
  1. 設置簽名
  • 形式
    • 用戶名
    • Email地址
  • 作用:區(qū)分不同開發(fā)人員的身份
  • 辨析:這里設置的簽名和登陸遠程庫(代碼托管中心)的賬號但骨、密碼沒有任何關系幢哨。
  • 命令
    • 項目級別/倉庫級別:僅在當前本地庫范圍內有效
    • 系統(tǒng)用戶級別:登陸當前操作系統(tǒng)的用戶范圍
    • 級別優(yōu)先級
      • 就近原則:項目級別優(yōu)先于系統(tǒng)用戶級別,二者都有時采用項目級別的簽名
      • 如果只有系統(tǒng)用戶級別的簽名,就以系統(tǒng)用戶級別的簽名為準
      • 二者都沒有是不允許的
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git config user.name mrwang

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git config user.email goodMorning_MrWang@example.com

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[user]
        name = mrwang
        email = goodMorning_MrWang@example.com

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git config --global user.name mrwang

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git config --global user.email goodMorning_MrWang@example.com

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ cd ~

lenovo@Maximus-PC MINGW64 ~
$ pwd
/c/Users/lenovo

lenovo@Maximus-PC MINGW64 ~
$ ls -lA |grep git
-rw-r--r-- 1 lenovo 197609      153 7月   1 11:12 .gitconfig

lenovo@Maximus-PC MINGW64 ~
$ cat .gitconfig
[user]
        name = mrwang
        email = goodMorning_MrWang@example.com
[core]
        autocrlf = true
        excludesfile = C:\\Users\\lenovo\\Documents\\gitignore_global.txt

添加提交以及查看狀態(tài)操作

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ vim good.txt

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        good.txt

nothing added to commit but untracked files present (use "git add" to track)

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git add good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   good.txt


lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git rm --cached good.txt
rm 'good.txt'

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        good.txt

nothing added to commit but untracked files present (use "git add" to track)

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ ll
total 1
-rw-r--r-- 1 lenovo 197609 24 7月   2 08:04 good.txt

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git add good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   good.txt


lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git commit good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
[master (root-commit) 488d18f] My first commit. New file good.txt.
 1 file changed, 3 insertions(+)
 create mode 100644 good.txt

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
nothing to commit, working tree clean

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ vim good.txt

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (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:   good.txt

no changes added to commit (use "git add" and/or "git commit -a")

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git add good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   good.txt


lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git commit -m "My second commit, modify good.txt" good.txt
warning: LF will be replaced by CRLF in good.txt.
The file will have its original line endings in your working directory.
[master 7752881] My second commit, modify good.txt
 1 file changed, 1 insertion(+)

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$
版本穿梭

查看歷史記錄的幾種方式

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git log
commit 91e905998a46c668b511925aa493e4b957b8b69c (HEAD -> master)
Author: mrwang <goodMorning_MrWang@example.com>
Date:   Mon Jul 2 09:30:01 2018 +0800

    insert fffffff edit

commit 594e334173b17a770bbeaf60239a9d70241cc783
Author: mrwang <goodMorning_MrWang@example.com>
Date:   Mon Jul 2 09:29:25 2018 +0800

    insert eeeeeee edit

commit b2025ce2431e90faefd3bc474b5033151f573516
Author: mrwang <goodMorning_MrWang@example.com>
Date:   Mon Jul 2 09:24:06 2018 +0800

    for test history

commit 77528812922c4c114db3aec3b0a33b709a7c3ae0
Author: mrwang <goodMorning_MrWang@example.com>
Date:   Mon Jul 2 09:13:35 2018 +0800

    My second commit, modify good.txt

commit 488d18f388b275815b934402fca31e4a51143c56
Author: mrwang <goodMorning_MrWang@example.com>
Date:   Mon Jul 2 08:55:07 2018 +0800

    My first commit. New file good.txt.

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git log --pretty=oneline
91e905998a46c668b511925aa493e4b957b8b69c (HEAD -> master) insert fffffff edit
594e334173b17a770bbeaf60239a9d70241cc783 insert eeeeeee edit
b2025ce2431e90faefd3bc474b5033151f573516 for test history
77528812922c4c114db3aec3b0a33b709a7c3ae0 My second commit, modify good.txt
488d18f388b275815b934402fca31e4a51143c56 My first commit. New file good.txt.

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git log --oneline
91e9059 (HEAD -> master) insert fffffff edit
594e334 insert eeeeeee edit
b2025ce for test history
7752881 My second commit, modify good.txt
488d18f My first commit. New file good.txt.

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git reflog
91e9059 (HEAD -> master) HEAD@{0}: commit: insert fffffff edit
594e334 HEAD@{1}: commit: insert eeeeeee edit
b2025ce HEAD@{2}: commit: for test history
7752881 HEAD@{3}: commit: My second commit, modify good.txt
488d18f HEAD@{4}: commit (initial): My first commit. New file good.txt.

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$

版本前進后退

  • 本質


    image.png
  • 基于索引值操作[推薦]
    • git reset --hard [局部索引值]
    • 例如:git reset --hard a6ace91
  • 使用^符號:只能后退
    • git reset --hard HEAD^
    • 注:一個^表示后退一步锁摔,^^表示后退兩步巡验,n個^表示后退n步
  • 使用~符號:只能后退
    • git reset --hard HEAD~n
    • 注:表示后退n步

基本操作

  1. 狀態(tài)查看操作
    git status
    查看工作區(qū)际插、暫存區(qū)狀態(tài)
  2. 添加操作
    git add [file]
    將工作區(qū)的“新建/修改”添加到暫存區(qū)
  3. 提交操作
    git commit -m "commit message" [file]
    將暫存區(qū)的內容提交到本地庫
  4. 查看歷史記錄
    git log log多屏顯示控制方式:空格 -> 向下翻頁,b -> 向上翻頁深碱,q -> 退出
    git log --pretty=oneline
    git log --oneline
    git reflog HEAD@{移動到當前版本需要多少步}
  5. 版本前進后退
  • 基于索引值操作[推薦]
    • git reset --hard [局部索引值]
    • 例如:git reset --hard a6ace91
  • 使用^符號:只能后退
    • git reset --hard HEAD^
    • 注:一個^表示后退一步腹鹉,^^表示后退兩步,n個^表示后退n步
  • 使用~符號:只能后退
    • git reset --hard HEAD~n
    • 注:表示后退n步
  1. reset命令的三個參數(shù)對比
  • --soft
    • 僅僅在本地庫移動HEAD指針
  • --mixed:
    • 在本地庫移動HEAD指針
    • 重置暫存區(qū)
  • --hard
    • 在本地庫移動HEAD指針
    • 重置暫存區(qū)
    • 重置工作區(qū)
  1. 刪除文件并找回
  • 前提:刪除前敷硅,文件存在時的狀態(tài)提交到了本地庫功咒。
  • 操作:`git reset --hard [需找回文件存在的指針位置]
    • 刪除操作已經提交到本地庫:指針位置指向歷史記錄
    • 刪除操作尚未提交到本地庫:指針位置使用HEAD
  1. 比較文件差異
  • git diff [file name]: 將工作區(qū)中的文件和暫存區(qū)進行比較
  • git diff [本地庫中歷史版本] [file name]: 將工作區(qū)中的文件和本地庫歷史記錄進行比較
  • 不帶文件名比較多個文件
lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git diff good.txt
diff --git a/good.txt b/good.txt
index baf19f6..74747ba 100644
--- a/good.txt
+++ b/good.txt
@@ -5,3 +5,4 @@ UUUUUUU
 ddddddd
 eeeeeee
 fffffff
+*******

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git diff HEAD^ good.txt
diff --git a/good.txt b/good.txt
index baf19f6..74747ba 100644
--- a/good.txt
+++ b/good.txt
@@ -5,3 +5,4 @@ UUUUUUU
 ddddddd
 eeeeeee
 fffffff
+*******

lenovo@Maximus-PC MINGW64 /e/IdeaProjects/workspace/EmptyDemo (master)
$ git diff
warning: LF will be replaced by CRLF in apple.txt.
The file will have its original line endings in your working directory.
diff --git a/apple.txt b/apple.txt
index 4c479de..656d67d 100644
--- a/apple.txt
+++ b/apple.txt
@@ -1 +1,2 @@
 apple
+-----
diff --git a/good.txt b/good.txt
index baf19f6..74747ba 100644
--- a/good.txt
+++ b/good.txt
@@ -5,3 +5,4 @@ UUUUUUU
 ddddddd
 eeeeeee
 fffffff
+*******
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市绞蹦,隨后出現(xiàn)的幾起案子力奋,更是在濱河造成了極大的恐慌,老刑警劉巖幽七,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件景殷,死亡現(xiàn)場離奇詭異,居然都是意外死亡澡屡,警方通過查閱死者的電腦和手機猿挚,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來驶鹉,“玉大人绩蜻,你說我怎么就攤上這事∈衣瘢” “怎么了办绝?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長姚淆。 經常有香客問我孕蝉,道長,這世上最難降的妖魔是什么腌逢? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任降淮,我火速辦了婚禮,結果婚禮上搏讶,老公的妹妹穿的比我還像新娘骤肛。我一直安慰自己纳本,他們只是感情好,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布腋颠。 她就那樣靜靜地躺著繁成,像睡著了一般。 火紅的嫁衣襯著肌膚如雪淑玫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天絮蒿,我揣著相機與錄音尊搬,去河邊找鬼。 笑死土涝,一個胖子當著我的面吹牛佛寿,可吹牛的內容都是我干的。 我是一名探鬼主播但壮,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼冀泻,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蜡饵?” 一聲冷哼從身側響起弹渔,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎溯祸,沒想到半個月后肢专,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡焦辅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年博杖,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片筷登。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡剃根,死狀恐怖,靈堂內的尸體忽然破棺而出仆抵,到底是詐尸還是另有隱情跟继,我是刑警寧澤种冬,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布镣丑,位于F島的核電站,受9級特大地震影響娱两,放射性物質發(fā)生泄漏莺匠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一十兢、第九天 我趴在偏房一處隱蔽的房頂上張望趣竣。 院中可真熱鬧摇庙,春花似錦、人聲如沸遥缕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽单匣。三九已至夕凝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間户秤,已是汗流浹背码秉。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留鸡号,地道東北人转砖。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像鲸伴,于是被迫代替她去往敵國和親府蔗。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內容

  • Git 基礎 基本原理 客戶端并不是只提取最新版本的文件快照挑围,而是把代碼倉庫完整的鏡像下來礁竞。這樣一來,任何一處協(xié)同...
    __silhouette閱讀 15,880評論 5 147
  • Git 命令行學習筆記 Git 基礎 基本原理 客戶端并不是只提取最新版本的文件快照杉辙,而是把代碼倉庫完整的鏡像下來...
    sunnyghx閱讀 3,915評論 0 11
  • 【1月18日模捂,輕食第25天】 上午餐,一盒火龍果蜘矢,二個干柿子狂男。 晚餐,廚房做了清炒豌豆品腹、萵筍片與豆腐炒肉岖食,后一個只...
    張春華閱讀 177評論 0 0
  • 2017.09.03No155 0735-0800用時25分鐘字數(shù)657 一直以來很佩服那些能夠做到全身心投入一件...
    何不可閱讀 370評論 1 4
  • 蟋蟀你能不叫,煩人忘記時間舞吭。吊墜絲瓜銜細雨泡垃,身披日月闌干。架下葡萄思想羡鸥,一心向往和盤蔑穴。 戶外青山楓葉,伴云親霧彌天...
    木貞ma閱讀 199評論 1 2