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

Multi tool use


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?
git remote get-url origin
I'm sure you meant
git remote get-url origin
.– shiri
Feb 25 '16 at 16:33
git remote get-url origin
codingaffairs.blogspot.com/2017/01/…
– Hammad Tariq
Jan 19 '17 at 10:55
git remote get-url origin
does not work for me--possibly deprecated? git remote show origin
worked though.– Klik
Oct 25 '17 at 16:47
git remote get-url origin
git remote show origin
git remote -v
give you a lot of information, including this.– Thorbjørn Ravn Andersen
Dec 10 '17 at 12:21
git remote -v
19 Answers
19
If you want only the remote URL, or referential integrity has been broken:
git config --get remote.origin.url
If you require full output or referential integrity is intact:
git remote show origin
When using git clone
(from GitHub, or any source repository for that matter) the default name for the source of the clone is "origin". Using git remote show
will display the information about this remote name. The first few lines should show:
git clone
git remote show
C:UsersjaredparVsVim> git remote show origin
* remote origin
Fetch URL: git@github.com:jaredpar/VsVim.git
Push URL: git@github.com:jaredpar/VsVim.git
HEAD branch: master
Remote branches:
If you want to use the value in the script, you would use the first command listed in this answer.
Use git config as described below instead if using jgit with amazon-s3.
– barryku
Mar 29 '12 at 17:20
Although not relevant to the purpose of the original question, please note that if attempting to get the "Push URL" and multiple URLs are entered for the remote specified, you'll either need to use
git remote show origin
(optionally with the -n flag provided by @Casey), or with git remote -v
as suggested by @Montaro and @rodel.– Amazingant
Jul 24 '14 at 14:39
git remote show origin
git remote -v
What does "if referential integrity has been broken" mean? There is a comment to another answer mention a case where the remote URL is "not available anymore": is this it? where "not available" meaning "not reachable"??
– Eric Lebigot
Jul 15 '15 at 12:49
@JaredPar What do you mean by referential integrity?
– Akshayraj Kore
Jan 29 '16 at 20:53
I find it staggering that this question has been up-voted so many times and viewed a crazy amount of times. A positive testament to Stack-overflow, and a negative one to Git
– Mads Boyd-Madsen
Jul 18 '17 at 14:23
Should you want this for scripting purposes, you can get only the URL with
git config --get remote.origin.url
This is. Way faster than the solution above.
– K-RAN
Mar 23 '13 at 2:10
This is the correct answer. It is way faster and it even works, if the remote url is not available anymore (
git remote show origin
just shows "conq: repository does not exist.").– apfelbox
May 22 '13 at 6:58
git remote show origin
This is not quite the right answer because of the config option
url.<base>.insteadOf
. See my answer - git has a command for this purpose.– arcresu
Jun 2 '13 at 5:17
url.<base>.insteadOf
@arcresu Cool, +1 to you! In my defense, that command wasn't added until March 2011, and it wasn't documented until September 2012.
– Cascabel
Jun 2 '13 at 6:03
Doesn't seem to work with --git-dir or --work-tree
– Catskul
Mar 21 '14 at 20:25
You can try:
git remote -v
It will print all your remotes' fetch/push URLs.
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
:
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.
"url.<base>.insteadOf"
git-config(1)
As pointed out by @Jefromi, this option was added in v1.7.5 and not documented until v1.7.12.2 (2012-09).
good one : this also would provide the same for previous versions > git remote -v| grep fetch|awk '{print $2}'
– ravi.zombie
Jul 17 '15 at 20:26
With Git 2.7 (release January 5th, 2015), you have a more coherent solution using git remote
:
git remote
git remote get-url origin
(nice pendant of git remote set-url origin <newurl>
)
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):
mathstuf
gitster
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.
insteadOf
ls-remote --url
pushInsteadOf
get-url
get-url:
Retrieves the URLs for a remote.
Configurations for insteadOf
and pushInsteadOf
are expanded here.
By default, only the first URL is listed.
insteadOf
pushInsteadOf
--push
--all
Before git 2.7, you had:
git config --get remote.[REMOTE].url
git ls-remote --get-url [REMOTE]
git remote show [REMOTE]
After 5 years a lot has changed and this should be the accepted answer now. But maybe add the pre-2.7 syntax as well.
– msparer
Dec 14 '15 at 8:17
@msparer I have updated the answer.
– VonC
Jan 5 '16 at 8:03
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)
Note
git config --get remote.origin.url
retrieves the original URL which was set with git remote add ...
or git remote set-url ...
while git ls-remote --get-url origin
retrieves the URL which is actually used to access the remote - which might be different in presence of git config --global url.XXX.insteadOf YYY
. So both outputs may differ! Also note that git ls-remote --get-url
(without origin
) does not neccessarily retrieve origin
, instead it shows the tracked upstream, so it will fail for example in detached HEAD state.– Tino
Dec 7 '14 at 9:27
git config --get remote.origin.url
git remote add ...
git remote set-url ...
git ls-remote --get-url origin
git config --global url.XXX.insteadOf YYY
git ls-remote --get-url
origin
origin
Short answer:
$ git remote show -n origin
or, an alternative for pure quick scripts:
$ git config --get remote.origin.url
Some info:
$ git remote -v
$ git remote show origin
origin
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?
$ git remote show -n origin
-n
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.
| grep -i fetch
If you require pure speed, then use:
$ git config --get remote.origin.url
Thanks to @Jefromi for pointing that out.
I think you can find it under .git/config
and remote["origin"]
if you didn't manipulate that.
.git/config
remote["origin"]
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
origin
For ssh://
repositories:
ssh://
git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d "/"
For git://
repositories:
git://
git ls-remote --get-url origin | cut -f 2 -d @ | cut -f 1 -d ":"
For
ssh
this only works in absence of ~/.ssh/config
which rewrites the hostname or alias.– Tino
Dec 7 '14 at 9:33
ssh
~/.ssh/config
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.
git reflog
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.)
This is probably the only real way to get the "original" remote if it has been changed.
– Justin Ohms
Jul 20 '16 at 23:57
But it doesn't show you the URL of the current used remote. To show the actuual used URL, you would need this solution then: stackoverflow.com/a/40630957/1069083
– rubo77
Nov 16 '16 at 11:28
The Git URL will be inside the Git configuration file. The value corresponds to the key url
:
url
cd project_dir
cat .git/config | grep url | awk '{print $3}'
Print arbitrarily named remote fetch URLs:
git remote -v | grep fetch | awk '{print $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:
git remote show origin
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.
.bashrc
.bash_profile
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.
giturl
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.
open $(giturl)
Or chrome $(giturl)
to open it with the Chrome browser on Linux.
chrome $(giturl)
A simple way is to open the .git/config
file:
.git/config
cat .git/config
To edit:
vim .git/config
or
vim .git/config
nano .git/config
nano .git/config
This presumes you are on Linux(?).
– Peter Mortensen
4 mins ago
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
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
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.
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.
So... that is what I mentioned 2 years ago above: stackoverflow.com/a/32991784/6309
– VonC
Nov 14 '17 at 11:39
No problem. I was just surprised by the upvote on this answer.
– VonC
Nov 14 '17 at 11:53
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:
~/.gitconfig
[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
#!/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 "%sn" "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 "%sn" "$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 git@github.com:your-username/your-repository.git
cd your-repository
git-remote-url origin
Output:
https://github.com/your-username/your-repository
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
With git 2.7 (Q4 2015),
git remote get-url origin
will be possible. See my answer below– VonC
Oct 7 '15 at 12:08