Search code examples
gitgithubshallow-clone

Git shallow clone: only branches with activity last X months


There is a long running repository of a project that's much larger than I want or need. I shallow cloned the last few months but did not use --no-single-branch, so my local repo only knows about origin/master and I can't checkout any other branches.

From How do I "undo" a --single-branch clone? I know I can get ALL branches, or add one at a time.

ALl includes dozens of branches I don't need to know about and downloads too many MB. 1 at a time is a bit of pain.

My question is, how can I add all branches which have had activity in last x months (or x*100 commits) -- and skip everything else?

How can I get a list of Git branches, ordered by most recent commit? sorts branches by date, but doesn't work from shallow clone. Is there some way to get this list from GitHub instead from git itself? (Since that seems to require downloading everything.)


Solution

  • Here's a hack job, a demonstration of how one shouldn't do it because it doesn't use the API. However with this you don't need an API key so the incantation can be used by anyone.

    If you're happy with what active means as determined by Github:

    bash shell

    curl https://github.com/raspberrypi/linux/branches/active > branches-active.html
    grep 'data-branch-name' branches-active.html | sed -r 's/^.*data-branch-name="(.*?)"(.*$)/git remote set-branches --add origin \1/'
    

    emits:

    git remote set-branches --add origin rpi-5.4.y
    git remote set-branches --add origin rpi-5.3.y
    git remote set-branches --add origin legacy_screen_blanking_update
    git remote set-branches --add origin rpi-5.2.y
    git remote set-branches --add origin rpi-4.19.y-rt
    git remote set-branches --add origin rpi-5.1.y
    git remote set-branches --add origin rpi-5.0.y
    git remote set-branches --add origin rpi-4.20.y
    git remote set-branches --add origin avs2
    git remote set-branches --add origin rpi-4.14.y-rt
    

    If you want to fine tune from the most recent commit dates:

    grep 'time-ago' branches-active.html | sed -r 's/^.*datetime="(....-..-..).*$/\1/'
    
    2019-11-05
    2019-10-28
    2019-10-28
    2019-10-28
    2019-09-16
    2019-09-23
    2019-09-23
    2019-09-23
    2019-09-05
    2019-05-28
    

    Naive script (hardcoded, no args) which demonstrates this:
    https://gist.github.com/maphew/1b706e66e87919dbd2538f21b6ea9f26

    Sources: