syncing a local directory with an existing git repository

前端 未结 2 1022
醉酒成梦
醉酒成梦 2021-02-09 10:30

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

相关标签:
2条回答
  • 2021-02-09 10:58

    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'
    

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

    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.


    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.

    • 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.


    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.


    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
    

    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.

    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.

    0 讨论(0)
  • 2021-02-09 11:01

    Also check to see if the checkout fails due to excessive path lengths if you're on Windows.

    0 讨论(0)
提交回复
热议问题