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 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
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'
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…