二、GIT基礎(chǔ)-記錄文件更新

2细办、記錄文件更新

1)檢查當(dāng)前文件狀態(tài)

要查看哪些文件處于什么狀態(tài)橙凳,可以用 git status 命令。

[root@node1 git-test]# git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)

我們可以看到這是個(gè)剛初始化的目錄笑撞,沒(méi)有未提交的文件岛啸。后面還提示我們,可以創(chuàng)建或者復(fù)制文件茴肥,然后使用git add命令對(duì)文件進(jìn)行跟蹤坚踩。

2)跟蹤新文件

我們首先在當(dāng)前目錄下創(chuàng)建一個(gè)新文件README,然后再查看當(dāng)前文件的狀態(tài)

[root@node1 git-test]# ll
total 4
-rw-r--r-- 1 root root 26 Nov 28 08:13 README

[root@node1 git-test]# git status
On branch master

Initial commit

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

    README

nothing added to commit but untracked files present (use "git add" to track)
[root@node1 git-test]#

在狀態(tài)報(bào)告中可以看到新建的 README 文件出現(xiàn)在 Untracked files 下面瓤狐。 未跟蹤的文件意味著 Git 在之前的快照(提交)中沒(méi)有這些文件瞬铸;Git 不會(huì)自動(dòng)將之納入跟蹤范圍批幌,而且后面的提示我們可以使用git add命令去跟蹤新文件
使用命令 git add 開(kāi)始跟蹤一個(gè)文件。
[root@node1 git-test]# git add README
再運(yùn)行 git status 命令嗓节,會(huì)看到 README 文件已被跟蹤荧缘,并處于暫存狀態(tài):

On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)   git add 的逆操作,具體后面講解

    new file:   README

只要在 Changes to be committed 這行下面的赦政,就說(shuō)明是已暫存狀態(tài)胜宇。 如果此時(shí)commit,那么該文件此時(shí)此刻的版本將被留存在歷史記錄中恢着。 git add 命令使用文件或目錄的路徑作為參數(shù);如果參數(shù)是目錄的路徑财破,該命令將遞歸地跟蹤該目錄下的所有文件掰派。我們也可以使用git add *提交當(dāng)前目錄下的所有文件

3)提交更新

現(xiàn)在我們已經(jīng)把創(chuàng)建的文件提交到了暫存區(qū),接下來(lái)我們就需要把暫存區(qū)的文件提交到本地版本庫(kù)進(jìn)行管理左痢。注意:在每交提交前最好先使用git status查看一下靡羡,是不是所有更改的文件都已經(jīng)暫存,然后再使用git commit命令提交俊性。

[master (root-commit) 5e874cc] first submit
 1 file changed, 1 insertion(+)
 create mode 100644 README
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

請(qǐng)記住略步,提交時(shí)記錄的是放在暫存區(qū)域的快照。 Git的每一次提交操作定页,都是對(duì)你項(xiàng)目作一次快照趟薄,以后可以回到這個(gè)狀態(tài),或者進(jìn)行比較典徊。

4)暫存己修改的文件

我們首先再新建一個(gè)Hello.txt的文件并提交:

[root@node1 git-test]# echo "Hello world">Hello.txt
[root@node1 git-test]# git status
On branch master
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)
[root@node1 git-test]# git add Hello.txt 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt

接著我們修改剛才已經(jīng)提交過(guò)的README文件杭煎,然后查看狀態(tài):

[root@node1 git-test]# echo "first modify" >>README 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt

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) 使用暫存區(qū)的文件覆蓋工作的文件

    modified:   README

文件 README 出現(xiàn)在 Changes not staged for commit 這行下面,說(shuō)明已跟蹤文件的內(nèi)容發(fā)生了變化卒落,但還沒(méi)有放到暫存區(qū)羡铲。 要暫存這次更新,需要運(yùn)行 git add 命令儡毕。 這是個(gè)多功能命令:可以用它開(kāi)始跟蹤新文件也切,或者把已跟蹤的文件放到暫存區(qū),還能用于合并時(shí)把有沖突的文件標(biāo)記為已解決狀態(tài)等腰湾。 將這個(gè)命令理解為“添加內(nèi)容到下一次提交中”更加合適雷恃。 現(xiàn)在讓我們運(yùn)行 git add 將"README"放到暫存區(qū),然后再看看 git status 的輸出:

You have new mail in /var/spool/mail/root
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt
    modified:   README

現(xiàn)在兩個(gè)文件都已暫存檐盟,下次提交時(shí)就會(huì)一并記錄到倉(cāng)庫(kù)褂萧。 假設(shè)此時(shí),你想要在 README 里再加一條內(nèi)容葵萎, 重新編輯存盤(pán)后导犹,再運(yùn)行 git status 看看:

[root@node1 git-test]# echo "second modify" >>README 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt
    modified:   README

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

你看到了什么唱凯? 現(xiàn)在 README 文件同時(shí)出現(xiàn)在暫存區(qū)和非暫存區(qū)。 這是為什么谎痢? 好吧磕昼,實(shí)際上 Git 只不過(guò)暫存了你運(yùn)行 git add 命令時(shí)的版本, 如果你現(xiàn)在提交节猿,README的版本是你最后一次運(yùn)行 git add 命令時(shí)的那個(gè)版本票从,而不是你運(yùn)行 git commit 時(shí),在工作目錄中的當(dāng)前版本滨嘱。 所以峰鄙,運(yùn)行了 git add 之后又作了修訂的文件,需要重新運(yùn)行 git add 把最新版本重新暫存起來(lái):

[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Hello.txt
    modified:   README

[root@node1 git-test]# git commit -m "second submit"
[master a306d94] second submit
 2 files changed, 3 insertions(+)
 create mode 100644 Hello.txt
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

5)四個(gè)區(qū)域和文件的四種狀態(tài)
四個(gè)區(qū)域:


image.png

文件的四種狀態(tài):


image.png

6)查看已暫存和未暫存的修改

我們?cè)俅涡薷?README 文件后暫存太雨,然后編輯 Hello.txt文件后先不暫存吟榴, 運(yùn)行 status 命令將會(huì)看到:

[root@node1 git-test]# echo "first modify" >>Hello.txt 
[root@node1 git-test]# git add README 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   README

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

要查看尚未暫存的文件更新了哪些部分,不加參數(shù)直接輸入 git diff:

[root@node1 git-test]# git diff
diff --git a/Hello.txt b/Hello.txt
index 802992c..6b599f0 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1 +1,2 @@
 Hello world
+first modify

此命令比較的是工作目錄中當(dāng)前文件和暫存區(qū)域快照之間的差異囊扳, 也就是修改之后還沒(méi)有暫存起來(lái)的變化內(nèi)容吩翻。
要查看已暫存的將要添加到下次提交里的內(nèi)容,可以用 git diff --cached 命令或git diff --staged锥咸,效果是相同的狭瞎。

[root@node1 git-test]# git diff --cached
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
 My first git test project
 first modify
 second modify
+three modify
[root@node1 git-test]# git diff --staged
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
 My first git test project
 first modify
 second modify
+three modify

現(xiàn)在我們將Hello.txt文件暫存,然后再進(jìn)行編輯后搏予,查看狀態(tài):

[root@node1 git-test]# git add Hello.txt 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   Hello.txt
    modified:   README

[root@node1 git-test]# git diff
[root@node1 git-test]# echo "second modify" >>Hello.txt 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   Hello.txt
    modified:   README

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

[root@node1 git-test]# git diff
diff --git a/Hello.txt b/Hello.txt
index 6b599f0..bfd0f21 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1,2 +1,3 @@
 Hello world
 first modify
+second modify
[root@node1 git-test]# git diff --staged
diff --git a/Hello.txt b/Hello.txt
index 802992c..6b599f0 100644
--- a/Hello.txt
+++ b/Hello.txt
@@ -1 +1,2 @@
 Hello world
+first modify
diff --git a/README b/README
index 771c673..5f9ac91 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
 My first git test project
 first modify
 second modify
+three modify

請(qǐng)注意熊锭,git diff 本身只顯示尚未暫存的改動(dòng),而不是自上次提交以來(lái)所做的所有改動(dòng)缔刹。 所以有時(shí)候你一下子暫存了所有更新過(guò)的文件后球涛,運(yùn)行 git diff 后卻什么也沒(méi)有,就是這個(gè)原因

7)跳過(guò)使用暫存區(qū)

git commit 加上 -a 選項(xiàng)校镐,Git 就會(huì)自動(dòng)把所有已經(jīng)跟蹤過(guò)的文件暫存起來(lái)一并提交亿扁,從而跳過(guò) git add 步驟:

[root@node1 git-test]# 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")
[root@node1 git-test]# git commit -a -m "jump git add submit"
[master 4794019] jump git add submit
 1 file changed, 2 insertions(+)
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

8、移除文件

我們先創(chuàng)建一個(gè)用于刪除測(cè)試的文件a.txt,并提交

[root@node1 git-test]# git add a.txt 
[root@node1 git-test]# git commit -m "rm test"
[master a80c6a9] rm test
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

現(xiàn)在我們?cè)诠ぷ髂夸泟h除a.txt文件鸟廓,然后查看狀態(tài),在 “Changes not staged for commit” 部分(也就是 未暫存清單)看到

[root@node1 git-test]# 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:    a.txt

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

然后再運(yùn)行 git rm 從暫存區(qū)刪除文件:

rm 'a.txt'
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    a.txt

[root@node1 git-test]# git commit -m "rm test finish"
[master 3a2db18] rm test finish
 1 file changed, 1 deletion(-)
 delete mode 100644 a.txt
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

另外一種情況是从祝,我們想把文件從 Git 倉(cāng)庫(kù)中刪除(亦即從暫存區(qū)域移除),但仍然希望保留在當(dāng)前工作目錄中引谜。 換句話(huà)說(shuō)牍陌,你想讓文件保留在磁盤(pán),但是并不想讓 Git 繼續(xù)跟蹤员咽。使用 --cached 選項(xiàng)

[root@node1 git-test]# git commit -m "delete a.txt from response"

9毒涧、移動(dòng)文件

使用git mv命令對(duì)文件進(jìn)行改名操作:
使用mv命令改名:

[root@node1 git-test]# 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:    Hello.txt

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

    Helloword.txt

no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 git-test]# git add Helloword.txt 
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   Helloword.txt

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:    Hello.txt

[root@node1 git-test]# git commit -m "mv test"
[master 9ccca93] mv test
 1 file changed, 4 insertions(+)
 create mode 100644 Helloword.txt
[root@node1 git-test]# 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:    Hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
[root@node1 git-test]# git rm Hello.txt
rm 'Hello.txt'
[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    Hello.txt

[root@node1 git-test]# git commit -m "delete Hello.txt"
[master 2b1583e] delete Hello.txt
 1 file changed, 4 deletions(-)
 delete mode 100644 Hello.txt
You have new mail in /var/spool/mail/root
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean
[root@node1 git-test]# ll
total 8
-rw-r--r-- 1 root root 52 Nov 28 11:17 Helloword.txt
-rw-r--r-- 1 root root 66 Nov 28 10:42 README

使用git mv 命令改名

[root@node1 git-test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    Helloword.txt -> Hello.txt

[root@node1 git-test]# git commit -m "git mv test"
[master 6597abf] git mv test
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename Helloword.txt => Hello.txt (100%)
[root@node1 git-test]# git status
On branch master
nothing to commit, working tree clean

不管何種方式結(jié)果都一樣。 兩者唯一的區(qū)別是贝室,mv 是一條命令而另一種方式需要三條命令契讲,直接用 git mv 輕便得多

10仿吞、忽略文件

一般我們總會(huì)有些文件無(wú)需納入 Git 的管理,也不希望它們總出現(xiàn)在未跟蹤文件列表捡偏。 通常都是些自動(dòng)生成的文件唤冈,比如日志文件,或者編譯過(guò)程中創(chuàng)建的臨時(shí)文件等银伟。 在這種情況下你虹,我們可以創(chuàng)建一個(gè)名為 .gitignore 的文件,列出要忽略的文件模式彤避。
我們?cè)倏匆粋€(gè) .gitignore 文件的例子:

*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末傅物,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子忠藤,更是在濱河造成了極大的恐慌挟伙,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件模孩,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡贮缅,警方通過(guò)查閱死者的電腦和手機(jī)榨咐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)谴供,“玉大人块茁,你說(shuō)我怎么就攤上這事」鸺。” “怎么了数焊?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)崎场。 經(jīng)常有香客問(wèn)我佩耳,道長(zhǎng),這世上最難降的妖魔是什么谭跨? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任干厚,我火速辦了婚禮,結(jié)果婚禮上螃宙,老公的妹妹穿的比我還像新娘蛮瞄。我一直安慰自己,他們只是感情好谆扎,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布挂捅。 她就那樣靜靜地躺著,像睡著了一般堂湖。 火紅的嫁衣襯著肌膚如雪闲先。 梳的紋絲不亂的頭發(fā)上状土,一...
    開(kāi)封第一講書(shū)人閱讀 51,573評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音饵蒂,去河邊找鬼声诸。 笑死,一個(gè)胖子當(dāng)著我的面吹牛退盯,可吹牛的內(nèi)容都是我干的彼乌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼渊迁,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼慰照!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起琉朽,我...
    開(kāi)封第一講書(shū)人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤毒租,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后箱叁,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體墅垮,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年耕漱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了算色。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡螟够,死狀恐怖灾梦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情妓笙,我是刑警寧澤若河,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站寞宫,受9級(jí)特大地震影響萧福,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜淆九,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一统锤、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧炭庙,春花似錦饲窿、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春鸦泳,著一層夾襖步出監(jiān)牢的瞬間银锻,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工做鹰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留击纬,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓钾麸,卻偏偏與公主長(zhǎng)得像更振,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子饭尝,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1.git的安裝 1.1 在Windows上安裝Git msysgit是Windows版的Git肯腕,從https:/...
    落魂灬閱讀 12,663評(píng)論 4 54
  • 天氣 陣雨 每天老師講的都是不一樣的加人方法,讓朋友分享到朋友圈的之前試過(guò)钥平,可能是方法不對(duì)实撒,所以也沒(méi)人加。今天上班...
    歡Y閱讀 113評(píng)論 0 0
  • 很感謝生命中出現(xiàn)的每一個(gè)人涉瘾,他們教會(huì)你成長(zhǎng)知态。 人生的路很遙遠(yuǎn),這么長(zhǎng)的路途中你會(huì)遇到各色各樣的人立叛。比如肴甸,工作中,同...
    巫女穎惠閱讀 372評(píng)論 0 0
  • 每天中午去外面吃午飯的時(shí)候囚巴,總要經(jīng)過(guò)一家有名的整容醫(yī)院,有時(shí)候會(huì)看到出入醫(yī)院的妙齡女郎友扰,面容姣好彤叉,身材健美,不禁會(huì)...
    Jane_gz閱讀 162評(píng)論 0 0
  • 像往常一樣,每天看完勃君的更新后開(kāi)始思考今天該寫(xiě)點(diǎn)什么甚负,勃君的文章正在一點(diǎn)點(diǎn)的發(fā)生著變化柬焕,思維邏輯更加嚴(yán)謹(jǐn),文筆更...
    黑莓可愛(ài)多閱讀 1,417評(píng)論 0 0