git 如何从另一个分支只获取一个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2364147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get just one file from another branch
提问by Nick Vanderbilt
I am using git and working on master branch. This branch has a file called app.js
.
我正在使用 git 并在 master 分支上工作。这个分支有一个名为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.
我有一个experiment
分支,我在其中进行了大量更改和大量提交。现在我想将所有所做的更改只带到app.js
fromexperiment
到master
分支。
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.
我再一次不想合并。我只想将所有更改app.js
从一个experiment
分支带到master
另一个分支。
回答by VonC
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?
Update August 2019, Git 2.23
2019 年 8 月更新,Git 2.23
With the new git switch
and git restore
commands, that would be:
使用 newgit switch
和git restore
命令,这将是:
git switch master
git restore -s experiment -- app.js
By default, only the working tree is restored.
If you want to update the index as well (meaning restore the file content, andadd it to the index in one command):
默认情况下,仅恢复工作树。
如果您还想更新索引(即恢复文件内容,并在一个命令中将其添加到索引中):
git restore -s experiment --staged --worktree -- app.js
# shorter:
git restore -s experiment -WS -- app.js
As Jakub Nar?bskimentions in the comments:
正如Jakub Nar?bski在评论中提到的:
git show experiment:path/to/app.js > path/to/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.
也可以工作,除了如 SO 问题“如何从 Git 中的特定修订版检索单个文件?”中详述的那样,您需要使用存储库根目录中的完整路径。
因此,Jakub 在他的示例中使用了 path/to/app.js。
As Frostymentions in the comment:
正如Frosty在评论中提到的:
you will only get the most recent state of app.js
你只会得到 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 checkout
or git show
,您实际上可以引用您想要的任何修订,如 SO 问题“ git gui 中文件的 git checkout 修订版”所示:
$ git show $REVISION:$FILENAME
$ git checkout $REVISION -- $FILENAME
would be the same is $FILENAME is a full pathof a versioned file.
与 $FILENAME 是版本化文件的完整路径相同。
$REVISION
can be as shown in git rev-parse
:
$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
and so on.
等等。
schmijosadds in the comments:
you also can do this from a stash:
git checkout stash -- app.js
This is very useful if you're working on two branches and don't want to commit.
你也可以从一个藏匿处做到这一点:
git checkout stash -- app.js
如果您在两个分支上工作并且不想提交,这将非常有用。
回答by Dmitry Avtonomov
Everything is much simpler, use git checkout for that.
一切都简单得多,为此使用 git checkout 。
Suppose you're on master
branch, to get app.js from new-feature
branch do:
假设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!
This will bring you the contents of the desired file. You can, as always, use part of sha1 instead ofnew-featurebranch nameto get the file as it was in that particular commit.
这将为您带来所需文件的内容。您可以一如既往地使用 sha1 的一部分而不是新功能分支名称来获取该特定提交中的文件。
Note:new-feature
needs to be a localbranch, not a remote one.
注意:new-feature
需要是本地分支,而不是远程分支。
回答by Sarath
git checkout branch_name file_name
Example:
例子:
git checkout master App.java
This will not work if your branch name has a period in it.
如果您的分支名称中有句点,这将不起作用。
git checkout "fix.june" alive.html
error: pathspec 'fix.june' did not match any file(s) known to git.
回答by AlexLordThorsen
Supplemental to VonC's and chhh's answers.
补充 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
or
或者
git checkout experiment -- app.js
回答by Jester
Or if you want all the files from another branch:
或者,如果您想要来自另一个分支的所有文件:
git checkout <branch name> -- .
回答by fearless_fool
Review the file on github and pull it from there
查看 github 上的文件并从那里拉取它
This is a pragmatic approach which doesn't directly answer the OP, but some have found useful:
这是一种不直接回答 OP 的实用方法,但有些人发现它很有用:
If the branch in question is on GitHub, then you can navigate to the desired branch and file using any of the many tools that GitHub offers, then click 'Raw' to view the plain text, and (optionally) copy and paste the text as desired.
如果有问题的分支在 GitHub 上,那么您可以使用 GitHub 提供的许多工具中的任何一个导航到所需的分支和文件,然后单击“原始”以查看纯文本,并(可选)将文本复制并粘贴为想要的。
I like this approach because it lets you look at the remote file in its entirety before pulling it to your local machine.
我喜欢这种方法,因为它可以让您在将远程文件拉到本地机器之前完整地查看它。
回答by arupjbasu
If you want the file from a particular commit (any branch) , say 06f8251f
如果您想要来自特定提交(任何分支)的文件,请说 06f8251f
git checkout 06f8251f path_to_file
git checkout 06f8251f path_to_file
for example , in windows:
例如,在 Windows 中:
git checkout 06f8251f C:\A\B\C\D\file.h
git checkout 06f8251f C:\A\B\C\D\file.h
回答by Santi
Another way is to create a patch with the differences and apply it in the master branch For instance. Let's say the last commit before you started working on app.js is 00000aaaaa and the commit containg the version you want is 00000bbbbb
另一种方法是创建具有差异的补丁并将其应用到主分支中。例如。假设您开始使用 app.js 之前的最后一次提交是 00000aaaaa,并且包含您想要的版本的提交是 00000bbbbb
The you run this on the experiment branch:
你在实验分支上运行这个:
git diff 00000aaaaa 00000bbbbb app.js > ~/app_changes.git
This will create a file with all the differences between those two commits for app.js that you can apply wherever you want. You can keep that file anywhere outside the project
这将创建一个文件,其中包含 app.js 的这两个提交之间的所有差异,您可以将其应用于任何您想要的地方。您可以将该文件保存在项目之外的任何位置
Then, in master you just run:
然后,在 master 中,您只需运行:
git apply ~/app_changes.git
now you are gonna see the changes in the projects as if you had made them manually.
现在您将看到项目中的更改,就像您手动进行更改一样。
回答by Rohit Saini
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>
Hope this will help you. Please let me know If you have any query.
希望这会帮助你。如果您有任何疑问,请告诉我。