git - 如何更新我的 fork 使其具有与 github 上的原始存储库相同的分支和标签?

标签 git github

当您在 github 上创建一个仓库时,您创建的仓库包含所有分支和标签。

随着时间的推移,这些分支和标签会过时。

如何使用 fork 如此简单地确保您的 fork 具有所有分支和标签而无需重新克隆?

即一个 git magicpull --rebase upstream/* myremote/*

这将获取上游的所有分支和标签,并确保它们存在于 myremote 中。

最佳答案

这假设您的“上游” Remote 被命名为“origin”并且您的用户名下有您的自定义分支(即“maxandersen”)

当您让您的克隆运行以下单行代码时(Track all remote git branches as local branches 的刷新:

remote=origin ; for brname in `git branch -r | grep origin | grep -v master | grep -v HEAD | sed -e 's/.*\///g'`; do git branch --track $brname  $remote/$brname ; done

这将为远程名为“origin”的所有分支设置跟踪分支。 如果您已经使用此分支名称 checkout ,除了确保跟踪到位外,它不会更改任何内容。

(可选)现在确保您所有的分支都是最新的(如果您已经 checkout 分支则很有用):

git pull --rebase --all

现在所有分支都设置为跟踪和更新推送分支标签到您的远程(用您的远程名称替换'maxandersen'):

git push --all maxandersen
git push --tags maxandersen

在此之后,您的 fork 就同步了。

以下脚本执行所有这些操作,包括要求确认:

## Checkout all branches from remote as tracking branches. Based on    https://stackoverflow.com/questions/379081/track-all-remote-git-branches-as-local-branches/6300386#6300386

UPSTREAM=$1
MYREPO=$2

usage() {
   echo "Usage:"
   echo "$0 <upstream-remote> <target-remote>"
   echo ""
   echo "Example which ensures remote named 'maxandersen' have all the same branches and tags as 'origin'"
   echo "$0 origin maxandersen"
   exit 1
}

if [ -z "$UPSTREAM" ]
then
 echo Missing upstream remote name.
 usage
fi

if [ -z "$MYREPO" ]
then
 echo Missing target remote name. 
 usage
fi

read -p "1. This will setup '$MYREPO' to track all branches in '$UPSTREAM' - Are you sure ?" -n 1 -r

if [[ $REPLY =~ ^[Yy]$ ]]
then
 for brname in `git branch -r | grep "$UPSTREAM" | grep -v master | grep -v HEAD | sed -e 's/.*\///g'`; do git branch --track $brname  $UPSTREAM/$brname ; done
fi

read -p "2. This will push all local branches and tags into '$MYREPO' - Are you sure ?" -n 1 -r

if [[ $REPLY =~ ^[Yy]$ ]]
then
 git push --all $MYREPO
 git push --tags $MYREPO
fi

将其保存为“updateallbranchestags.sh”并执行:

sh updateallbranches.sh origin maxandersen

并且来自“origin”的所有分支/标签都将在名为“maxandersen”的远程中可用

关于git - 如何更新我的 fork 使其具有与 github 上的原始存储库相同的分支和标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15779740/

相关文章:

version-control - github公共(public) repo 有多公开?

git - 如何从命令行删除远程分支(例如 Github)?

git - 在 OS X 上使用 vi 添加 git 提交消息

git - 如果我已经开始 rebase ,如何将两个提交 merge 为一个?

amazon-web-services - Amazon AWS S3 的 GO 脚本运行时错误

git - ssh session 中的 ssh 未使用正确的 ssh 私钥

git - 在 Git 中,你如何检查你从命令行推送到 Github 中的哪个仓库?

git - 如何确保我的 git repo 代码是安全的?

git - 有人熟悉 git 错误 "missing object 0000000000000000000000000000000000000000 for refs/heads/..."吗?

git - 如何运行 git rebase 交互模式来删除重复提交