我有一个名为app.js的主分支。我在一个实验分支上对这个文件进行了更改。

我只想将实验中对app.js所做的更改应用到master分支。


当前回答

git checkout branch_name file_name

例子:

git checkout master App.java

如果分支名称中有句点,则此操作将不起作用。

git checkout "fix.june" alive.html
error: pathspec 'fix.june' did not match any file(s) known to git.

其他回答

补充VonC和chhh的答案:

git show experiment:path/to/relative/app.js > app.js

# If your current working directory is relative, then just use:
git show experiment:app.js > app.js

or

git checkout experiment -- app.js

或者,如果您想要其他分支的所有文件:

git checkout <branch name> -- .
git checkout branch_name file_name

例子:

git checkout master App.java

如果分支名称中有句点,则此操作将不起作用。

git checkout "fix.june" alive.html
error: pathspec 'fix.june' did not match any file(s) known to git.

要从另一个分支签出文件,只需简单的一行命令:

git checkout branch C:\path\to\file.cs

如果您想要多个文件

git checkout branch C:\path\to\file1.cs C:\path\to\file2.cs
git checkout <branch_name> -- <paths>

更多信息