Skip to content

Find a git repo on your system by its remote URL

Louis Maddox edited this page Jan 23, 2018 · 4 revisions

If you cloned a repo but can't find it on your system, surely you can figure out where it is?

  1. list all git repos by searching for directories called .git
  • better yet, make an index of all such directories
  1. use git remote show origin to show the repo source (i.e. GitHub URL: user/org and repo name)

  • findafile is a function: find / -iname "$@" 2> /dev/null

  • findafile .git lists all git repos

  • findafile .git | head -4 returns a list (to figure it out I'm just getting a few with head)

    /gits/getpaperstmp/.git
    /gits/grc-issues-backup/.git
    /gits/euler/.git
    /gits/websiteresources/.git
    
  • git-config is the tool to query a repo's .git/config file, which has a line with the URL in, e.g. for this repository wiki (yes, you can clone a GitHub wiki!)

    [core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [remote "origin"]
            url = git@github.com:lmmx/devnotes.wiki.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
            remote = origin
            merge = refs/heads/master
    
    • get the parameter by calling git config --get remote.origin.url if inside the directory above .git/,
    • or point it at the directory using the --git-dir flag: git --git-dir=devnotes.wiki/.git config --get remote.origin.url
  • So... to do this for a list, such as the one I took the head of above, apply a call to xargs,

    findafile .git | head -4 | xargs -I {} git --git-dir={} config --get remote.origin.url
  • an improvement would be to print out say, tab-separated columns of the directory and the remote URL, but I can't quite see how to do this

    • if you rename the {} argument by placing a variable name to use after it and replacing the --git-dir flag parameter with said word, the word isn't parsed (e.g. declaring 'gitdir' as the variable reports "xargs: gitdir: No such file or directory")
  • since you can't (?) do that, it's fine to just run the call twice (kinda defeats the purpose of calling xargs but whatever - for the purpose I'm writing this for, I just want to grep to see if a certain repo URL exists, i.e. if I have it on my computer, so it'll do)

Fin!

Extension: keeping a list to query

Clone this wiki locally