我正在使用git并在master分支上工作 . 该分支有一个名为 app.js 的文件 .
app.js
我有一个 experiment 分支,其中我进行了一系列更改和大量提交 . 现在我想将所有更改仅从 app.js 到 app.js 进行 master 分支 .
experiment
master
我怎么做?
再一次,我不想合并 . 我只想将 app.js 中的所有更改从 experiment 分支转移到 master 分支 .
补充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
git checkout branch_name file_name
例:
git checkout master App.java
一切都更简单,使用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!
这将为您带来所需文件的内容 . 您可以像往常一样获取该特定提交中的文件. use part of sha1 instead of new-feature branch name .
git checkout master # first get back to master git checkout experiment -- app.js # then copy the version of app.js # from branch "experiment"
另见git how to undo changes of one file?
正如Jakub Narębski在评论中提到:
git show experiment:path/to/app.js > path/to/app.js
同样有效,除了如SO问题“How to retrieve a single file from specific revision in Git?”中详细说明的那样,您需要使用repo根目录中的完整路径 .因此,Jakub在他的例子中使用的路径/ to / app.js .
正如Frosty在评论中提到:
您将只获得最新的app.js状态
但是,对于 git checkout 或 git show ,您实际上可以引用您想要的任何修订,如SO问题“git checkout revision of a file in git gui”中所示:
git checkout
git show
$ git show $REVISION:$FILENAME $ git checkout $REVISION -- $FILENAME
将是相同的是$ FILENAME是版本化文件的 full path .
$REVISION 可以如git rev-parse所示:
$REVISION
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
等等 .
schmijos添加in the comments:
你也可以从藏匿处执行此操作:git checkout stash - app.js如果您正在处理两个分支并且不想提交,那么这非常有用 .
或者,如果您想要来自另一个分支的所有文件:
git checkout <brachname> -- .
5 回答
补充VonC和chhh的答案 .
要么
例:
一切都更简单,使用git checkout .
假设 you're on master 分支,得到 app.js from new-feature 分支做:
这将为您带来所需文件的内容 . 您可以像往常一样获取该特定提交中的文件. use part of sha1 instead of new-feature branch name .
另见git how to undo changes of one file?
正如Jakub Narębski在评论中提到:
同样有效,除了如SO问题“How to retrieve a single file from specific revision in Git?”中详细说明的那样,您需要使用repo根目录中的完整路径 .
因此,Jakub在他的例子中使用的路径/ to / app.js .
正如Frosty在评论中提到:
但是,对于
git checkout
或git show
,您实际上可以引用您想要的任何修订,如SO问题“git checkout revision of a file in git gui”中所示:将是相同的是$ FILENAME是版本化文件的 full path .
$REVISION
可以如git rev-parse所示:等等 .
schmijos添加in the comments:
或者,如果您想要来自另一个分支的所有文件: