简体   繁体   中英

Unable to fetch commit from github

The following commands fail to check out this commit :

$ git clone git@github.com:DefinitelyTyped/DefinitelyTyped.git
$ cd DefinitelyTyped
$ git checkout e90424c424ef9087d0589234e9e9cd0140bab0d7
fatal: reference is not a tree: e90424c424ef9087d0589234e9e9cd0140bab0d7

Attempting to follow the advice given here results in the following:

$ git fetch origin e90424c424ef9087d0589234e9e9cd0140bab0d7
$ echo $?
1

Is this failure something specific to GitHub / large repos on GitHub? My git version is 2.10.1.

** edit **

$ git fetch origin e90424c424ef9087d0589234e9e9cd0140bab0d7:refs/remotes/origin‌​/foo-commit
$ echo $?
1
$ git cat-file -p e90424c424ef9087d0589234e9e9cd0140bab0d7
fatal: Not a valid object name e90424c424ef9087d0589234e9e9cd0140bab0d7
$ git branch -a --contains e90424c424ef9087d0589234e9e9cd0140bab0d7 
error: no such commit e90424c424ef9087d0589234e9e9cd0140bab0d7
$ git reset e90424c424ef9087d0589234e9e9cd0140bab0d7
fatal: Could not parse object 'e90424c424ef9087d0589234e9e9cd0140bab0d7'.

The behavior you are seeing can happen if a repository is rebased. You can replicate this yourself by pushing a commit to a remote repository, then updating it with git commit --amend and (force) pushing the updated repository.

The old commit id will continue to exist in the remote repository until some sort of garbage collection runs, but because it is unreachable from any existing reference it will not be included in a git clone operation.

I suspect that the commit is not reachable from any ref (branch or tag) so the server isn't including it in the pack file it sends you.

In my tests, fetching a specific commit by SHA1 only seems to work if the commit is already in your local repo (which isn't too useful as far as I can tell; it does update FETCH_HEAD I guess)... I didn't even know it would work in that case.

Without direct access to the remote repo, I don't know a procedure to recover. (I'm not counting the idea of manually fetching each object individually and reconstructing the commit - and any missing history - locally; even if it can be done in theory, it's totally impractical.)

A little research suggests trying this command:

git archive -o repo.tar --remote=git@github.com:DefinitelyTyped/DefinitelyTyped.git e90424c424ef9087d0589234e9e9cd0140bab0d7

I'm not in a position to test this with any protocol but https, and it won't work with that protocol; but you might give it a shot.

You can check whether e90424c424ef9087d0589234e9e9cd0140bab0d7 commit is the part of current branch or not.

git branch -a --contains e90424c424ef9087d0589234e9e9cd0140bab0d7 

With this, you can see list of all branches which contains this hash commit.

Also with "checkout" option works with branch names not with hash commit. Using hash commit with checkout either results in "detached head state" or "fatal: reference is not a tree."

If you want to navigate to specific commit in your current branch use git reset with soft/hard options.

git reset e90424c424ef9087d0589234e9e9cd0140bab0d7
git reset --hard e90424c424ef9087d0589234e9e9cd0140bab0d7

This will update your HEAD pointer to specific commit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM