如何从另一个分支获取一个文件 [英] How to get just one file from another branch

查看:68
本文介绍了如何从另一个分支获取一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用git并在master分支上工作。这个分支有一个名为 app.js 的文件。



我有一个 code>分支,我在其中进行了一系列更改和大量提交。现在我只想将 app.js 中的所有更改从 experiment 更改为 master



我该怎么做?

想要合并。我只想将 app.js 中的所有更改从 experiment 分支更改为 master 分支。

解决方案

  git checkout master#first get back to master 
git checkout experiment - app.js#然后将app.js
#的版本从分支experiment复制到

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



由于 JakubNarębski 在评论中提及:

  git show实验:path / to / app.js> app.js 

的作用也是如此,除了在SO问题如何从Git中的特定版本检索单个文件?,您需要使用repo根目录中的完整路径。

因此,Jakub在他的示例中使用了/ app / j.js路径。



由于 Frosty 在评论中提及:


您只会获得app.js的最新状态


但是,对于 git checkout git show ,你可以实际引用你想要的任何修订版本,如SO问题 git gui a>:

  $ git s $ REVISION:$ FILENAME 
$ git checkout $ REVISION - $ FILENAME

会是$ FILENAME是一个版本化文件的完整路径

>可以如 git rev-parse

  experiment @ {yesterday}:app.js #app.js,因为它是昨天
实验^:app.js#app.js第一次提交父母
实验@ {2}:app.js#app.js两次提交前

等等。


I am using git and working on master branch. This branch has a file called app.js.

I have an experiment branch in which I made a bunch of changes and tons of commits. Now I want to bring all the changes done only to app.js from experiment to master branch.

How do I do that?

Once again I do not want a merge. I just want to bring all the changes in app.js from experiment branch to master branch.

解决方案

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

See also git how to undo changes of one file?

As Jakub Narębski mentions in the comments:

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

works too, except that, as detailed in the SO question "How to retrieve a single file from specific revision in Git?", you need to use the full path from the root directory of the repo.
Hence the path/to/app.js used by Jakub in his example.

As Frosty mentions in the comment:

you will only get the most recent state of app.js

But, for git checkout or git show, you can actually reference any revision you want, as illustrated in the SO question "git checkout revision of a file in git gui":

$ git show $REVISION:$FILENAME
$ git checkout $REVISION -- $FILENAME

would be the same is $FILENAME is a full path of a versioned file.

$REVISION can be as shown in 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

and so on.

这篇关于如何从另一个分支获取一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆