简体   繁体   English

Git从特定目录结帐

[英]Git checkout from a specific directory

When we build our application, we fetch code from a directory of a CVS repository first then build it. 在构建应用程序时,我们首先从CVS存储库的目录中获取代码然后构建它。 So in Git, do we have anything similar to do it. 所以在Git中,我们有类似的东西吗? I just want to checkout from a specfic diectory of Git repo and then build our code. 我只想从一个特定的Git仓库处理结账,然后构建我们的代码。 No local repo maintainance, nothing. 没有本地仓库维护,没有。 Just code checkout. 只是代码结帐。

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并告诉它如何进行稀疏结账。

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! 这当然可以很容易地放在脚本中!

Git philosophy is miles away from CVS, so I suggest you to act differently here. Git哲学离CVS只有几英里远,所以我建议你采取不同的行动。 Your Git repositories will be distributed, every developper or server will have a copy of it, with one being authoritative among these ( origin remote in Git terms). 您的Git存储库将被分发,每个开发人员或服务器都将拥有它的副本,其中一个在这些中具有权威性( origin Git术语的远程)。

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存储库完全集成。

You can try cloning your repository using the --depth option, to see if it fits your need. 您可以尝试使用--depth选项克隆存储库,以查看它是否符合您的需要。 --depth option won't clone the full repository history, and thus, will be faster. --depth选项不会克隆完整的存储库历史记录,因此会更快。

> 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

If you want to checkout only a subdirectory of a git repository, AFAIK, it is not currently possible. 如果您只想签出一个git存储库AFAIK的子目录,那么它目前是不可能的。

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

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