2016-10-14 95 views
1

如何在提交中顯示每個文件的文件內容?Git - 如何在提交中顯示每個文件的文件內容?

例如,說提交有20個文件已更改。現在,我爲每個文件使用git show *commit*:*file_path* 20次。

有沒有辦法在一個git命令中獲取所有這些信息?

+0

您是否在尋找已更改文件的*內容*或已更改文件的列表*? –

+0

更改文件的內容。我已經在使用'git diff --name-only commit'來獲取已更改文件的列表。 – ab2231ss

回答

1

最簡單的方法是檢查該提交,然後查看這些文件(並忽略任何其他文件)。請注意,您可以檢查出的不同的工作樹,例如,使用git worktree add(因爲Git版本2.5版,主要是可靠的2.6.x的,但最好是在2.8+大概):

$ git worktree add /tmp/tmpbranch HEAD~3 
Preparing /tmp/tmpbranch (identifier tmpbranch) 
HEAD is now at ... 
$ ... work with files in /tmp/tmpbranch ... 

你可以看到你有什麼用:

$ git worktree list 
/home/torek/[path] d22d10a [master] 
/tmp/tmpbranch  b6fc8a3 (detached HEAD) 

和清理是這樣的:

$ rm -rf /tmp/tmpbranch 
$ git worktree prune 

請注意,您可以在任何地方放置新的工作樹,但我會把它放在了當前工作-是一個(甚至在/ tmp像這樣)只是爲了避免混淆自己。

(我叫這個tmpbranch,但使用HEAD~3作爲提交說明符導致它變得超脫。不這樣做,將Git的已簽出的HEAD下的新分支名稱tmpbranch提交。給人一種分支的名稱作爲commit-除非你添加--detach來獲得分離的HEAD,使用原始提交哈希應該每次都得到一個分離的HEAD。)

+0

謝謝,我擔心我不得不這樣做。 – ab2231ss

+0

那麼,你也可以總是使用一個shell腳本循環:'在p1 p2 p3 ... pN中的路徑;做git show $ commit:$ path>/tmp/dir/$ path; done'。 'git show'的主要缺點是它不能通過過濾器(這也是它的主要優勢)運行文件。我只是認爲一個簡單的臨時工作樹更容易*。 – torek

0

你可以交互地重新綁定你的提交併壓扁它們。然後做git顯示那個提交哈希。舉例來說,你可以做到以下幾點:

git rebase -i HEAD~20 
# your terminal will display either nano editor or vim. The format will be as such 
# pick *commit_hash* *commit_message* 
# pick *commit_hash* *commit_message* 
# pick *commit_hash* *commit_message* 
# pick *commit_hash* *commit_message* 

#change all of your 'pick' to 's' (s means squash). Start from the bottom and go up 
#Save your changes on which ever editor you are using. Git will prompt you to name your squash commit 
# Then just do git show *commit_hash* 

希望這有助於!

+0

然後,您可以通過查看git reflog – gkalikapersaud

+0

來撤消更改。這是個好主意。我沒有這樣想過。我會嘗試一下。謝謝。 – ab2231ss

+0

讓我知道如果你有任何問題 – gkalikapersaud

相關問題