54

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

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

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

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

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

4

4 回答 4

56

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

当您让您的克隆运行以下单行程序时(将所有远程 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

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

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

git pull --rebase --all

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

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

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

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

## 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”的远程中提供

于 2013-04-03T07:20:47.693 回答
21

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

上游和分叉

  • git push --all origin(起源是你的叉子):假设:
    • 您有所有本地分支跟踪所有上游分支(参见上一步)。
      否则,您将默认只推送一个本地分支,因为克隆只会创建一个本地分支(默认分支)
    • 你还没有在这些分支上推送你自己的提交。
于 2013-04-03T05:53:47.543 回答
4

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

git push -f origin refs/remotes/upstream/*:refs/heads/*
于 2017-12-24T12:58:31.073 回答
0

我已将Max出色答案中给出的 Bash 脚本翻译成 Batch 脚本,以防万一您在 Windows 上需要它。唯一的问题是,要在 Windows 上运行它,您需要在首选终端应用程序中提供 grep 和 sed 工具(我个人使用Scoop和 Windows Terminal)。

@echo off

set UPSTREAM=%~1
set MYREPO=%~2

if "%UPSTREAM%" == "" (
    echo Missing upstream remote name.
    goto :usage
)

if "%MYREPO%" == "" (
    echo Missing target remote name.
    goto :usage
)

set /p REPLY="Setup '%MYREPO%' to track all branches in '%UPSTREAM%' [y/n]? "

set "_YES=0"
if "%REPLY%" == "y" set "_YES=1"
if "%REPLY%" == "Y" set "_YES=1"
if %_YES% equ 1 (
    for /f "tokens=1" %%G in ('git branch -r ^| grep %UPSTREAM% ^| grep -v master ^| grep -v HEAD ^| sed -e "s/.*\///g"') do (
        git branch --track %%G %UPSTREAM%/%%G
    )
)

set /p REPLY="Push all local branches in '%MYREPO%' [y/n]? "

set "_YES=0"
if "%REPLY%" == "y" set "_YES=1"
if "%REPLY%" == "Y" set "_YES=1"
if %_YES% equ 1 (
    git pull --rebase --all
    git push --all %MYREPO%
    git push --tags %MYREPO%
)

exit /B %ERRORLEVEL%

:usage
echo Usage:
echo %~0 upstream-remote target-remote
echo Example which ensures remote named 'origin' have all the same branches and tags as 'upstream'
echo %~0 upstream origin
exit 1
于 2021-09-09T14:32:02.787 回答