简体   繁体   English

Git clone 防止克隆工作副本(非裸)存储库

[英]Git clone prevent from cloning working copy (non bare) repository

If you do a clone of a git working copy (repository with a working tree) change some files, commit and try to push you'll get a message:如果您克隆 git 工作副本(带有工作树的存储库)更改一些文件,提交并尝试推送,您将收到一条消息:

remote: error: refusing to update checked out branch: refs/heads/master
...
! [remote rejected] master -> master (branch is currently checked out)

That's understandable and wanted behavior to me.这对我来说是可以理解和想要的行为。

I would like to prevent accidentally cloning a working copy of a repository.我想防止意外克隆存储库的工作副本。

How to prevent git clone from cloning working copies instead of remote bare repositories and signal an error in case of an attempt to clone a working copy?如何防止 git 克隆克隆工作副本而不是远程裸存储库,并在尝试克隆工作副本时发出错误信号?

Is there any command line switch that causes git clone non zero exit status in case of an attempt to clone working copy instead of a bare remote repository?如果尝试克隆工作副本而不是裸远程存储库,是否有任何命令行开关导致 git 克隆非零退出状态?

If not then how to check a repository location (url, or path to dir) if it contains a bare repository so I can test that in bash before cloning.如果不是,那么如何检查存储库位置(url 或目录的路径)是否包含裸存储库,以便我可以在克隆之前在 bash 中对其进行测试。

Please note that a working copy of a repository does not necessarily mean it is local because it can be shared by remote as well.请注意,存储库的工作副本并不一定意味着它是本地的,因为它也可以由远程共享。

In my case git clone should be allowed only to work with with git bare repositories and signal an error if used to clone working copy.在我的情况下,git 克隆应该只允许与 git 裸存储库一起使用,如果用于克隆工作副本,则会发出错误信号。

There is no way to prevent you from cloning a repository with a working tree.没有办法阻止您使用工作树克隆存储库。 When Git commits contents into a repository, they are available by accessing the .git directory or its equivalent without any inspection or manipulation of the working tree.当 Git 将内容提交到存储库时,可以通过访问.git目录或其等效目录来获得它们,而无需对工作树进行任何检查或操作。

This is actually extremely important for security because one of the only safe things you can do with an untrusted repository is to clone or fetch from it.这实际上对安全性非常重要,因为您可以对不受信任的存储库做的唯一安全的事情之一就是克隆或从中获取。 If you were prevented from doing that, there would be no way to get data from untrusted repositories in a safe way.如果您被阻止这样做,就无法以安全的方式从不受信任的存储库中获取数据。

You can check whether a local repository has a working tree by running git rev-parse --is-bare-repository in the repository in question (or using -C ).您可以通过在相关存储库中运行git rev-parse --is-bare-repository (或使用-C )来检查本地存储库是否具有工作树。 It will print true if it's bare (that is, lacks a working tree) and false if it's not.如果它是裸露的(即缺少工作树),它将打印true ,如果不是,它将打印false You cannot check this on a repository that is not accessible via a local file system (eg, an HTTPS or SSH remote) because that would involve you being able to inspect the remote system's file system and it would be a security problem if remote users could do that.您无法在无法通过本地文件系统访问的存储库上检查此内容(例如,HTTPS 或 SSH 远程),因为这将涉及您能够检查远程系统的文件系统,如果远程用户可以去做。

My recommendation is to not worry about this scenario too much instead of trying to fight Git on this.我的建议是不要太担心这种情况,而不是试图在这方面与 Git 抗争。 This behavior poses few practical problems and shouldn't typically be a cause for concern.这种行为几乎没有实际问题,通常不应该引起关注。

To augment bk2204's answer , you should think of the working tree as not part of the repository ... because in a pretty strong sense, it isn't part of the repository.为了增加bk2204 的答案,您应该将工作树视为不是存储库的一部分……因为在非常强烈的意义上,它不是存储库的一部分。 Since the work-tree isn't part of the repository, it never gets copied by git clone .由于工作树不是存储库的一部分,因此它永远不会被git clone That means there are no issues here.这意味着这里没有问题。

(Git's index, and files copied into the index, are, at least sort of, part of the repository, but the index itself is not copied. Blob objects—file contents—that were newly stored into the Git repository, via git add , but that have not yet been committed, could potentially be copied, although in general unreferenced objects should not get copied during cloning. I am pretty sure I found instances of unreferenced objects in a fresh clone at least once, back in the Git 1.5.5 / Git 1.6-ish days. But working tree files that have never been git add -ed have no representation at all in the repository databases, and those are what are copied during cloning.) (Git 的索引和复制到索引中的文件至少在某种程度上是存储库的一部分,但索引本身并未被复制。通过git add ,但尚未提交, 可能被复制, 尽管通常在克隆期间不应复制未引用的对象. 我很确定我在新克隆中找到了未引用对象的实例至少一次, 回到 Git 1.5.5 / Git 1.6 天。但是从未git add工作树文件在存储库数据库中根本没有表示,这些是在克隆期间复制的内容。)

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

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