1

Say I have a list of strings like so:

remotes/origin/master
remotes/origin
origin/master
origin

how can I tell if the beginning of the string is a valid remote? For example,

remotes/origin
origin

should both be valid remotes, in general, right? How to determine that though?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • `remotes/origin` is very probably not a valid remote name, in what context are you trying to use this? – jthill Aug 11 '18 at 00:21

3 Answers3

3

Hokay, to answer the actual question (instead of the one I saw-what-I-expected-to-see from the examples):

git config --local --get-regexp "^remote\\.$yourname\\.url" >&- \
&& echo it\'s a remote

should serve nicely. I carry aliases for frequently-used option combos, the one I'd be using here is

git config --global alias.grl '!f() { git config --local --get-regexp "${@-.}"; }; f'

so the test I'd actually use for my own work would be

git grl "^remote\\.$yourname\\.url" >&-  && echo it\'s a remote

To test for a remote-tracking-branch name, the question I answered on my first attempt, it's

[[ `git rev-parse --symbolic-full-name $anything` = refs/remotes/* ]] \
&& echo it\'s a remote

or

case `git rev-parse --symbolic-full-name $anything` in
refs/remotes/*)  echo yes ;;
esac

if you you're not using bash.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • This tests for a valid *remote-tracking name*, but not for a valid *remote* (which matches the examples, but not the question itself). Note that double square brackets is a bash construct: in old POSIX sh you can use `case`. – torek Aug 10 '18 at 23:34
  • Gak. I saw the examples and kinda read what I expected to read as the question. – jthill Aug 10 '18 at 23:54
  • p.s. I'd forgotten `[[` is a bashism, thanks for the reminder @torek – jthill Aug 11 '18 at 00:20
2

origin is a real git remote - a construct that contains URI of remote repository your local clone interacts with.

origin/master OR remotes/origin/master refer to version of master branch available on remote origin, these two can be used interchangeably as per this answer.

I'd recommend checking out this answer for more details (your question may be a duplicate?).

As for the answer to 'how do I check if this is a valid remote' - there's no built-it way that I'm aware of but this thread lists some nice ideas how to chain git commands to check.

orhtej2
  • 2,133
  • 3
  • 16
  • 26
2

This is a strange way to look at names in git... Any name goes. All your examples could be local branches. It would be unnecessarily confusing but nothing keeps you from naming a local branch

remotes/something

Then if you push it to your upstream, your remotes list will show something like

remotes/remotes/something

I'd say from a contextless string you can't assume anything.

To check if some string matches one of your repo's remotes

git branch -r | grep yourString

should do the trick.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • right, so how would you check to see if a given string is a git remote for that repo? – Alexander Mills Aug 10 '18 at 22:40
  • 1
    @AlexanderMills have a look [here](https://stackoverflow.com/questions/8223906/how-to-check-if-remote-branch-exists-on-a-given-remote-repository), sadly (?) no accepted answer there. – orhtej2 Aug 10 '18 at 22:41
  • 1
    @orthej2 Great answer there, thanks for the link :-) However, it adresses a specific context (no locally updated remotes). `git branch -r` is simpler if available. – Romain Valeri Aug 10 '18 at 22:42