## 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
@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
4条答案
按热度按时间baubqpgj1#
这假定您的“上游”远程名为“origin”,并且您的用户名下有您的自定义分支(即“maxandersen”)
当你让你的克隆运行下面的一行代码(刷新将所有远程git分支作为本地分支跟踪:
这将为在名为“origin”得远程目录中找到得所有分支设置跟踪分支.如果您已经使用此分支名称 checkout ,则除了确保跟踪已就绪外,不会更改任何内容.
(可选)现在,确保所有分支都是最新的(如果您已 checkout 分支,则这很有用):
现在,所有分支都已设置为可跟踪,并将最新的分支 * 和 * 标记推送到您的远程(将'maxandersen'替换为您的远程名称):
在此之后,您的fork将处于同步状态。
以下脚本执行所有这些操作,包括要求确认:
将其另存为“updateallbranchestags.sh”并使用以下命令执行:
来自'origin'的所有分支/标记将在名为'maxandersen'的远程中可用
dphi5xsq2#
您仍然需要本地克隆,它将:
请参阅“How do I clone all remote branches with Git?“,了解有关将所有远程分支设置为本地分支的详细信息。
我使用了问题“Track all remote git branches as local branches“中的一行程序。
另请参阅“What is the difference between
origin
andupstream
in GitHub“git push --all origin
(原始为您的分支):假设:否则,默认情况下只推送一个本地分支,因为克隆只创建一个本地分支(默认分支)
tf7tbtn23#
你可以直接推送远程引用。显然这没有考虑到fork中的变化,但它仍然回答了这个问题。如果变化是有限的,那么你可以很容易地使用本地分支合并/重定基它们。
pqwbnv8z4#
我已经把Max给出的Bash脚本翻译成了Batch脚本,以防你在Windows上需要它。唯一的问题是,要在Windows上运行它,你需要在你喜欢的终端应用程序中提供grep和sed工具(我个人使用Scoop和Windows终端)。