2014-03-25 75 views
4

讓我們從一個情境開始。git checkout之間的差異HEAD - 文件名和git簽出 - 文件名

  1. 我藏了一些變化(5檔)git stash
  2. 更改一些文件
  3. 提交更改git commit -m "Commit message"
  4. 什麼變化從藏匿回git stash apply

我收到了合併衝突在2個文件中,因爲提交。我不再需要這兩個文件中的更改所以我試圖恢復這些更改。

我做git checkout -- background.js

它失敗,錯誤

error: path 'src/background/background.js' is unmerged

但後來我讀this SO post,並試圖

git checkout HEAD -- background.js 

它的工作原理成功。這兩個文件都發生在我身上。我想了解bewteen

git checkout -- filegit checkout HEAD -- file

回答

4

的差別通常沒有太大的差別。關鍵是你有衝突要解決。從手冊頁http://csurs.csr.uky.edu/cgi-bin/man/man2html?1+git-checkout

git checkout [--patch] [] [--] ...

...

The index may contain unmerged entries because of a previous failed merge. By default, if you try to check out such an entry from the index, the checkout operation will fail and nothing will be checked out. Using -f will ignore these unmerged entries. The contents from a specific side of the merge can be checked out of the index by using --ours or --theirs. With -m, changes made to the working tree file can be discarded to re-create the original conflicted merge result.

您使用checkout這樣當它失敗。所以設計上,由於沒有合併的更改(不解決衝突),結賬將失敗。添加HEAD告訴git要使用哪個「分支」,因此會實際檢出文件。

HEAD指的是您在回購庫中的特定SHA。所以你要告訴git把文件從哪裏拉出來,就像你爲不同的分支一樣。

+0

+1非常感謝您的詳細解釋。對此,我真的非常感激。 – sachinjain024

+5

@blunderboy,簡而言之:'git checkout - '從索引中檢出名爲''*的內容*,而'git checkout HEAD - '從特定的提交中檢出它, HEAD'解析(在90%的情況下,它是您當前簽出分支的提示)。 – kostix

+0

@kostix我認爲你應該添加它作爲答案。簡單,直接和重點。並且請在你的回答中寫出1-2條關於git索引的文字,以使它更完整。我喜歡它。 – sachinjain024

相關問題