1

I need only the server part, for example, if my project is on git@192.168.1.1:project.git, I want git@192.168.1.1

Thanks,

Nevik Rehnel
  • 49,633
  • 6
  • 60
  • 50
eaglebh
  • 21
  • 4
  • 1
    If you just want to use git for that, have a look at [this question](http://stackoverflow.com/questions/4089430/how-can-i-determine-the-url-that-a-local-git-repo-was-originally-cloned-from). – speakr Apr 15 '13 at 14:18
  • Note a machine might have more than one ip – mmmmmm Apr 15 '13 at 14:19
  • Ummm....git is a **distributed** version control system, so there is no such thing as a single authoritative server for a given repo. –  Apr 15 '13 at 14:20
  • @jack: but you may have a remote branch on which you local branch tracks changes. I think it's fair to assume, that he talks about that remote. If such a remote branch is not given... well, then you don't know which URL to look for. – tessi Apr 15 '13 at 14:41
  • @eaglebh which platform/programming languages is/are available for your solution? Having python, ruby or something better than bash, would definitely help. This may we might even be able to avoid regexes ;) – tessi Apr 15 '13 at 15:09
  • Jack: I know that, but on this case we have just one server, that have one address when accessed from internal network, and other when accessed externaly. Philipp: I'm on Windows and Linux, but on Windows I still have bash with Git Bash. @all: thanks for the help, with all the info and some bash skills I got it done now. – eaglebh Apr 17 '13 at 16:07

2 Answers2

3

The following bash command gives you the server part for the remote called origin:

echo `git config --get "remote.origin.url"` | sed "s|\([^:]*\):.*|\1|"

If you are not sure if the remote for the current branch is always called origin, you may execute the following to find the remote of the current branch:

git config --get "branch.master.remote"
tessi
  • 13,313
  • 3
  • 38
  • 50
  • 1
    I admit, that the regex needs some work. It probably does not work with URLs containing colons (IPv6, user:pass@server.com, ...). Any ideas for a better regex appreciated :) – tessi Apr 15 '13 at 14:55
  • The problem with this happens when we have for example: ssh://git@192.16.10.3:2222/project.git – eaglebh Apr 17 '13 at 16:16
1

Given a url, you just parse the url.

Here's how to find the url of one of your own repo's remotes

If you mean you want to find "the" url for your own repo, that won't work. It all depends on whether or not somebody's fired up a server at a particular address; nothing says there can't be multiples -- and you can use 'file:' urls without any server at all.

Community
  • 1
  • 1
jthill
  • 55,082
  • 5
  • 77
  • 137
  • Thanks. Using that with the command below I could get what I wanted: – eaglebh Apr 17 '13 at 16:02
  • repodir=`git rev-parse --show-toplevel` myself=`basename $repodir` urlbase=`git config --get remote.origin.url|sed "s/$myself\.git//"` – eaglebh Apr 17 '13 at 16:10