用于更新文件夹中所有存储库的片段,尊重“main”与“master”

发布于 2025-01-10 03:50:02 字数 1539 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肩上的翅膀 2025-01-17 03:50:02

gh 版本将涉及通过 API 获取默认分支

gh api /repos/{owner}/{repo} --jq '.default_branch'

这样,您的远程存储库可以默认使用 master 或 main 或任何其他分支,您可以在本地切换到正确的分支

find . -maxdepth 1 -type d -exec sh -c \ 
  '(cd {} && \
    ownerRepo=$(git remote get-url origin) && \
    ownerRepo=${ownerRepo#https://github.com/} && \
    remoteBranch=$(gh api /repos/${ownerRepo} --jq '.default_branch') && \
    git switch ${remoteBranch} && git pull)' ';'

如果您不想依赖远程 GitHub API,您至少可以阅读 本地存储库默认分支

find . -maxdepth 1 -type d -exec sh -c \ 
  '(cd {} && \
    defaultBranch=$(git rev-parse --abbrev-ref origin/HEAD) && \
    defaultBranch=${defaultBranch#origin/} && \
    git switch ${defaultBranch} && git pull)' ';'

The gh version would involve getting the default branch through API.

gh api /repos/{owner}/{repo} --jq '.default_branch'

That way, your remote repository could use master or main or any other branch as default, you would switch to the right one locally

find . -maxdepth 1 -type d -exec sh -c \ 
  '(cd {} && \
    ownerRepo=$(git remote get-url origin) && \
    ownerRepo=${ownerRepo#https://github.com/} && \
    remoteBranch=$(gh api /repos/${ownerRepo} --jq '.default_branch') && \
    git switch ${remoteBranch} && git pull)' ';'

If you do not want to depend on a remote GitHub API, you can at least read the local repository default branch:

find . -maxdepth 1 -type d -exec sh -c \ 
  '(cd {} && \
    defaultBranch=$(git rev-parse --abbrev-ref origin/HEAD) && \
    defaultBranch=${defaultBranch#origin/} && \
    git switch ${defaultBranch} && git pull)' ';'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文