在 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/