来自特定目录的 Git 签出

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

Git checkout from a specific directory

gitgit-checkout

提问by Ahmad

When we build our application, we fetch code from a directory of a CVS repository first then build it. So in Git, do we have anything similar to do it. I just want to checkout from a specfic diectory of Git repo and then build our code. No local repo maintainance, nothing. Just code checkout.

当我们构建应用程序时,我们首先从 CVS 存储库的目录中获取代码,然后构建它。那么在 Git 中,我们是否有类似的事情要做。我只想从 Git 存储库的特定目录中签出,然后构建我们的代码。没有本地回购维护,什么都没有。只需代码结帐。

I know there is something called "Sparse checkout". Do we have anything simpler?

我知道有一种叫做“稀疏结账”的东西。我们还有更简单的吗?

My problem is that this build script can be run on any server. So if I choose Sparse checkout, I will have to go on each server ,set-up Git and and tell it how to do sparse checkout.

我的问题是这个构建脚本可以在任何服务器上运行。所以如果我选择稀疏结账,我将不得不在每台服务器上,设置 Git 并告诉它如何进行稀疏结账。

回答by CharlesB

To answer litteraly to your question, you'd have to do the following:

要完全回答您的问题,您必须执行以下操作:

Retrieve repo data in a temporary directory

在临时目录中检索 repo 数据

mkdir path/to/temporary && cd path/to/temporary
git clone <repo-url> --depth 1 --bare

And checkout the directory you want in another one

并在另一个目录中签出您想要的目录

git --work-tree=/path/to/checkout checkout HEAD -- sub/directory

You can then delete the temporary and work with the checkout.

然后,您可以删除临时文件并进行结帐。

This can be easily put in a script of course!

这当然可以很容易地放在脚本中!

回答by CharlesB

Git philosophy is miles away from CVS, so I suggest you to act differently here. Your Git repositories will be distributed, every developper or server will have a copy of it, with one being authoritative among these (originremote in Git terms).

Git 哲学与 CVS 相去甚远,所以我建议你在这里采取不同的行动。您的 Git 存储库将被分发,每个开发人员或服务器都将拥有它的副本,其中一个是权威的(originGit 术语中的远程)。

When you want to start a build from a fresh checkout, you first say Git to grab the latest changes from origin, this is called a fetch.

当您想从新的检出开始构建时,您首先说 Git 从 获取最新更改origin,这称为获取。

git fetch origin

Then, to have a fresh checkout, you want your filesystem to be exactly the same as the server version, without any other file.

然后,要重新结帐,您希望文件系统与服务器版本完全相同,没有任何其他文件。

git checkout --force origin/master
git clean -d --force

The advantage here is that the network operation is very efficient, you grab only the latest diffs from the server you want to sync with, and you're still sure that you're building from the right files, without them being changed.

这里的优点是网络操作非常高效,您只从要与之同步的服务器获取最新的差异,并且您仍然确定您是从正确的文件构建的,而不会对其进行更改。

You'll want to check also tools for Continuous Integration, like Jenkins, which quite well integrated with Git repositories.

您还需要检查持续集成工具,例如 Jenkins,它与 Git 存储库很好地集成在一起。

回答by Guillaume Darmont

You can try cloning your repository using the --depthoption, to see if it fits your need. --depth option won't clone the full repository history, and thus, will be faster.

您可以尝试使用该--depth选项克隆您的存储库,看看它是否符合您的需要。--depth 选项不会克隆完整的存储库历史记录,因此会更快。

> git clone http://myrepo.git --depth 1

> git clone http://myrepo.git --depth 1

You can further restrict the checkout by specifying a branch name :

您可以通过指定分支名称来进一步限制结帐:

> git clone http://myrepo.git -b master --depth 1

> git clone http://myrepo.git -b master --depth 1

If you want to checkout only a subdirectory of a git repository, AFAIK, it is not currently possible.

如果您只想签出 git 存储库 AFAIK 的子目录,目前是不可能的。