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

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


当前回答

补充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 C:\path\to\file.cs

如果您想要多个文件

git checkout branch C:\path\to\file1.cs C:\path\to\file2.cs

补充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 master               -go to the master branch first
git checkout <your-branch> -- <your-file> --copy your file data from your branch.

git show <your-branch>:path/to/<your-file> 

希望这对你有所帮助。如果你有任何疑问,请告诉我。

一切都简单多了,用git结账。

假设您在master分支上,要从新功能分支获取app.js,请执行以下操作:

git checkout new-feature path/to/app.js

// note that there is no leading slash in the path!

这将为您带来所需文件的内容。您可以像往常一样,使用sha1的一部分而不是新的功能分支名称来获取特定提交中的文件。

注意:新功能需要是本地分支,而不是远程分支。

查看github上的文件并从那里取出

这是一种务实的方法,不会直接回答OP,但有些人发现很有用:

如果有问题的分支位于GitHub上,那么您可以使用GitHub提供的许多工具中的任何一个导航到所需的分支和文件,然后单击“原始”查看纯文本,并(可选)根据需要复制和粘贴文本。

我喜欢这种方法,因为它允许您在将远程文件拉到本地计算机之前查看其完整性。