从 git repo 中只提取一个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10124223/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:25:52  来源:igfitidea点击:

Pulling just one directory out of a git repo

gitgit-pullgit-checkoutgit-fetch

提问by Dan

I have a git repo that I want to do a pull from. I do a normal git pullwith no problems. The issue is that I want just one certain directory out of the repo. My thinking was that I could use a .gitignorefile with a rule like this:

我有一个 git repo,我想从中提取。我做一个正常git pull的没有问题。问题是我只想要 repo 中的一个特定目录。我的想法是我可以使用.gitignore具有以下规则的文件:

#Ignore all files
/
#Except the one I want
!/temp

The problem is this doesn't work. Is that the right way to do it or is there a better way?

问题是这行不通。这是正确的方法还是有更好的方法?

回答by c00kiemon5ter

git pullfetches and merges the remote branch.

git pull获取并合并远程分支。

.gitignoreworks only locally, and will hide matching entries from showing up on git statusand being added to the index with git add. It's not what you want.

.gitignore仅在本地工作,并将隐藏匹配的条目,git status使其不显示并添加到带有git add. 这不是你想要的。

What you want to do, is fetchthe remote branch, and from that, extract the dir/file you need.

您想要做的是fetch远程分支,然后从中提取您需要的目录/文件。

$ git fetch <remote> <branch>
$ git checkout <remote>/<branch> -- relative/path/to/file/or/dir

the file/dir should now be in your branch and added to the index.

文件/目录现在应该在您的分支中并添加到索引中。