10

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?

2 Answers 2

26

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.

5
  • 1
    Why download the zp file if pull works ? Ideally there should be a way to sync the zip download without the damn pull.
    – Siddharth
    Commented Mar 23, 2015 at 17:39
  • i think we need pull to get the news in the project, which was downloaded some time ago. this is a quite common problem.
    – WebComer
    Commented Sep 25, 2016 at 23:20
  • @Siddharth I rewrote substantially the entire answer, it wasn't very well done. Even so, it would be better to know the specific errors and debug those, but I don't expect to get that now, 4+ years after the original question...
    – janos
    Commented Mar 24, 2018 at 7:06
  • This worked, but I'm getting every file in the repository wanting to commit changes, and the changed version is exactly the same as the original. So the file stays the same, but sourcetree in my case shows all lines in red and then the exact same lines in green. Where did I go wrong and how can I fix this?
    – Flion
    Commented Aug 3, 2022 at 10:16
  • 1
    @Flion when the diff view shows all lines changed but they look the same to the human eye, it's probably because the line ending is different. Most probably the files in the repo have \n as line ending, and in your local working tree it's \n\r. Try to remove \r from the line endings one file, and see if it helps. Then remove from all.
    – janos
    Commented Aug 4, 2022 at 5:44
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.