Last Updated: May 15, 2019
·
2.524K
· phlipper

A Simple Shell Function to Update Git Branches

A client of mine was having a hard time remembering the project workflow to keep topic branches updated, so I put together this simple shell function as a workflow reference:

function git-sync-master() {
  git checkout master && 
    git fetch origin &&
    git pull origin master &&
    git checkout $1 &&
    git rebase master
}

As written, that could be added to a ~/.bash_profile or similar location allowing you to run something like:

$ cd myproject
$ git-sync-master mybranch

Taking this one step further, you can "integrate" this command in to git with the following minor modifications:

$ cat > ~/bin/git-sync-master
git checkout master &&
  git fetch origin &&
  git pull origin master &&
  git checkout $1 &&
  git rebase master
^D

$ chmod +x ~/bin/git-sync-master
$ cd myproject
$ git sync-master mybranch