Skip to content

Instantly share code, notes, and snippets.

@mtking2
Last active August 30, 2019 21:15
Show Gist options
  • Save mtking2/3a150134794edc57dda797850823a69a to your computer and use it in GitHub Desktop.
Save mtking2/3a150134794edc57dda797850823a69a to your computer and use it in GitHub Desktop.
Git Upstream/Origin Sync

Git Upstream/Origin Sync

This script will sync your origin with upstream master, whether you're currently on master or checked-out on a working branch.

If you don't have an upstream remote then it will just pull and push to origin accordingly.

Just throw this in a file somewhere, chmod 755 that bad boy, and then export PATH="path/to/script:$PATH" in your favorite ~/.rc file to run from anywhere.

Breakdown of commands for various cases

With upstream remote

  • when on some-working-branch

    1. git checkout master
    2. git pull upstream master
    3. git push origin master
    4. git checkout some-working-branch (it will remember your working branch and automatically check back into it)
    5. git pull origin master
    6. git push origin some-working-branch
  • when on master branch

    1. git pull upstream master
    2. git push origin master

Without upstream remote

  • when on some-working-branch

    1. git pull origin master
    2. git push origin some-working-branch
  • when on master branch

    1. git push origin master (nothing really to do here but go ahead and pull origin master because why not)

#!/bin/bash
blue="\033[0;34m"
green="\033[0;32m"
yellow='\033[0;33m'
white="\033[0m"

# get currently checked-out  branch
parse_git_branch() {
  local branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
  echo -e $branch
}

# pull upstream master and push origin master
plum_and_pom() {
  echo -e "${blue}Pulling in upstream ${yellow}master$white"
  git pull upstream master
  echo -e "${blue}Pushing updates to origin ${yellow}master$white"
  git push origin master
}

# pull origin master and push origin working branch
plom_and_powb() {
  echo -e "${blue}Pulling origin ${yellow}master$blue into $yellow$1$white"
  git pull origin master
  echo -e "${blue}Pushing updates to origin $yellow$1$white"
  git push origin $1
}

working_branch=$(parse_git_branch)
remotes="$(git remote show)"

if [[ $remotes =~ upstream ]]; then
  # does have upstream remote
  if [ $working_branch == master ]; then
    plum_and_pom
  else
    echo -e "${blue}Checking out ${yellow}master${blue} branch${white}"
    git checkout master
    plum_and_pom
    echo -e "${blue}Checking back into ${yellow}$working_branch${blue} branch${white}"
    git checkout $working_branch
    plom_and_powb $working_branch
  fi
else
  # does not have upstream remote
  if [ $working_branch == master ]; then
    echo -e "${blue}Pulling origin ${yellow}master$blue for kick and giggles${white}"
    git pull origin master
  else
    plom_and_powb $working_branch
  fi
fi

echo -e "done ${green}${white}"

TODO's

  • automagically stash uncommitted changes (if any) beforehand and then pop stash once sync is done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment