Git 只获取一个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18323534/
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
Git fetch only one directory
提问by fredy
I have a developer that works on one folder and another that works on another. I would like to update the production with a specific folder I'm looking for a command like:
我有一个开发人员在一个文件夹上工作,另一个在另一个文件夹上工作。我想用一个特定的文件夹更新生产,我正在寻找一个命令,如:
cd /myproject
git pull myfolder
and expect that only myfolder will be updated
并期望只有 myfolder 会被更新
Is it possible?
是否可以?
ok, i'll rephrase... I have one project, one branch and two developers and two folders in the project. each developer works on one folder. both make changes and push them into the branch.
好的,我会改写...我有一个项目,一个分支和两个开发人员和项目中的两个文件夹。每个开发人员都在一个文件夹上工作。两者都进行更改并将它们推送到分支中。
I want to pull only changes of one folder into my local machine
我只想将一个文件夹的更改拉入我的本地机器
Basic/Simple isn't it?
基本/简单不是吗?
回答by John Szakmeister
Assuming what you're really asking is to remotely grab files from a git repository, without having a working tree of your own (or a clone of the repository locally), you could do this:
假设您真正想要的是从 git 存储库远程抓取文件,而没有您自己的工作树(或本地存储库的克隆),您可以这样做:
git archive --format=tgz --remote=<url-to-repo> master -- myfolder | tar zxvf -
This says to create a gzipped tar ball of the master branch of the repository
living at <url-to-repo>
, but include only the contents under myfolder
.
Piping that through tar
will unarchive the data and put it in the current
working directory.
这表示要创建位于 的存储库的 master 分支的 gzip 压缩 tar 球<url-to-repo>
,但仅包含myfolder
. 通过管道tar
将取消归档数据并将其放在当前工作目录中。
If you're actually trying to manage a working tree and work with the other two developers, then what you're asking to do really goes against the grain of the tool. But you'd have to be much more elaborate about what you're trying to accomplish in order for someone to make a good workflow suggestion.
如果您实际上是在尝试管理工作树并与其他两个开发人员一起工作,那么您要求做的事情确实与工具的本质背道而驰。但是您必须更详细地说明您要完成的工作,以便有人提出好的工作流程建议。
Update:
更新:
So it does sound as if you want to fetch just one folder's changes in a managed working tree. Sorry, but Git does not operate that way. As I said in the comments below, Git manages trees of files. It does not allow for part of a tree to be at one revision, and another part to be at a different revision like you're suggesting--perhaps you are a previous user of Subversion where this was possible?
所以听起来好像您只想在托管工作树中获取一个文件夹的更改。抱歉,但 Git 不会那样操作。正如我在下面的评论中所说,Git 管理文件树。它不允许树的一部分处于一个修订版,而另一部分则处于不同的修订版,就像您建议的那样 - 也许您是 Subversion 的以前用户,这是可能的?
You can get an effect that is similar to what you want, but you have to modify your working copy. I would not committhe modification unless you want to discard one of your developers work. You could to this:
您可以获得与您想要的效果类似的效果,但您必须修改您的工作副本。除非您想放弃您的开发人员之一,否则我不会提交修改。你可以这样:
$ git fetch
$ git checkout @{u} -- myfolder
git fetch
will bring your remote references up to date--it will fetch the
latest information from the servers. git checkout @{u} -- myfolder
will make
myfolder
look like what exists in the tracking branch. This has two
consequences:
git fetch
将使您的远程引用保持最新——它将从服务器获取最新信息。 git checkout @{u} -- myfolder
将
myfolder
看起来像跟踪分支中存在的内容。这有两个后果:
You do not have all the changes on your local branch (and you didn't want them, so that's okay), and
myfolder
will show as being modified because the content ofmyfolder
has been updated with the latest available from the remote repository, but it is not the same as your current revision.
您没有在本地分支上进行所有更改(并且您不想要它们,所以没关系),并且
myfolder
将显示为正在修改,因为 的内容myfolder
已更新为远程存储库中可用的最新版本,但与您当前的修订版本不同。
To get your working copy back to normal, do this:
要使您的工作副本恢复正常,请执行以下操作:
$ git checkout -- myfolder
From there, you call follow your usual mechanism for getting your branch
up-to-date with the remote (likely git pull
).
从那里,您按照通常的机制调用使您的分支与远程(可能git pull
)保持同步。
This, however, is probably not how you want to operate. Despite your comment, "doesn't matter the use case", it really does matter. There are often better ways to accomplish your overall goal and manage your development, but it requires understanding what you're trying to do. At the moment, what you propose really goes against how Git is intended to work.
然而,这可能不是您想要的操作方式。尽管您的评论是 “用例无关紧要”,但这确实很重要。通常有更好的方法来实现您的总体目标和管理您的开发,但这需要了解您正在尝试做什么。目前,您的提议确实与 Git 的工作方式背道而驰。
So let me put a few things out there for you. Research using topic branchesas a way to manage the development work being done, rather than using folders for each developer in the tree. Clear, logical changes can be made on branches and the merged as you test and approve them for inclusion into production.
所以让我为你准备一些东西。研究使用主题分支作为管理正在完成的开发工作的一种方式,而不是为树中的每个开发人员使用文件夹。当您测试和批准将分支和合并纳入生产时,可以对分支和合并进行清晰、合乎逻辑的更改。
A good place to start is here. It contains a fair amount of information itself, and some follow on links to several different styles of Git workflows. The place where you want to end is where there is a commit that represents the version you actually put into production, without destroying the work that others are doing for you. Ideally, you'd tag what was pushed into production in some meaningful way.
这里是一个很好的起点 。它本身包含相当多的信息,有些信息会跟随指向几种不同风格的 Git 工作流程的链接。您想要结束的地方是有一个代表您实际投入生产的版本的提交,而不破坏其他人为您所做的工作。理想情况下,您应该以某种有意义的方式标记推送到生产中的内容。
I know no one likes to hear it, but training may be very helpful in your case. GitHub offers some free online classesas well as several other resources. Judging from experience, once you grasp how Git works and the problems it solves, you'll end up with a much better workflow.
我知道没有人喜欢听它,但培训可能对您的情况非常有帮助。GitHub 提供了一些免费的在线课程以及其他一些资源。从经验来看,一旦你掌握了 Git 的工作原理和它解决的问题,你就会得到一个更好的工作流程。
回答by PherricOxide
Do you have some sort of production branch? Assuming a dev and production branch...
你有某种生产分支吗?假设有一个开发和生产分支......
# Get all the changes from the dev branch
git checkout dev
git pull origin dev
# Checkout the production branch
git checkout production
# Checkout the dev version of the folder you want
git checkout dev myfolder
# Commit all the changes that are now showing up.
This is sort of a terrible way to do things, because you're losing the change history when you checkout and recommit just that folder.
这是一种糟糕的做事方式,因为当您检出并重新提交该文件夹时,您将丢失更改历史记录。
回答by kmario23
Yes, it's easy to git fetch
only one git sub-directory. Suppose you have the following project directory in your remote git-repo.
是的,git fetch
只有一个 git 子目录很容易。假设您的远程 git-repo 中有以下项目目录。
project/
├── code
└── data
For the moment assume that you have cloned the project to your local filesystem as a git repository.
目前假设您已将项目作为 git 存储库克隆到本地文件系统。
$ cd project
$ rm -rf code
#now we try to fetch the "code" directory.
$ git fetch
$ git checkout HEAD code
In case you have nested directory structure like,
如果您有嵌套的目录结构,例如,
project/
├── code
│?? ├── dev
│?? └── test
└── data
Then in this case you should use the specific directory that you want,
那么在这种情况下,您应该使用所需的特定目录,
$ git checkout HEAD code/dev