gitgithub

Git - can I determine from the command line where a forked repo was forked from?


I've got a forked github repo on my machine, I'll call MYFORK.

It was forked from a repo I'll call MAIN_REPO

Is there a generic way from the command line to find out where MYFORK came from? ie the answer should be MAIN_REPO, not the url to my fork on github.

I'm open to git or command line github specific solutions, and am interested in solutions that don't require me to have manually added an "upstream" at some point, as implied here.


Solution

  • Nine years later, there is an easier answer. I'm not sure when it happened, but when you fork a repo on GitHub, it will set an additional remote called upstream.

    You can find the upstream information with:

    git remote show upstream
    

    In my case, I forked FiloSottile/age. Here is what I see for git remote show upstream:

    * remote upstream
      Fetch URL: git@github.com:FiloSottile/age.git
      Push  URL: git@github.com:FiloSottile/age.git
      HEAD branch: main
      Remote branches:
        debian/11        new (next fetch will store in remotes/upstream)
        filippo/detached new (next fetch will store in remotes/upstream)
        filippo/plugin   new (next fetch will store in remotes/upstream)
        main             tracked
        master           new (next fetch will store in remotes/upstream)
        tmp/61779        new (next fetch will store in remotes/upstream)
      Local ref configured for 'git push':
        main pushes to main (up to date)
    

    If you want to extract just the FiloSottile/age bit, you could use the following shell snippet:

    git remote show upstream | \
    awk '/Fetch URL/ {gsub(/^[^:]*:/,"",$3); gsub(/.git$/,"",$3); print $3}'