How to fetch an orphaned commit?

Question:

tl;dr: I have a repository cloned but cannot see an orphaned commit locally. How can I get that commit if it did not come with the repo?

Details:

I am trying to get the contents of a specific file on a specific commit using gitpython as below (using Git 2.17.1):

repo.git.show('{}:{}'.format(12cf56252dcb1535e0fbeb9c3e3586551af671ea, '%s/%s' % ('versions/library-2.6' ,'Dockerfile')) but I am getting this error:

"Cmd(‘git’) failed due to: exit code(128) cmdline: git show 12cf56252dcb1535e0fbeb9c3e3586551af671ea:versions/library-2.6/Dockerfile stderr: ‘fatal: Path ‘versions/library-2.6/Dockerfile’ does not exist in ’12cf56252dcb1535e0fbeb9c3e3586551af671ea”"

The path actually exists in that commit:
link to the commit

However, a warning appears on the page saying: "This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository."

How can I get the contents of that file on this commit using gitpython?

Asked By: smgtkn

||

Answers:

To fetch an orphaned commit:

git fetch origin <full-40-char-sha>
# for example:
git fetch origin 12cf56252dcb1535e0fbeb9c3e3586551af671ea

This assumes the commit still exists in the repo you are fetching from (and that your remote is called origin). For many service based SCM tools, including GitHub and Azure DevOps, by default, pushed commits are never garbage collected and will remain reachable via their full hash, forever.

Answered By: TTT
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.