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ū)域:
文件的四種狀態(tài):
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