将本地目录与现有git存储库同步 [英] syncing a local directory with an existing git repository

查看:109
本文介绍了将本地目录与现有git存储库同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题听起来像是重复的,但我找不到我的特殊问题的答案.

The title might sound like a duplicate but I couldn't find an answer to my particular problem.

我想在本地计算机上克隆一个git存储库.但是,当我尝试使用Shell进行此操作时,出现错误.大多数文件都被检出,但是由于某种原因,其中一个图像文件引起了麻烦,并且我收到错误消息:致命:无法检出工作树"和警告:克隆成功,但检出失败".因此,我现在要做的是从github以zip文件格式下载回购文件,并将其解压缩到我的本地目录中.所以现在我所有的文件都在本地目录中,但是本地目录未连接到github上的仓库.有命令可以完成此任务吗?

I want to clone a git repository on my local machine. However, when I try to do this using the shell, I am getting an error. Most of the files are being checked out but for some reason one of the image files is causing trouble and I am getting the error "fatal: unable to checkout working tree" and "warning: clone succeeded, but checkout failed". So what I did now is download the repo files from github as a zip file and unpack them into my local directory. So now I have all the files in my local directory but the local directory is not connected to the repo on github. Is there a command that will accomplish this?

推荐答案

问题似乎不完整.在这种情况下,git clone的输出应如下所示:

The question seems incomplete. The output of git clone in such cases should look like:

<some errors specific to your situation here>
...
fatal: unable to checkout working tree
warning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry the checkout with 'git checkout -f HEAD'

首先,特定错误(在fatal: ...上方的消息中)应该有助于了解发生了什么以及如何解决.

First, the specific errors (in the messages above the fatal: ...) should help understand what happened and how to fix it.

第二,您可以按照说明进行操作,运行git status以查看已检出的内容,然后尝试git checkout -f HEAD对其进行修复.

Second, you could follow the instructions, run git status to see what was checked out, and then try git checkout -f HEAD to fix it.

某些原因导致warning: Clone succeeded, but checkout failed.我已经看到:

Some causes of warning: Clone succeeded, but checkout failed. I've seen:

  • 存储库包含符号链接,而您要检出的文件系统不支持符号链接.

  • The repository contains symbolic links, and the filesystem where you want to checkout doesn't support symbolic links. See an alternative below that might help, using a zip of the GitHub repo.

存储库包含一些深层目录层次结构,例如,在当前目录级别进行检出将导致路径太长,从而导致本地文件系统不支持.尝试git clone到您可以访问的最短路径.例如文件系统根目录.克隆成功后,您可以尝试将其移动到其他位置.如果那不起作用,则以下技术也将不起作用,甚至无需尝试.

The repository contains some deep directory hierarchies, such that checkout at the current directory level will result in too long paths such that your local filesystem doesn't support. Try git clone to the shortest path you have access to. For example the filesystem root. Once the clone is successful, you can try to move it somewhere else. If that doesn't work, then the technique below won't work either, no need to even try.

其他?最好将其包括在问题中.我无法在不知道确切原因的情况下告诉您以下技术是否有效.

Other? It would be best to include in the question. The technique below may or may not work, I cannot tell without knowing the exact cause.

git clone失败的结果应该是至少部分完成的工作树.由于存储库来自GitHub,并且您可以下载内容的zip,因此可以将其解压缩到失败克隆的顶部.这样的结果应该是一个工作树,其内容与master匹配,并且您还将具有Git历史记录,如克隆成功"消息所暗示的那样. git status可能会报告某些更改,这可能是由于符号链接已由常规文件替换,或者是文件系统权限不同,但这似乎是您要解决的问题状态.

The result of the failed git clone should be an at least partially complete working tree. Since the repository comes from GitHub, and you can download the zip of the content, you could unzip that on top of the failed clone. The result of that should be a working tree whose content matches master, and you will have the Git history too, as implied by the "Clone succeeded" message. git status may report some changes, possibly due to symbolic links replaced by regular files, or different filesystem permissions, but it seems this is the state you wanted to reach with your question.

作为获取存储库历史记录的另一种方法, 您可以将下载的zip初始化为新的Git存储库, 添加远程存储库,获取并重置为它:

As yet another alternative way to get the repository history, you could initialize the downloaded zip as a new Git repository, add the remote repository, fetch, and reset to it:

unzip project.zip
cd project
git init
git remote add origin url_on_github
git fetch origin
git reset origin/master
git status

git fetch步骤将获取历史记录.如果自下载zip以来,master上没有新的提交,则工作树的内容应与origin/master中的最新内容相同. git reset origin/master将在工作树和它所对应的历史记录中的点之间建立关联.

The git fetch step will get the history. If there were no new commits on master since you downloaded the zip, then the content of the working tree should be the same as the latest as of origin/master. The git reset origin/master will make the association between the working tree and the point in the history it corresponds to.

理想情况下,git status会告诉您工作树没有更改. 取决于git clone失败的原因,情况可能并非总是如此.您可以尝试使用git checkout -f HEAD进行修复,尽管它可能会因相同的错误而失败.

Ideally git status will tell you that the working tree has no changes. That may not always be the case, depending on the cause of the failure of git clone. You could try to fix with git checkout -f HEAD, though it's likely to fail with the same error.

这篇关于将本地目录与现有git存储库同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆