简体   繁体   English

如何仅从git存储库中提取特定文件?

[英]How to pull only particular files from a git repository?

I have 2 repositories A and B. 我有2个存储库A和B。

I wanted to pull only some of the files from repository A to repository B. 我只想将某些文件从存储库A拉到存储库B。

Is there any way to do this? 有什么办法吗?

If you don't mind pulling the entire repository A (ie you aren't worried about saving on download time for some reason), you should be able to pull to another branch: 如果您不介意拉整个存储库A(即您不担心出于某种原因节省下载时间),则应该可以拉到另一个分支:

git pull --no-commit <URI for repository A> master:temp-branch

Then you can either merge temp-branch automatically or cherry-pick commits from it (if you really wanted to merge some patches instead of files, or if the files were added in one or more convenient commits). 然后,您可以自动合并temp-branch也可以从中合并“ temp-branch选择”提交(如果您确实想合并一些补丁而不是文件,或者文件是在一个或多个便捷的提交中添加的)。

Alternatively, if you don't want repository A's history, but having all its files somewhere is okay: 另外,如果您不希望存储库A的历史记录,但是可以将其所有文件放在某个位置也可以:

git clone --depth 1 <URI for repository A> somewhere

Git is not a file-oriented SCM. Git不是面向文件的SCM。 History is based on trees, so you can't ask Git to check out only a single file from a remote repository. 历史记录是基于树的,因此您不能要求Git从远程存储库中仅签出单个文件。

However, if you're using a repository browser interface (eg as provided by Github) or gitweb, you can certainly download individual raw files from the remote repository. 但是,如果您使用的是存储库浏览器界面(例如Github提供的界面)或gitweb,则可以从远程存储库下载单个原始文件。

One option, if you want to deal with files not commits, is to do it outside of git, ie with a setup of 如果您要处理文件而不是提交,一种选择是在git之外执行,即使用

files/project_1 # Includes a .git directory at this level.
files/project_2 # Same here.

you can do 你可以做

cp files/project_2/your_spec*.* files/project_1/

then 然后

cd files/project_1
git add .
git commit -m"msg"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM