Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syncing a local directory with an existing git repository

Tags:

git

github

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?

like image 931
user1457366 Avatar asked Dec 06 '13 02:12

user1457366


1 Answers

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.

like image 89
janos Avatar answered Oct 25 '22 19:10

janos