How to update my fork to have the same branches and tags as the original repository on github?
GitGithubGit Problem Overview
When you fork a repository on github your forked repo contains all branches and tags.
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 ?
i.e. a git magicpull --rebase upstream/* myremote/*
which would fetch all the branches and tags in upstream and make sure the same are present in myremote.
Git Solutions
Solution 1 - Git
This assumes your "upstream" remote is named "origin" and you have your custom fork under your username (i.e. "maxandersen")
When you have your clone run the following one-liner (refresh of https://stackoverflow.com/questions/379081/track-all-remote-git-branches-as-local-branches/6300386#6300386 :
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.
(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 and tags to your remote (replace 'maxandersen' with your remote name):
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=$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
Save it as 'updateallbranchestags.sh' and execute it with:
sh updateallbranches.sh origin maxandersen
And all branches/tags from 'origin' will be made available in remote named 'maxandersen'
Solution 2 - Git
You would still need a local clone, which would:
- fetch and update all the upstream branches into local branches (git fetch upstream, with upstream being a reference to the original repo you have forked)
See "How do I clone all remote branches with Git?" for more on having all remote branches as local branches.
I use this one-liner from the question "Track all remote git branches as local branches".
See also "What is the difference betweenorigin
andupstream
in GitHub"
-
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.
- you have all the local branches tracking all the upstream branches (see previous step).
Solution 3 - Git
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.
git push -f origin refs/remotes/upstream/*:refs/heads/*
Solution 4 - Git
I have translated the Bash script given in the excellent answer by Max into Batch script, just in case you need this on Windows. The only gotcha is that, to run this on Windows, you need to have grep and sed tools available in your preferred terminal application (I personally use Scoop and 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