以下git命令有什麼區別?這些git diff命令有什麼區別?
git diff HEAD
git diff HEAD^
git diff --cached
或同義詞git diff --staged
git diff
以下git命令有什麼區別?這些git diff命令有什麼區別?
git diff HEAD
git diff HEAD^
git diff --cached
或同義詞git diff --staged
git diff
git diff HEAD
- 顯示發生了什麼變化,因爲上次提交。git diff HEAD^
- 顯示自提交之前發生了什麼變化最新的提交。git diff --cached
- 通過git add
顯示已添加到索引的內容,但尚未提交。git diff
- 通過git add
顯示已更改但尚未添加到索引中的內容。它看起來像這樣:
Working
Directory <----+--------+------+
| | | |
| | | |
V | | |
"git add" | | |
| diff | |
| | | |
V | | |
Index <----+ diff HEAD |
| | | |
| | | |
V | | |
"git commit" | | |
| diff --cached | |
| diff --staged | |
V | | |
HEAD <----+--------+ |
| |
| diff HEAD^
V |
previous "git commit" |
| |
| |
V |
HEAD^ <--------------------+
HEAD
是在樹的當前HEAD
指針,HEAD^
是HEAD
之前提交。
--cached
我不確定。
--cached
會顯示您所做的任何更改,但尚未添加到索引中。
kernal.org上的git tutorial是一個很好的閱讀。
你有'--cached'稍有不妥:它表明你只改變了你*有*添加到索引(但沒犯)。 – Amber 2010-09-11 02:39:23
git diff HEAD
:HEAD和工作目錄之間的差異。git diff HEAD^
:HEAD的直接祖先和工作目錄之間的差異。git diff --cached
或同義詞git diff --staged
:HEAD和索引之間的差異。git diff
:索引與工作目錄之間的差異。有沒有人有一個很好的方法來記住這一點?我一直回到這裏。 – 2015-09-13 22:48:00
@kevin比做數獨遊戲好嗎? – Samuel 2017-08-09 22:34:42
git diff
會告訴你在還沒有上演的工作目錄改變下一次提交。
git diff --cached
會告訴你該指數之間的差異上次提交;如果在沒有「-a」選項的情況下運行「git commit」,你會提交什麼。
git diff HEAD
顯示,因爲在工作目錄更改上次提交;如果你運行「git commit -a」你會提交什麼。
這裏有一個簡單的方法來記住這些命令:
git diff
的源被認爲是工作 目錄,並將其目標是索引。--cached
標誌將源更改爲索引。一旦添加更多參數,它不一定會改變目標。但爲了方便起見,如果沒有提供目標,目標將成爲最後的提交。玩得開心把它們組合起來,以自己的喜好:)
@Picasso它是手繪的還是您使用過一些ASCII藝術工具?有沒有任何git命令呢? ;) – takeshin 2010-09-10 20:41:19
@takeshin:手繪;這並不難。 (但是有一個用於繪製ASCII藝術的emacs模式,並不是我使用它。) – Amber 2010-09-11 02:38:28
僅針對ascii-art。一個圖像= 1000個字;一個ascii-art = 1025個字符。 – 2014-03-07 15:48:38