How can I determine the URL that a local Git repository was originally cloned from?


Answers

Should you want this for scripting purposes, you can get only the URL with

git config --get remote.origin.url
Question

I pulled a project from GitHub a few days ago. I've since discovered that there are several forks on GitHub, and I neglected to note which one I took originally. How can I determine which of those forks I pulled?




I basically use:

git remote get-url origin

It works for git bash command console or cmd command console in Windows. That said it works with version 2.x of Git.




I think you can find it under .git/config and remote["origin"] if you didn't manipulate that.




To summarize, there are at least four ways:

(The following was tried for the official Linux repository)

Least information:

$ git config --get remote.origin.url
https://github.com/torvalds/linux.git

and

$ git ls-remote --get-url
https://github.com/torvalds/linux.git

More information:

$ git remote -v
origin    https://github.com/torvalds/linux.git (fetch)
origin    https://github.com/torvalds/linux.git (push)

Even more information:

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/torvalds/linux.git
  Push  URL: https://github.com/torvalds/linux.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)



I can never remember all the parameters to git commands, so I just put an alias in the ~/.gitconfig that makes more sense to me so I can remember it and it results in less typing.

[alias]
url = ls-remote --get-url

After reloading the terminal, you can then just type

> git url

Here are a few more of my frequently used ones:

[alias]
cd = checkout
ls = branch
lsr = branch --remote
lst = describe --tags



To get the answer:

git ls-remote --get-url [REMOTE]

This is better than reading the config; refer to the man page for git-ls-remote:

--get-url

Expand the URL of the given remote repository taking into account any "url.<base>.insteadOf" config setting (See git-config(1)) and exit without talking to the remote.

As pointed out by @Jefromi, this option was added in v1.7.5 and not documented until v1.7.12.2.




with git remote show origin you have to be in the projects directory. But if you want to determine the URLs from anywhere else you could use

cat <path2project>/.git/config | grep url

If you'll need this command often, you could define an alias in your .bashrc or .bash_profile with MacOS.

alias giturl='cat ./.git/config | grep url'

So you just need to call giturl in the git root folder in order to obtain simply its URL.


EDIT:

If you extend this alias like this

alias giturl='cat .git/config | grep -i url | cut -d'=' -f 2'

you get only the plain URL without the preceding

"url="

in

url=http://example.com/repo.git

you get more possibilities in its usage:

Example

On Mac you could call open $(giturl) to open the URL in the standard browser.

Or chrome $(giturl) to open it with the chrome browser on Linux.




Print arbitrarily named remote fetch URLs:

git remote -v | grep fetch | awk '{print $2}'



To get the IP address/hostname of origin

For ssh:// repositories:

git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d "/"

For git:// repositories:

git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d ":"