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.
-
when on
some-working-branch
git checkout master
git pull upstream master
git push origin master
git checkout some-working-branch
(it will remember your working branch and automatically check back into it)git pull origin master
git push origin some-working-branch
-
when on
master
branchgit pull upstream master
git push origin master
-
when on
some-working-branch
git pull origin master
git push origin some-working-branch
-
when on
master
branchgit 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}"
- automagically stash uncommitted changes (if any) beforehand and then pop stash once sync is done