将本地目录与现有的 git 存储库同步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20415028/
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
syncing a local directory with an existing git repository
提问by user1457366
The title might sound like a duplicate but I couldn't find an answer to my particular problem.
标题可能听起来像重复,但我找不到我的特定问题的答案。
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 存储库。但是,当我尝试使用 shell 执行此操作时,出现错误。大多数文件正在被检出,但由于某种原因,其中一个图像文件引起了问题,我收到错误“致命:无法检出工作树”和“警告:克隆成功,但检出失败”。所以我现在所做的是从 github 下载 repo 文件作为 zip 文件并将它们解压缩到我的本地目录中。所以现在我的本地目录中有所有文件,但本地目录没有连接到 github 上的 repo。有没有一个命令可以完成这个?
回答by janos
The question seems incomplete. The output of git clone
in such cases should look like:
这个问题似乎不完整。git clone
在这种情况下的输出应如下所示:
<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'
<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'
First, the specific errors (in the messages above the fatal: ...
) should help understand what happened and how to fix it.
首先,特定错误(在 上方的消息中fatal: ...
)应该有助于了解发生了什么以及如何修复它。
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.
其次,您可以按照说明,运行git status
查看检查出的内容,然后尝试git checkout -f HEAD
修复它。
Some causes of warning: Clone succeeded, but checkout failed.
I've seen:
warning: Clone succeeded, but checkout failed.
我见过的一些原因:
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.
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.
存储库包含符号链接,您要检出的文件系统不支持符号链接。使用 GitHub 存储库的 zip,查看下面可能有帮助的替代方法。
存储库包含一些深层目录层次结构,因此在当前目录级别签出会导致路径太长,以至于您的本地文件系统不支持。尝试
git clone
使用您可以访问的最短路径。例如文件系统根。克隆成功后,您可以尝试将其移动到其他地方。如果这不起作用,那么下面的技术也不起作用,甚至不需要尝试。其他?最好包括在问题中。下面的技术可能有效,也可能无效,在不知道确切原因的情况下我无法判断。
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.
失败的结果git clone
应该是一个至少部分完整的工作树。由于存储库来自 GitHub,并且您可以下载内容的 zip,因此您可以将其解压缩到失败的克隆之上。其结果应该是一个内容匹配的工作树master
,并且您也将拥有 Git 历史记录,正如“克隆成功”消息所暗示的那样。git status
可能会报告一些更改,可能是由于符号链接被常规文件替换,或不同的文件系统权限,但似乎这是您想通过问题达到的状态。
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:
作为获取存储库历史记录的另一种替代方法,您可以将下载的 zip 初始化为新的 Git 存储库,添加远程存储库,获取并重置它:
unzip project.zip
cd project
git init
git remote add origin url_on_github
git fetch origin
git reset origin/master
git status
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 fetch
步骤将获取历史记录。如果自下载 zip 后 master 上没有新的提交,则工作树的内容应与origin/master
. 在git reset origin/master
将工作树和它对应的历史点之间的关联。
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 status
会告诉您工作树没有变化。情况可能并非总是如此,具体取决于git clone
. 您可以尝试使用 修复git checkout -f HEAD
,尽管它可能会因相同的错误而失败。
回答by Arenhag
Also check to see if the checkout fails due to excessive path lengths if you're on Windows.
如果您使用的是 Windows,还要检查结帐是否由于路径长度过长而失败。