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

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?

This question is related to git github git-remote

The answer is


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


I can never remember all the parameters to Git commands, so I just put an alias in the ~/.gitconfig file 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

I also highly recommend git-extras which has a git info command which provides much more detailed information on the remote and local branches.


easy just use this command where you .git folder placed

git config --get remote.origin.url

if you are connected to network

git remote show origin

it will show you the URL that a local Git repository was originally cloned from.

hope this help


Print arbitrarily named remote fetch URLs:

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

For me, this is the easier way (less typing):

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

actually, I've that into an alias called s that does:

git remote -v
git status

You can add to your profile with: alias s='git remote -v && git status'


A simple way is to open the .git/config file:

cat .git/config

To edit:

vim .git/config or

nano .git/config


You can try:

git remote -v

It will print all your remotes' fetch/push URLs.


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

git config --get remote.origin.url

The Git URL will be inside the Git configuration file. The value corresponds to the key url.

For Mac and Linux use the commands below:

 cd project_dir
 cat .git/config | grep url | awk '{print $3}'

For Windows open the below file in any text editor and find the value for key url.

project_dir/.git/config

Note: This will work even if you are offline or the remote git server has been taken down.


You cloned your repo with SSH clone.

git config --get remote.origin.url
[email protected]:company/product/production.git

But you want to get http url to open it in the browser or share it:

git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g'

https://gitlab.com/company/product/production.git

GitHub or GitLab doesn’t matter.


alias git-repo="git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g'"
alias git-pr="git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g' | sed 's/....$//' | sed -ne 's/$/\/pulls &/p'"

add this expresion to .zshrc or .bashrcs on main directory

after you can use like

git-repo
git-pr

Short answer:

$ git remote show -n origin

or, an alternative for pure quick scripts:

$ git config --get remote.origin.url

Some info:

  1. $ git remote -v will print all remotes (not what you want). You want origin right?
  2. $ git remote show origin much better, shows only origin but takes too long (tested on git version 1.8.1.msysgit.1).

I ended up with: $ git remote show -n origin, which seems to be fastest. With -n it will not fetch remote heads (AKA branches). You don't need that type of info, right?

http://www.kernel.org/pub//software/scm/git/docs/git-remote.html

You can apply | grep -i fetch to all three versions to show only the fetch URL.

If you require pure speed, then use:

$ git config --get remote.origin.url

Thanks to @Jefromi for pointing that out.


The upstream's remote may not be called "origin", so here's a variation:

remote=$(git config --get branch.master.remote)
url=$(git config --get remote.$remote.url)
basename=$(basename "$url" .git)
echo $basename

Or:

basename $(git config --get remote.$(git config --get branch.master.remote).url) .git

For more useful variables there's:

$ git config -l

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.


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 simply obtain its URL.


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.


To supplement the other answers: If the remote has for some reason been changed and so doesn't reflect the original origin, the very first entry in the reflog (i.e. the last entry displayed by the command git reflog) should indicate where the repo was originally cloned from.

e.g.

$ git reflog | tail -n 1
f34be46 HEAD@{0}: clone: from https://github.com/git/git
$

(Bear in mind that the reflog may be purged, so this isn't guaranteed to work.)


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)

To get the answer:

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

This is better than reading the configuration; 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 (2012-09).


With Git 2.7 (release January 5th, 2015), you have a more coherent solution using git remote:

git remote get-url origin

(nice pendant of git remote set-url origin <newurl>)

See commit 96f78d3 (16 Sep 2015) by Ben Boeckel (mathstuf).
(Merged by Junio C Hamano -- gitster -- in commit e437cbd, 05 Oct 2015):

remote: add get-url subcommand

Expanding insteadOf is a part of ls-remote --url and there is no way to expand pushInsteadOf as well.
Add a get-url subcommand to be able to query both as well as a way to get all configured URLs.

get-url:

Retrieves the URLs for a remote.
Configurations for insteadOf and pushInsteadOf are expanded here.
By default, only the first URL is listed.

  • With '--push', push URLs are queried rather than fetch URLs.
  • With '--all', all URLs for the remote will be listed.

Before git 2.7, you had:

 git config --get remote.[REMOTE].url
 git ls-remote --get-url [REMOTE]
 git remote show [REMOTE]

If you do not know the name of the upstream remote for a branch, you can look that up first by inspecting the upstream branch name that the current branch was built upon. Use git rev-parse like this:

git rev-parse --symbolic-full-name --abbrev-ref @{upstream}

This shows that upstream branch that was the source for the current branch. This can be parsed to get the remote name like this:

git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1

Now take that and pipe it to git ls-remote and you'll get the URL of the upstream remote that is the source of the current branch:

git ls-remote --get-url \
  $(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} | cut -d / -f 1)

Now it should be noted, that this is not necessarily the same as the source remote repository that was cloned from. In many cases however it will be enough.


#!/bin/bash

git-remote-url() {
 local rmt=$1; shift || { printf "Usage: git-remote-url [REMOTE]\n" >&2; return 1; }
 local url

 if ! git config --get remote.${rmt}.url &>/dev/null; then
  printf "%s\n" "Error: not a valid remote name" && return 1
  # Verify remote using 'git remote -v' command
 fi

 url=`git config --get remote.${rmt}.url`

 # Parse remote if local clone used SSH checkout
 [[ "$url" == git@* ]] \
 && { url="https://github.com/${url##*:}" >&2; }; \
 { url="${url%%.git}" >&2; };

 printf "%s\n" "$url"
}

Usage:

# Either launch a new terminal and copy `git-remote-url` into the current shell process, 
# or create a shell script and add it to the PATH to enable command invocation with bash.

# Create a local clone of your repo with SSH, or HTTPS
git clone [email protected]:your-username/your-repository.git
cd your-repository

git-remote-url origin

Output:

https://github.com/your-username/your-repository

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 ":"

I prefer this one as it is easier to remember:

git config -l

It will list all useful information such as:

user.name=Your Name
[email protected]
core.autocrlf=input
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/mapstruct/mapstruct-examples
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

git config --list

This command will give all information related to your repository.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to git-remote

Why does Git tell me "No such remote 'origin'" when I try to push to origin? Git - What is the difference between push.default "matching" and "simple" error: src refspec master does not match any How to connect to a remote Git repository? Changing the Git remote 'push to' default What does '--set-upstream' do? Git: How to remove remote origin from Git repo Git push error: "origin does not appear to be a git repository" How to add a local repo and treat it as a remote repo Git branching: master vs. origin/master vs. remotes/origin/master