Git:只签出没有存储库的文件?

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

Git: Checkout only files without repository?

git

提问by Max

i'd like to just checkout the files without the .git files and the whole repository. It's because i'd like to manage a website (php & html) with git and i'm looking for an easy way to update the files in the htdocs folder from the repository, without having the repository public. (now it's in the home-dir and is accessed via ssh, but i have to put the new files to htdocs manually.

我只想签出没有 .git 文件和整个存储库的文件。这是因为我想用 git 管理一个网站(php 和 html),我正在寻找一种简单的方法来更新存储库中 htdocs 文件夹中的文件,而无需公开存储库。(现在它在 home-dir 中并通过 ssh 访问,但我必须手动将新文件放入 htdocs。

采纳答案by Jakub Nar?bski

The git-archivemanpage contains the following example:

git的存档手册页包含了下面的例子:

git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)

Create a tar archive that contains the contents of the latest commit on the current branch, and extract it in the '/var/tmp/junk' directory.

git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)

创建一个包含当前分支上最新提交内容的 tar 存档,并将其解压缩到“/var/tmp/junk”目录中。

Or you can use low level git-checkout-index, which manpage contains the following example:

或者您可以使用低级git-checkout-index,其中联机帮助页包含以下示例:

Using git checkout-index to "export an entire tree"

The prefix ability basically makes it trivial to use 'git checkout-index' as an "export as tree" function. Just read the desired tree into the index, and do

   $ git checkout-index --prefix=git-export-dir/ -a

git checkout-indexwill "export" the index into the specified directory.

The final "/" is important. The exported name is literally just prefixed with the specified string.

使用 git checkout-index 来“导出整棵树”

前缀功能基本上使得使用 ' git checkout-index' 作为“导出为树”函数变得微不足道。只需将所需的树读入索引,然后执行

   $ git checkout-index --prefix=git-export-dir/ -a

git checkout-index将索引“导出”到指定目录中。

最后的“/”很重要。导出的名称实际上只是以指定的字符串作为前缀。

Or you can try to use --work-treeoption to git wrapper, or GIT_WORK_TREE environment variable, e.g. by using "git --work-tree=/somwehere/else checkout -- .".

或者您可以尝试使用--work-treegit 包装器选项或 GIT_WORK_TREE 环境变量,例如使用“ git --work-tree=/somwehere/else checkout -- .”。

回答by seanhodges

Git is much easier than Subversion for this, as the whole repository is held in a single directory. You only need to delete the hidden ".git" folder in the root of the project to create a production-ready copy of your site.

在这方面,Git 比 Subversion 容易得多,因为整个存储库都保存在一个目录中。您只需要删除项目根目录中隐藏的“.git”文件夹即可创建站点的生产就绪副本。

In Linux/OSX this would be:

在 Linux/OSX 中,这将是:

mkdir dist && cd dist
git checkout --depth=1 http://path/to/your/project.git
rm -rf .git

回答by Jon Spencer

git clone --depth 1does exactly what you want. I have a git repository with 41,000 commits and this is vastly faster. See this answeror this more general responsefor a more detailed explanation.

git clone --depth 1做你想要的。我有一个包含 41,000 次提交的 git 存储库,这要快得多。有关更详细的解释,请参阅此答案此更一般的回复

If you want to specify the branch to use (instead of the default "master" branch) use this syntax (requires Git 1.9+):

如果要指定要使用的分支(而不是默认的“master”分支),请使用以下语法(需要 Git 1.9+):

git clone -b <remoteBranch> --single-branch --depth 1 ssh://[email protected]:serverport/PathToProject <FolderName>

回答by Sven Marnach

You could create a tar archive in a git working directory and copy the tar to the server:

您可以在 git 工作目录中创建一个 tar 存档并将 tar 复制到服务器:

git archive -o foo.tar HEAD
scp foo.tar server:

回答by siliconrockstar

Expanding on seanhodges answer, you could just rsync the project's files to wherever you need to put them and use --exclude to avoid the .git directory.

扩展 seanhodges 答案,您可以将项目文件 rsync 到您需要放置它们的任何位置,并使用 --exclude 来避免 .git 目录。

rsync -avz --exclude=.git /source/directory/ [email protected]:/target-directory

I didn't test that command so feel free to edit it, but you get the jist.

我没有测试那个命令,所以可以随意编辑它,但你明白了。

A useful option is to add --dry-run to the end of the command, that will show you what files will be sent without actually making any changes.

一个有用的选项是将 --dry-run 添加到命令的末尾,这将显示将发送哪些文件而不实际进行任何更改。

回答by Slam

You'd probably benefit from a bare repo plus post-receive hook.

您可能会从裸仓库和接收后挂钩中受益。

This method is useful if you desire to push changes to your server but don't want the htdocs themselves under version control.

如果您希望将更改推送到服务器但不希望 htdoc 本身处于版本控制之下,则此方法很有用。

We'll make a folder for the bare git repo. I like to put it in the same parent folder as htdocs:

我们将为裸 git 存储库创建一个文件夹。我喜欢把它和 htdocs 放在同一个父文件夹中:

mkdir barerepo.git
cd barerepo.git
git --bare init 

Then create a post-receive hook file, make it executable:

然后创建一个 post-receive hook 文件,使其可执行:

touch hooks/post-receive
chmod ug+x hooks/post-receive

Edit post-receive in your favorite text editor:

在您最喜欢的文本编辑器中编辑接收后:

GIT_WORK_TREE=/full/path/to/htdocs git checkout -f
# optional stuff:
cd down/to/some/directory
[do some stuff]

Now every time you push to this bare repo it will checkout the working tree to htdocs. But htdocs itself is not under version control; running git statusin htdocs will give the error fatal: Not a git repository (or any parent up to mount point /data). It's just plain files.

现在,每次推送到这个裸仓库时,它都会将工作树检出到 htdocs。但是 htdocs 本身不受版本控制;git status在 htdocs 中运行会出现错误fatal: Not a git repository (or any parent up to mount point /data)。这只是普通文件。

Note that you must always push from master for this to work otherwise git checkoutwon't know what branch to checkout.

请注意,您必须始终从 master 推送才能使其工作,否则git checkout将不知道要检出哪个分支。