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

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


当前回答

git checkout master               # first get back to master
git checkout experiment -- app.js # then copy the version of app.js 
                                  # from branch "experiment"

另请参见git如何撤消对一个文件的更改?


2019年8月更新,Git 2.23

使用新的git switch和git restore命令,将是:

git switch master
git restore --source experiment -- app.js

默认情况下,仅恢复工作树。如果您也想更新索引(即恢复文件内容,并在一个命令中将其添加到索引中):

git restore --source experiment --staged --worktree -- app.js
# shorter:
git restore -s experiment -SW -- app.js

正如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 gui中文件的git checkout-revision”所示:

$ 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签出存储--app.js如果您正在处理两个分支,并且不想提交,这非常有用。

其他回答

git checkout master               # first get back to master
git checkout experiment -- app.js # then copy the version of app.js 
                                  # from branch "experiment"

另请参见git如何撤消对一个文件的更改?


2019年8月更新,Git 2.23

使用新的git switch和git restore命令,将是:

git switch master
git restore --source experiment -- app.js

默认情况下,仅恢复工作树。如果您也想更新索引(即恢复文件内容,并在一个命令中将其添加到索引中):

git restore --source experiment --staged --worktree -- app.js
# shorter:
git restore -s experiment -SW -- app.js

正如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 gui中文件的git checkout-revision”所示:

$ 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签出存储--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 checkout <branch name> -- .

另一种方法是创建具有差异的补丁,并将其应用于主分支例如。假设您开始使用app.js之前的最后一次提交是00000aaaa,而包含所需版本的提交是00000bbbbb

你在实验分支上运行这个:

git diff 00000aaaaa 00000bbbbb app.js > ~/app_changes.git

这将为app.js创建一个包含这两个提交之间所有差异的文件,您可以在任何地方应用该文件。您可以将该文件保存在项目之外的任何位置

然后,在master中,您只需运行:

git apply ~/app_changes.git

现在,您将看到项目中的更改,就像您手动进行了更改一样。

git checkout <branch_name> -- <paths>

更多信息