git - 如何在没有集线器的情况下自动查找 GitHub 上一个分支的上游 URL?

标签 git github

在 GitHub 上 fork 和克隆存储库后,通常是 configures an upstream remote for the fork :

$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git

有没有办法自动(以编程方式)找到上游 URL 而不使用 hub 这样我就可以为上述命令创建一个别名(或脚本)?

最佳答案

根据@Vampire 的回答,你可以在 ~/.bashrc 中添加以下 bash 函数:

function add_upstream() {
    url=$(git config --get remote.origin.url)
    repoWithOwnerExt=${url#*github.com[/:]}
    repoWithOwner=${repoWithOwnerExt%.git}
    remote=$(curl -s "https://api.github.com/repos/$repoWithOwner" | jq -r '.parent.clone_url')
    if [ "$remote" != "null" ]; then
        git remote add upstream "$remote"
    else
        echo "no upstream found"
    fi
}

它正在提取用户和 repo 值,并使用 jq解析 .parent.clone_url 的 JSON 解析器。

然后,将其与add_upstream 命令一起使用

关于git - 如何在没有集线器的情况下自动查找 GitHub 上一个分支的上游 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43015808/

相关文章:

git - 当 git repos 变大时,什么操作变慢了,为什么?

linux - 使用 Linux 和 Windows 时最好的 git 配置设置是什么?

xcode - 陷入 git 循环

git - 将存储在 github 上的项目部署到 debian 网络服务器

github - 查找漏洞的来源(在 package.json 中)(在 package-lock.json 中)

Git 无意中推送文件

wordpress - openshift + wordpress + git

Git: merge stash 了某些更改

php - CMS 可以由 Git 存储库提供支持吗?

Github V4 GraphQL API - 审计日志查询