是否可以在 Git 中只提取一个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16230838/
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
Is it possible to pull just one file in Git?
提问by Audrius Meskauskas
I am working on a Git branch that has some broken tests, and I would like to pull (merge changes, not just overwrite) these tests from another branch where they are already fixed.
我正在处理一个有一些损坏测试的 Git 分支,我想从另一个已经修复的分支中提取(合并更改,而不仅仅是覆盖)这些测试。
I know I can do
我知道我能做到
git pull origin that_other_branch
but this will attempt to merge lots of other files, for that I am not yet ready.
但这将尝试合并许多其他文件,为此我还没有准备好。
Is it possible to pull and merge only the specified file (and not everything) from that another branch?
是否可以仅从另一个分支中提取和合并指定的文件(而不是所有文件)?
This is not a duplicate of Git pull request for just one fileas all answers to that question are how to revert the locally changed file to the repository version, without changing any branches.
这不是对一个文件的Git 拉取请求的副本,因为该问题的所有答案都是如何将本地更改的文件恢复到存储库版本,而不更改任何分支。
采纳答案by aleroot
You can fetch and then check out only one file in this way:
您可以通过这种方式获取然后仅检出一个文件:
git fetch
git checkout -m <revision> <yourfilepath>
git add <yourfilepath>
git commit
Regarding the git checkout
command:<revision>
- a branch name, i.e. origin/master
<yourfilepath>
does not include the repository name (that you can get from clicking copy path
button on a file page on GitHub), i.e. README.md
关于git checkout
命令:<revision>
- 分支名称,即origin/master
<yourfilepath>
不包括存储库名称(您可以通过单击copy path
GitHub 上的文件页面上的按钮获得),即README.md
回答by Chris
Here is a slightly easier method I just came up with when researching this:
这是我在研究这个时想出的一个稍微简单的方法:
git fetch {remote}
git checkout FETCH_HEAD -- {file}
回答by Mawardy
git checkout master -- myplugin.js
master = branch name
myplugin.js = file name
master = 分支名称
myplugin.js = 文件名
回答by Luke Flournoy
@Mawardy's answer worked for me, but my changes were on the remote so I had to specify the origin
@Mawardy 的回答对我有用,但我的更改在遥控器上,所以我必须指定原点
git checkout origin/master -- {filename}
回答by Luke Flournoy
Yes, here is the process:
是的,过程如下:
# Navigate to a directory and initiate a local repository
git init
# Add remote repository to be tracked for changes:
git remote add origin https://github.com/username/repository_name.git
# Track all changes made on above remote repository
# This will show files on remote repository not available on local repository
git fetch
# Add file present in staging area for checkout
git check origin/master -m /path/to/file
# NOTE: /path/to/file is a relative path from repository_name
git add /path/to/file
# Verify track of file(s) being committed to local repository
git status
# Commit to local repository
git commit -m "commit message"
# You may perform a final check of the staging area again with git status