简体   繁体   中英

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.)

Fundamentally, Git has no idea when a commit was unless Git has the commit itself: the two date-and-time-stamps, for author and committer, are part of the commit.

This means that any sort of sorting or selection based on the committer timestamp requires that, well, you have the commit. So you'll need to get every tip commit of every branch that you intend to feed into the sorting algorithm to sort by date, or to feed into the selector.

Your only other option is to get some other Git—which already has those commits—or system to do the sorting or selecting for you, or to hand over the date stamps (with or without the rest of the commit). There's nothing built in to standard Git to get less than the whole commit, 1 but various providers have APIs by which you can obtain the date stamps. To use those, you'll have to step outside Git proper. (Your question is only tagged ; add to get GitHub-specific answers, or just go browse their API since the answers are in there...)


1 There is something in the works, through what Git developers are calling "promisor packs". They're not ready for prime time though.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM