3 回答

TA貢獻1828條經驗 獲得超3個贊
git checkout master # first get back to master
git checkout experiment -- app.js # then copy the version of app.js
# from branch "experiment"
另請參閱git如何撤消一個文件的更改?
正如JakubNar?bski在評論中提到的那樣:
git show experiment:path/to/app.js > path/to/app.js
也可以工作,除了如SO問題“ 如何從Git中的特定修訂中檢索單個文件? ”中詳細說明的那樣,您需要使用repo根目錄中的完整路徑。
因此,Jakub在他的例子中使用了路徑/ to / app.js。
正如Frosty在評論中提到的那樣:
你只會獲得app.js的最新狀態
但是,對于git checkout
或者git show
,您實際上可以引用您想要的任何修訂,如SO問題“ git gui中文件的git checkout修訂版 ”所示:
$ git show $REVISION:$FILENAME$ git checkout $REVISION -- $FILENAME
將是相同的是$ FILENAME是版本化文件的完整路徑。
$REVISION
可以如下所示git rev-parse
:
experiment@{yesterday}:app.js # app.js as it was yesterday
experiment^:app.js # app.js on the first commit parent
experiment@{2}:app.js # app.js two commits ago
等等。
你也可以從藏匿處做到這一點:
git checkout stash -- app.js如果您正在處理兩個分支并且不想提交,那么這非常有用。

TA貢獻1815條經驗 獲得超6個贊
一切都更簡單,使用git checkout。
假設you're on master分支,得到app.js from new-feature分支做:
git checkout new-feature path/to/app.js
// note that there is no leading slash in the path!
這將為您帶來所需文件的內容。您可以像往常一樣使用sha1的一部分而不是 新功能 分支名來獲取該特定提交中的文件。

TA貢獻1827條經驗 獲得超4個贊
補充VonC和chhh的答案。
git show experiment:path/to/relative/app.js > app.js
# If your current working directory is relative than just use
git show experiment:app.js > app.js
要么
git checkout experiment -- app.js
- 3 回答
- 0 關注
- 1476 瀏覽
添加回答
舉報