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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15779740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:54:39  来源:igfitidea点击:

How to update my fork to have the same branches and tags as the original repository on github?

gitgithub

提问by Max Rydahl Andersen

When you fork a repository on github your forked repo contains all branches and tags.

当您在 github 上分叉存储库时,您分叉的存储库包含所有分支和标签。

Over time these branches and tags gets outdated.

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

How does one as easy it is with fork make sure your fork has all branches and tags without having to reclone ?

使用 fork 如何轻松确保您的 fork 拥有所有分支和标签,而无需重新克隆?

i.e. a git magicpull --rebase upstream/* myremote/*

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

which would fetch all the branches and tags in upstream and make sure the same are present in myremote.

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

采纳答案by Max Rydahl Andersen

This assumes your "upstream" remote is named "origin" and you have your custom fork under your username (i.e. "maxandersen")

这假设您的“上游”遥控器名为“origin”,并且您的用户名下有您的自定义叉(即“maxandersen”)

When you have your clone run the following one-liner (refresh of Track all remote git branches as local branches:

当你让你的克隆运行以下单行(刷新跟踪所有远程 git 分支作为本地分支

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

This will setup tracking branches for all the branches found in the remote named 'origin'. If you already have a checkout with this branchname it will not change anything except ensure the tracking is in place.

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

(Optional) Now ensure all your branches are uptodate (useful if you have already branches checked out):

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

git pull --rebase --all

Now with all branches setup for tracking and uptodate push branches andtags to your remote (replace 'maxandersen' with your remote name):

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

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

After this your fork is in sync.

在此之后,您的叉子是同步的。

The following script does all this including asking for confirmation:

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

## 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=
MYREPO=

usage() {
   echo "Usage:"
   echo "
sh updateallbranches.sh origin maxandersen
<upstream-remote> <target-remote>" echo "" echo "Example which ensures remote named 'maxandersen' have all the same branches and tags as 'origin'" echo "
git push -f origin refs/remotes/upstream/*:refs/heads/*
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

Save it as 'updateallbranchestags.sh' and execute it with:

将其另存为“updateallbranchestags.sh”并使用以下命令执行:

##代码##

And all branches/tags from 'origin' will be made available in remote named 'maxandersen'

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

回答by VonC

You would still need a local clone, which would:

您仍然需要一个本地克隆,它将:

upstream and fork

上游和分叉

  • git push --all origin(origin being your fork): that supposes that:
    • you have all the local branches tracking all the upstream branches (see previous step).
      Otherwise, you would push only one local branch by default, since a clone would create only one local branch (the default one)
    • you haven't pushed commits of your own on those branches.
  • git push --all origin(起源是你的叉子):假设:
    • 您拥有跟踪所有上游分支的所有本地分支(请参阅上一步)。
      否则,默认情况下您只会推送一个本地分支,因为克隆只会创建一个本地分支(默认)
    • 你还没有在这些分支上推送你自己的提交。

回答by Tamir Daniely

You can directly push the remote refs. Obviously this doesn't take into account changes in the fork, But it still answers the question. And if the changes are limited then you can easily merge/rebase them using your local branches.

您可以直接推送远程引用。显然这并没有考虑到fork的变化,但它仍然回答了这个问题。如果更改有限,那么您可以使用本地分支轻松合并/重新设置它们。

##代码##

回答by typelogic

Here https://asciinema.org/a/lv9v5mh1GGQJsfKIzUSlzsg7iis a no huzz-fuzz actual demo how I sync my cloned repo's masterand release-18.09branches from upstream.

这里https://asciinema.org/a/lv9v5mh1GGQJsfKIzUSlzsg7i是一个没有 huzz-fuzz 的实际演示,我如何从上游同步我克隆的 repomasterrelease-18.09分支。