Git 項目有三個工做區域的概念:工做目錄,暫存區域以及Git 倉庫git
Git之中的文件有三種狀態,文件可能處於其中之一:已提交(committed)、已修改(modified)和已暫存(staged)。已提交表示數據已經安全的保存在本地數據庫中。 已修改表示修改了文件,但還沒保存到數據庫中。 這三種狀態其實並不重要,重要的是三個工做區域的概念,已提交表示git commit -m'xxx',文件保存到倉庫了,已暫存表示文件已經執行了git add file,已暫存表示對一個已修改文件的當前版本作了標記,使之包含在下次提交的快照中,已修改表示對於本地工做目錄之中的文件,進行了修改,是咱們普通創做的基本操做github
成功安裝git後,會有一個 git config 的文件控制 Git 外觀和行爲的配置變量數據庫
用戶信息vim
當安裝完 Git 應該作的第一件事就是設置你的用戶名稱與郵件地址。 每一次 Git 提交都會使用這些信息,而且它會寫入到每一次提交中,不可更改,須要說明的是,以下的配置都是在bash命令行之中執行的,而不是在git config文件之中緩存
$ git config --global user.name "hello" $ git config --global user.email 123456789@qq.com
若是使用了 --global 選項,那麼該命令只須要運行一次,這樣是一種全局配置,若是要針對特定項目使用不一樣的用戶名稱與郵件地址時,能夠在那個項目目錄下運行沒有 --global 選項的命令來配置安全
Git 會使用操做系統默認的文本編輯器,一般是 Vim。 若是你想使用不一樣的文本編輯器,例如 Emacs,可使用以下配置:bash
$ git config --global core.editor emacs
若是想要檢查你的配置,可使用 git config --list 命令來列出全部 Git 當時能找到的配置。服務器
$ git config --list user.name=hello user.email=123456789@qq.com color.status=auto color.branch=auto color.interactive=auto color.diff=auto ...
能夠經過輸入 git config <key>: 來檢查 Git 的某一項配置less
$ git config user.name hello
獲取幫助編輯器
若使用 Git 時須要獲取幫助,有三種方法能夠找到 Git 命令的使用手冊:
$ git help <verb> $ git <verb> --help $ man git-<verb>
例如,要想得到 config 命令的手冊,執行
$ git help config
有兩種取得 Git 項目倉庫的方法。 第一種是在現有項目或目錄下導入全部文件到 Git 中; 第二種是從一個服務器克隆一個現有的 Git 倉庫。
在現有目錄中初始化倉庫
使用 Git 來對現有的項目進行管理,須要進入該項目目錄並輸入:
$ git init
該命令將建立一個名爲 .git 的子目錄,這個子目錄含有初始化的 Git 倉庫中全部的必須文件,這些文件是 Git 倉庫的骨幹。此時僅僅是作了一個初始化的操做,項目裏的文件尚未被跟蹤。在一個已經存在文件的文件夾(而不是空文件夾)中初始化 Git 倉庫來進行版本控制的話,應該開始跟蹤這些文件並提交。 可經過 git add 命令來實現對指定文件的跟蹤,而後執行 git commit 提交
$ git add *.c $ git add LICENSE $ git commit -m 'initial project version'
若是想得到一份已經存在了的 Git 倉庫的拷貝,好比說github上面的某一個項目,這時就要用到 git clone 命令。 Git 克隆的是該 Git 倉庫服務器上的幾乎全部數據,而不是僅僅複製完成你的工做所須要文件。 當執行 git clone 命令的時候,默認配置下遠程 Git 倉庫中的每個文件的每個版本都將被拉取下來。 克隆倉庫的命令格式是 git clone [url] 。 好比,要克隆 Git 的可連接庫 libgit2,能夠用下面的命令
$ git clone https://github.com/libgit2/libgit2
這會在當前目錄下建立一個名爲 「libgit2」 的目錄,並在這個目錄下初始化一個 .git 文件夾,從遠程倉庫拉取下全部數據放入 .git 文件夾,而後從中讀取最新版本的文件的拷貝。 以上命令獲得的本地倉庫和遠程倉庫名稱相同,若是想在克隆遠程倉庫的時候,自定義本地倉庫的名字,可使用以下命令
$ git clone https://github.com/libgit2/libgit2 mylibgit
這將執行與上一個命令相同的操做,不過在本地建立的倉庫名字變爲 mylibgit。
工做目錄下的每個文件都不外乎這兩種狀態:已跟蹤或未跟蹤。 已跟蹤的文件是指那些被歸入了版本控制的文件,在上一次快照中有它們的記錄,在工做一段時間後,它們的狀態可能處於未修改,已修改或已放入暫存區。 工做目錄中除已跟蹤文件之外的全部其它文件都屬於未跟蹤文件,它們既不存在於上次快照的記錄中,也沒有放入暫存區。 初次克隆某個倉庫的時候,工做目錄中的全部文件都屬於已跟蹤文件,並處於未修改狀態。編輯過某些文件以後,因爲自上次提交後對它們作了修改,Git 將它們標記爲已修改文件。 咱們逐步將這些修改過的文件放入暫存區,而後提交全部暫存了的修改,如此反覆。因此使用 Git 時文件的生命週期以下:
要查看哪些文件處於什麼狀態,能夠用 git status 命令。 若是在克隆倉庫後當即使用此命令,會看到相似這樣的輸出
$ git status On branch master nothing to commit, working directory clean
這說明全部已跟蹤文件在上次提交後都未被更改過。 此外,上面的信息還代表,當前目錄下沒有出現任何處於未跟蹤狀態的新文件,該命令還顯示了當前所在分支,分支名是 「master」,這是默認的分支名。
如今在項目下建立一個新的 README 文件。 若是以前並不存在這個文件,使用 git status 命令,你將看到一個新的未跟蹤文件
$ echo 'My Project' > README $ git status On branch master 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)
在狀態報告中能夠看到新建的 README 文件出如今 Untracked files 下面。 未跟蹤的文件意味着 Git 在以前的快照(提交)中沒有這些文件;Git 不會自動將之歸入跟蹤範圍,除非使用git add README來講明我須要跟蹤該文件, 次吃纔會將此文件歸入到跟蹤範圍之中,而且,此時此文件在暫存區,是一個快照,並未保存到Git 本地倉庫之中永久存儲
一般,咱們使用git status獲得的文件狀態比較複雜,咱們可使用git status -s來獲取簡略的信息,一般有A和M兩種,A表示新添加的文件,M表示修改過的文件
使用命令 git add 開始跟蹤一個文件。 因此,要跟蹤 README 文件,能夠運行以下命令行
$ git add README
此時再運行 git status 命令,會看到 README 文件已被跟蹤,並處於暫存狀態
$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README
只要在 Changes to be committed 這行下面的,就說明是已暫存狀態。 若是此時提交,那麼該文件此時此刻的版本將被留存在歷史記錄中。git add後文件存放在暫存區, git add是個多功能命令:能夠用它開始跟蹤新文件,或者把已跟蹤的文件放到暫存區,還能用於合併時把有衝突的文件標記爲已解決狀態等。這個命令能夠理解爲「添加內容到下一次提交中」。
如今咱們來修改一個已被跟蹤的文件。 若是修改了一個名爲 CONTRIBUTING.md 的已被跟蹤的文件,而後運行 git status 命令,就可實現對已經暫存的文件的狀態查看
$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: 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: CONTRIBUTING.md
文件 CONTRIBUTING.md 出如今 Changes not staged for commit 這行下面,說明已跟蹤文件的內容發生了變化,但尚未放到暫存區。 要暫存此次更新,須要運行 git add 命令
而後運行 git add 將"CONTRIBUTING.md"放到暫存區,如今兩個文件都已暫存,下次提交時就會一併記錄到倉庫。輸入 git status 可看到
$ git add CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README modified: CONTRIBUTING.md
假設此時,須要在 CONTRIBUTING.md 里加條註釋, 而後保存,再運行 git status ,則有以下輸出
$ vim CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README modified: CONTRIBUTING.md 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: CONTRIBUTING.md
此時的 CONTRIBUTING.md 文件同時出如今暫存區和非暫存區。 此時的操做是1.剛git add過一次,2.而後當即又修改了文檔,3.再又運行git status命令。實際上此時的 Git 只不過暫存了運行 git add 命令時的版本(1處的版本), 若是此時提交,CONTRIBUTING.md 的版本是你最後一次運行 git add 命令時的那個版本(1處的版本),而不是在工做目錄中的當前版本(2處的版本)。 因此,運行了 git add 以後又做了修訂的文件,須要從新運行 git add 把最新版本從新暫存起來
$ git add CONTRIBUTING.md $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README modified: CONTRIBUTING.md
雖然,如今咱們可使用git status來查看文檔的狀態,可是這只是針對與文檔的,也就是說,咱們能夠看見那些文檔是新增的,那些是修改的,可是具體文檔之中修改了什麼,新增了什麼,咱們沒法知道,這種狀況下,咱們須要使用git diff命令來完成查看,儘管 git status 已經經過在相應欄下列出文件名的方式回答了這個問題,git diff 將經過文件補丁的格式顯示具體哪些行發生了改變
git diff: 文件內容的增長和修改
假如再次修改 README 文件後暫存,而後編輯 CONTRIBUTING.md 文件後先不暫存, 運行 status 命令將會看到:
$ 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: CONTRIBUTING.md
要查看還沒有暫存的文件更新了哪些部分,不加參數直接輸入 `git diff
$ git diff diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ebb991..643e24f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,7 +65,8 @@ branch directly, things can get messy. Please include a nice description of your changes when you submit your PR; if we have to read the whole diff to figure out why you're contributing in the first place, you're less likely to get feedback and have your change -merged in. +merged in. Also, split your changes into comprehensive chunks if your patch is +longer than a dozen lines. If you are starting to work on a particular area, feel free to submit a PR that highlights your work in progress (and note in the PR title that it's
此命令比較的是工做目錄中當前文件和暫存區域快照之間的差別, 也就是修改以後尚未暫存起來的變化內容
若要查看已暫存的將要添加到下次提交裏的內容,能夠用 git diff --cached 命令。也就是比較同一個文件,在本次git add和前一次git add,兩次git add之間的差異,cached表示兩個快照之間的差異,Git 1.6.1 及更高版本還容許使用 git diff --staged,效果是相同的,但更好記些)
$ git diff --staged diff --git a/README b/README new file mode 100644 index 0000000..03902a1 --- /dev/null +++ b/README @@ -0,0 +1 @@ +My Project
請注意,git diff 自己只顯示還沒有暫存的改動,而不是自上次提交以來所作的全部改動。 因此有時候一會兒暫存了全部更新過的文件後,運行 git diff 後卻什麼也沒有,就是這個緣由
引入新的例子,新建一個倉庫,加入aaa.txt文檔在其中,將其引入跟蹤,運行一次git add aaa.txt,其中的內容爲aaa ,而後修改aaa.txt,在第6行加入bbb,此時運行 git status ,可是此時並未『再次運行』git add命令,此時運行 git status 會看到暫存先後的兩個版本。此處要查看的是未暫存的修改,效果以下
$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: aaa.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) modified: aaa.txt
如今運行 git diff 看暫存先後的變化,此時查看的是未暫存的修改
$ git diff diff --git a/aaa.txt b/aaa.txt index 7c4a013..6dad95f 100644 --- a/aaa.txt +++ b/aaa.txt @@ -1 +1,6 @@ -aaa \ No newline at end of file +aaa + + + + +bbb \ No newline at end of file
而後用 當運行了git add命令後,再次運行git diff --cached ,查看的就是已暫存的修改,比較先後兩個stage(暫存)之間的修改和變化,--staged 和 --cached 是同義詞
$ git diff --cached diff --git a/aaa.txt b/aaa.txt new file mode 100644 index 0000000..6dad95f --- /dev/null +++ b/aaa.txt @@ -0,0 +1,6 @@ +aaa + + + + +bbb \ No newline at end of file
當暫存區域已經準備穩當能夠提交了。在此以前,請必定要確認還有什麼修改過的或新建的文件尚未 git add 過,不然提交的時候不會記錄這些還沒暫存起來的變化。 這些修改過的文件只保留在本地磁盤。 因此,每次準備提交前,先用 git status 看下,是否是都已暫存起來了, 而後再運行提交命令 git commit
$ git commit
若是想要更詳細的對修改了哪些內容的提示,能夠用 -v 選項,這會將所作的改變的 diff 輸出放到編輯器中從而使你知道本次提交具體作了哪些修改。退出編輯器時,Git 會丟掉註釋行,用你輸入提交附帶信息生成一次提交。
另外,也能夠在 commit 命令後添加 -m 選項,將提交信息與命令放在同一行,以下所示:
$ git commit -m "Story 182: Fix benchmarks for speed" [master 463dc4f] Story 182: Fix benchmarks for speed 2 files changed, 2 insertions(+) create mode 100644 README
能夠看到,提交後它會告訴你,當前是在哪一個分支(master)提交的,本次提交的完整 SHA-1 校驗和是什麼(463dc4f),以及在本次提交中,有多少文件修訂過,多少行添加和刪改過。
提交時記錄的是放在暫存區域的快照。任何已經修改,但還未暫存(放到快照區(使用git add提交))的文件,在本次提交之中其所做的修改不會保存其中,而在下次添加到緩存區以後,才能夠歸入版本管理。 每一次運行提交操做,都是對項目做一次快照,之後能夠回到這個狀態,或者進行比較
跳過使用暫存區域
使用暫存區域的方式略顯繁瑣,Git 提供了一個跳過使用暫存區域的方式, 只要在提交的時候,給 git commit 加上 -a 選項,Git 就會自動把全部已經跟蹤過的文件暫存起來一併提交,從而跳過 git add 步驟,而直接提交,可是,這種操做只是針對已經加入追蹤的文件,對於從未加入追蹤的文件,是不會加入追蹤的,因此仍然須要git add xxx。以下,111.txt早已加入到追蹤,修改以後,並未使用git add,而是直接使用git commit -a -m'commit without add step',而11tt.txt文件從未加入過追蹤,因此直接提交的時候,對其不起做用,只能先加入追蹤,之後纔可使用直接提交的命令,爲了保險起見,建議使用add步驟,而後提交
hello@PC-HELLO MINGW64 /e/Codes/gittest (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: 111.txt Untracked files: (use "git add <file>..." to include in what will be committed) 11tt.txt no changes added to commit (use "git add" and/or "git commit -a") hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git commit -a -m'commit without add step' [master 9d5b684] commit without add step 1 file changed, 3 insertions(+) hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) 11tt.txt nothing added to commit but untracked files present (use "git add" to track) hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git log commit 9d5b6842302a0b7d3fcb4d58f694cf19a9530972 (HEAD -> master) Author: hello <123456789@qq.com> Date: Thu Dec 28 14:38:07 2017 +0800 commit without add step
要從 Git 倉庫中移除某個文件,就必需要從已跟蹤文件清單中移除(確切地說,是從暫存區域移除),而後提交。 能夠用 git rm 命令完成此項工做,並連帶從工做目錄中刪除指定的文件,在下一次提交以後,刪除的文檔就不會出如今跟蹤文件清單中,可是當刪除文件未提交時,仍然受跟蹤,而且是deleted狀態。注意的一點是,當使用了git rm 命令刪除了某一文件以後,其會當即進入已追蹤等待提交的狀態,通過實驗有無git add .都是能夠的,『.』表示當前目錄的全部文件,。以下例子中,刪除了123.txt文件,而且提交以後顯示此分支是clean的
hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ ls 11tt.txt 123.txt aaa.txt odf.txt ppp.txt tt.py hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git rm 123.txt rm '123.txt' hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git commit -m'delete' [master 72d0b8b] delete 1 file changed, 1 deletion(-) delete mode 100644 123.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master nothing to commit, working tree clean
git中的移動文件,其實也就是更名字+移動兩個功能,使用mv命令來完成,格式是$ git mv file_from file_to,能夠同一層級更名,也能夠不一樣層級更名+移動,運行 git mv 就至關於運行了以下3條命令
$ mv README.md README $ git rm README.md $ git add README
下面兩個例子分別是不一樣層級的移動和同級移動,s1是git倉庫下的一個文件夾
# eg.1 hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ ls 11tt.txt aaa.txt hello.c p1.txt s1/ tt.py tt.txt wqe.py hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git mv aaa.txt s1/123.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: aaa.txt -> s1/123.txt # eg.2 hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git commit -m 'move action' [master 63475c1] move action 1 file changed, 0 insertions(+), 0 deletions(-) rename aaa.txt => s1/123.txt (100%) hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git mv tt.txt tt1.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: tt.txt -> tt1.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git commit -m'mv rename' [master e56502a] mv rename 1 file changed, 0 insertions(+), 0 deletions(-) rename tt.txt => tt1.txt (100%)
最基本的查看提交歷史的命令就是git log了,它會顯示全部的歷史,此時進入文本read-only模式(VIM),須要使用基本的進入,退出等操做,這個不在話下。git log命令常有不少 的參數,一個經常使用的選項是 -p,用來顯示每次提交的內容差別。 加上 -2 來僅顯示最近兩次提交;--stat 選項在每次提交的下面列出全部文件的增刪改和文件內容的增刪改狀況,這是基本經常使用的命令,其餘命令能夠查詢
hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git log -p -2 commit e56502a144d8f157dfc60296636089ee3235337b (HEAD -> master) Author: hello <123456789@qq.com> Date: Thu Dec 28 15:24:04 2017 +0800 mv rename diff --git a/tt.txt b/tt1.txt similarity index 100% rename from tt.txt rename to tt1.txt commit 63475c1e79f00f0b11c153a01bcb6fba26985d8a Author: hello <123456789@qq.com> Date: Thu Dec 28 15:21:42 2017 +0800 move action diff --git a/aaa.txt b/s1/123.txt similarity index 100% rename from aaa.txt rename to s1/123.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git log --stat -3 commit e56502a144d8f157dfc60296636089ee3235337b (HEAD -> master) Author: hello <123456789@qq.com> Date: Thu Dec 28 15:24:04 2017 +0800 mv rename tt.txt => tt1.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) commit 63475c1e79f00f0b11c153a01bcb6fba26985d8a Author: hello <123456789@qq.com> Date: Thu Dec 28 15:21:42 2017 +0800 move action aaa.txt => s1/123.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) commit b627e58c45f055b6e64dcf79096cf802cbe982a3 Author: hello <123456789@qq.com> Date: Thu Dec 28 15:20:46 2017 +0800 213213 s1/qqq.txt | 1 + 1 file changed, 1 insertion(+)
git log 的經常使用選項
選項 說明
-p 按補丁格式顯示每一個更新之間的差別。
--stat 顯示每次更新的文件修改統計信息。
--shortstat 只顯示 --stat 中最後的行數修改添加移除統計。
--name-only 僅在提交信息後顯示已修改的文件清單。
--name-status 顯示新增、修改、刪除的文件清單。
--abbrev-commit 僅顯示 SHA-1 的前幾個字符,而非全部的 40 個字符。
--relative-date 使用較短的相對時間顯示(好比,「2 weeks ago」)。
--graph 顯示 ASCII 圖形表示的分支合併歷史。
--pretty 使用其餘格式顯示歷史提交信息。可用的選項包括 oneline,short,full,fuller 和 format(後跟指定格式)。
提交後(未修改快照),修改提交信息
有時候咱們提交完了才發現漏掉了幾個文件沒有添加,或者提交信息寫錯了。 此時,能夠運行帶有 --amend 選項的提交命令嘗試從新提交
$ git commit --amend
這個命令會將暫存區中的文件提交。 若是自上次提交以來你還未作任何修改(例如,在上次提交後立刻執行了此命令),那麼快照會保持不變,而修改的只是提交信息。若是提交後發現忘記了暫存某些須要的修改,能夠像下面這樣操做,(暫時沒有演示,只展現命令)
$ git commit -m 'initial commit' $ git add forgotten_file $ git commit --amend
最終只會有一個提交,也就是第二次提交將代替第一次提交的結果,以下
hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git add mmm123.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git commit -m 'add mmm123.txt' [master a575e22] add mmm123.txt 1 file changed, 1 insertion(+) create mode 100644 mmm123.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git log -p -1 commit a575e2225c1b35411d133b1115bca6393290766e (HEAD -> master) Author: hello <123456789@qq.com> Date: Thu Dec 28 17:18:20 2017 +0800 add mmm123.txt # 提交的信息 diff --git a/mmm123.txt b/mmm123.txt new file mode 100644 index 0000000..6e9e156 --- /dev/null +++ b/mmm123.txt @@ -0,0 +1 @@ +1232132112312 \ No newline at end of file hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git commit --amend -m'add mmm123.txt hello' # 修改提交的信息 [master c97ccc3] add mmm123.txt hello Date: Thu Dec 28 17:18:20 2017 +0800 1 file changed, 1 insertion(+) create mode 100644 mmm123.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git log -p -1 commit c97ccc30c76e148bbc3ab2c07b64d10da9944f3e (HEAD -> master) Author: hello <123456789@qq.com> Date: Thu Dec 28 17:18:20 2017 +0800 add mmm123.txt hello # 修改了提交的信息 diff --git a/mmm123.txt b/mmm123.txt new file mode 100644 index 0000000..6e9e156 --- /dev/null +++ b/mmm123.txt @@ -0,0 +1 @@ +1232132112312 \ No newline at end of file
取消暫存文件,說明此時,文件已經被暫存起來了,也就是已經運行過了git add xxx命令,此時須要取消此次add,那麼就須要使用git reset HEAD <file>命令,並且可若是一次存了多個文件,可使用具體的文件名,一個一個的取消,使用例子以下
hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) mg.txt pt.txt nothing added to commit but untracked files present (use "git add" to track) hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git add . renjiaxin@PC-RENJIAXIN MINGW64 /e/Codes/gittest (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: mg.txt new file: pt.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git reset HEAD mg.txt # 取消單個文件 hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: pt.txt Untracked files: (use "git add <file>..." to include in what will be committed) mg.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git add . hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: mg.txt new file: pt.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git reset HEAD . # 取消多個文件 hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) mg.txt pt.txt nothing added to commit but untracked files present (use "git add" to track)
此處所講的撤銷對文件的修改指的是本地的工做目錄中的文件,此時工做目錄中的文件已經修改,可是此時還未提交到git倉庫之中,此時若是咱們想撤銷全部對於工做目錄中文件的修改,一個方法就是使用倉庫中的文件,由於倉庫中的文件沒有修改過,因此咱們可使用它覆蓋本地文件,可是這是一個很危險的動做,由於這樣會讓咱們的工做成果,在不經意之間化爲烏有,因此必定要謹慎!撤銷的命令爲git checkout <file>,運行了此命令,本地文檔的修改就被撤銷。好比,文檔sg.txt的內容原本以下123456,提交以後,又修改pt.txt的內容,在第3行添加654321 hello
hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) sg.txt nothing added to commit but untracked files present (use "git add" to track) hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git add . renjiaxin@PC-RENJIAXIN MINGW64 /e/Codes/gittest (master) $ git commit -m'sg 123456' [master 5257ed3] sg 123456 1 file changed, 1 insertion(+) create mode 100644 sg.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (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: sg.txt no changes added to commit (use "git add" and/or "git commit -a") hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git checkout sg.txt hello@PC-HELLO MINGW64 /e/Codes/gittest (master) $ git status On branch master nothing to commit, working tree clean
獲取遠程的倉庫,可使用git clone giturl來將遠程的倉庫複製到本地,查看遠程倉庫使用的是git remote,使用git remote -v,能夠獲取獲得須要讀寫遠程倉庫使用的 Git 保存的簡寫與其對應的 URL。 origin是 Git 給克隆的倉庫服務器的默認名字。添加一個遠程Git倉庫使用git remote add <shortname> <url> 命令,同時還能夠指定一個輕鬆引用的簡寫,設定好簡寫以後,能夠在命令行中使用設定的字符串來代替整個 URL,從遠程服務器抓取倉庫數據,可使用git fetch [remote-name],這樣會訪問遠程倉庫,而且從中拉取本地尚未的數據,獲取其全部分支。若是使用 clone 命令克隆了一個倉庫,命令會自動將其添加爲遠程倉庫並默認以 「origin」 爲簡寫。 因此,git fetch origin 會抓取克隆(或上一次抓取)後新推送的全部工做。 注意 git fetch 命令會將數據拉取到你的本地倉庫,它並不會自動合併或修改你當前的工做。有獲取也有推送,當咱們更新了本地代碼,就能夠將其推送到遠程倉庫,使用git push [remote-name] [branch-name],好比想要將 master 分支推送到 origin 服務器時,那麼運行git push origin master命令就能夠將本地修改推送到遠程服務器。若是想要查看某一個遠程倉庫的更多信息,可使用 git remote show [remote-name] 命令。若是想要重命名引用的名字能夠運行 git remote rename oldname newname 去修改一個遠程倉庫的簡寫名,遠程刪除倉庫的分支可使用git remote rm [branch-name]
$ git clone https://github.com/schacon/ticgit Cloning into 'ticgit'... remote: Reusing existing pack: 1857, done. remote: Total 1857 (delta 0), reused 0 (delta 0) Receiving objects: 100% (1857/1857), 374.35 KiB | 268.00 KiB/s, done. Resolving deltas: 100% (772/772), done. Checking connectivity... done. $ git remote origin $ git remote -v origin https://github.com/schacon/ticgit (fetch) origin https://github.com/schacon/ticgit (push) $ git remote add pb https://github.com/paulboone/ticgit $ git remote -v pb https://github.com/paulboone/ticgit (fetch) pb https://github.com/paulboone/ticgit (push) $ git fetch pb remote: Counting objects: 43, done. remote: Compressing objects: 100% (36/36), done. remote: Total 43 (delta 10), reused 31 (delta 5) Unpacking objects: 100% (43/43), done. From https://github.com/paulboone/ticgit * [new branch] master -> pb/master * [new branch] ticgit -> pb/ticgit $ git remote rename pb paul $ git remote origin paul $ git remote rm paul $ git remote origin
Git 能夠給歷史中的某一個提交打上標籤,以示重要。列出標籤只須要輸入 git tag。Git 使用兩種主要類型的標籤:輕量標籤(lightweight)與附註標籤(annotated)。一個輕量標籤很像一個不會改變的分支,它只是一個特定提交的引用。附註標籤是存儲在 Git 數據庫中的一個完整對象。 它們是能夠被校驗的;其中包含打標籤者的名字、電子郵件地址、日期時間;還有一個標籤信息;而且可使用 GNU Privacy Guard (GPG)簽名與驗證。 一般建議建立附註標籤,這樣就能夠擁有以上全部信息;可是若是隻是想用一個臨時的標籤,或者由於某些緣由不想要保存那些信息,輕量標籤也是可用的。 輕量標籤本質上是將提交校驗和存儲到一個文件中 , 沒有保存任何其餘信息。 建立輕量標籤,不須要使用 -a、-s 或 -m 選項,只須要提供標籤名字。建立附註標籤最簡單的方式是當你在運行 tag 命令時指定 -a 選項,-m 選項指定了一條將會存儲在標籤中的信息,經過使用 git show 命令能夠看到標籤信息與對應的提交信息
$ git tag v0.1 v1.3 $ git tag v1.4-lw $ git tag v0.1 v1.3 v1.4 v1.4-lw v1.5 $ git show v1.4-lw commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@gee-mail.com> Date: Mon Mar 17 21:52:11 2008 -0700 changed the version number $ git tag -a v1.4 -m 'my version 1.4' $ git tag v0.1 v1.3 v1.4 $ git show v1.4 tag v1.4 Tagger: Ben Straub <ben@straub.cc> Date: Sat May 3 20:19:12 2014 -0700 my version 1.4 commit ca82a6dff817ec66f44342007202690a93763949 Author: Scott Chacon <schacon@gee-mail.com> Date: Mon Mar 17 21:52:11 2008 -0700 changed the version number
ref:
1.1.3 起步 - Git 基礎, 2.1.6 起步 - 初次運行 Git 前的配置, 3.1.7 起步 - 獲取幫助, 4.2.1 Git 基礎 - 獲取 Git 倉庫, 5.2.2 Git 基礎 - 記錄每次更新到倉庫 6.2.3 Git 基礎 - 查看提交歷史, 7.2.4 Git 基礎 - 撤消操做, 8.2.5 Git 基礎 - 遠程倉庫的使用, 9.2.6 Git 基礎 - 打標籤