Suppose you are working on several shared git repositories.
If you work on a repository everyday, you normally will have your repository up-to-date. However if you only work on a repository once a week or less, next time you visit your local repository, all your branches will probably be behind your origin/upstream repository.
Normally you will update your local repo before starting to work, by doing the following:
$ git fetch $ git checkout master $ git pull $ git checkout develop $ git pull
This is a bit cumbersome, even with a GUI you normally follow these steps by clicking and using menu commands. If you have more local branches tracking remote feature branches, the procedure will be even more painful.
After a while, you will dread revisiting old projects because of this, and will think it’s better to scrap your local repo and re-clone it again in order to avoid manually updating all your local branches.
But fear not, with a bit of scripting, we can automate this tedious task, and using a nice git fetch trick we will do it without checking out other brances, so the procedure will be lightning fast.
The gist of the script is shown here:
unset ambtrackings declare -A ambtrackings while read branch; do upstream=$(git rev-parse --abbrev-ref $branch@{upstream} 2>/dev/null) if [[ $? == 0 ]]; then remote=${upstream%/*} remotebranch=${upstream#*/} echo $branch tracks $upstream: $remotebranch on remote $remote ambtrackings[$remote]="${ambtrackings[$remote]} $remotebranch:$branch" else echo $branch has no upstream configured fi done < <(git for-each-ref --format='%(refname:short)' refs/heads/*) git checkout --detach for i in "${!ambtrackings[@]}"; do echo "Updating local branches tracking remote: $i" echo "> [remotebranch]:[localbranch]: ${ambtrackings[$i]}" git fetch $i ${ambtrackings[$i]} done git checkout -
You can put this somewhere in your $PATH, or configure it as a custom action if you’re using SourceTree, and you can go directly from here:
to here:
by executing the script:
$ updatelocalbranches.sh develop tracks origin/develop: develop on remote origin hotfix/PJYS-8-... tracks origin/hotfix/PJYS-8-...: hotfix/PJYS-8-... on remote origin master tracks origin/master: master on remote origin Note: checking out 'HEAD'. You are in 'detached HEAD' state. [...] HEAD is now at 2401da1... Merge pull request #5 in PRUEB/pruebas-de-stash-y-jira from feature/PJYS-8-automatizar-las-versiones-del-codigo to develop Updating local branches tracking remote: origin > [remotebranch]:[localbranch]: develop:develop hotfix/PJYS-8-...:hotfix/PJYS-8-... master:master From http://codman2-d.esios.ree.es:7990/bitbucket/scm/prueb/pruebas-de-stash-y-jira 2401da1..e601e0b develop -> develop 8b99600..6534e9e hotfix/PJYS-8-automatizar-las-versiones-del-codigo -> hotfix/PJYS-8-automatizar-las-versiones-del-codigo Your branch is up-to-date with 'origin/develop'. Previous HEAD position was 2401da1... Merge pull request #5 in PRUEB/pruebas-de-stash-y-jira from feature/PJYS-8-... to develop Switched to branch 'develop'
The script is a bit verbose right now in order to see what’s going on. But its output may be tweaked/removed at will.
It also supports more than one remote. As long as your trackings are correctly set, it will update them against the right remote.
Using this is a breeze and a joy. Having your branches behind your origin/upstream will no longer be an issue.
Enjoy!