利用好git status的提示信息
git里有工作區,暫存區,本地庫,遠程庫這些概念,在此不贅述。本地庫里面你會處於某個分支branch上,具體到你會在這條branch的某個版本HEAD,這個HEAD的名字不會變,但它可以移動到它的上一個版本。
而git status是很重要的一條命令,一般在執行git操作前都會看一下這條命令的提示(感覺可以這么說,看懂git status的提示,那基本上你就懂git操作了)。下面開始介紹:
Changes not staged for commit
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)
- 既然是Changes not staged for commit,就說明出現這個提示下的所有文件改動,都是存在於工作區的。stage是暫存區的意思,not stage說明都不在暫存區,那么說明在工作區。
- (use “git add …” to update what will be committed)。執行這條命令就可以工作區里面的改變加入到暫存區。可以執行
git add .把當前目錄下所有改動加入暫存區。 - (use “git checkout – <file>…” to discard changes in working directory)。執行這條命令將丟棄在工作區的改動。可以執行
git checkout *把當前目錄下所有工作區的改動丟棄掉。
Untracked files
Untracked files:
(use "git add <file>..." to include in what will be committed)
- Untracked files,就說明出現這個提示下的所有文件都是當前HEAD沒有被加入過的文件。這種改動也屬於工作區。
- (use “git add <file>…” to include in what will be committed)。把Untracked files加入暫存區。
Changes to be committed
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
- 既然是Changes to be committed,那么說明改動已經存在於暫存區,需要提交到本地庫上去。執行完
git commit以后,會把這條提示下面的所有改動作為一次commit提交到本地庫上去。 - (use “git reset HEAD <file>…” to unstage)。to unstage說明了這個改動已存在於暫存區,當然也存在於工作區。這條命令清空add命令向暫存區提交的關於file文件的修改。也就是說,只清空暫存區,但不動工作區。
Unmerged paths
Unmerged paths:
(use "git reset HEAD <file>..." to unstage)
(use "git add <file>..." to mark resolution)
- 這是我運行
git pull和git stash apply后,沖突的文件。在文件中把沖突都保留下來了,並需要自己手動處理。 - (use “git reset HEAD <file>…” to unstage)。分析同上。改動已在暫存區。
- (use “git add <file>…” to mark resolution)。在你手動修改后,(修改后工作區更新了,但暫存區還沒更新),需要add來更新暫存區。
沖突文件
沖突文件示例:
<<<<<<< Updated upstream
//來自git pull的更新
=======
//來自git stash apply的更新
>>>>>>> Stashed changes
branch信息
On branch master
Your branch is up to date with 'origin/master'.
你在當前分支master上,且你的分支和遠程庫origin的master分支是一致的,即本地庫的HEAD和遠程庫的HEAD是同一個。
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
你的本地分支比遠程分支領先了一次commit。
git reset的提示
Unstaged changes after reset
當前git status的提示是:本地庫的版本指向的是最新的這個commit。
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
執行git reset HEAD~后的提示為:
Unstaged changes after reset:
M first.java
D second.java
這條命令會把HEAD指向最新的commit之前的那個版本(清空掉最新的這個commit),同時會把暫存區的改變清空掉,這樣,改動就只存在於工作區了。Unstaged意思就是將以前已經stage的改動給Unstaged了。
