Skip to content

Instantly share code, notes, and snippets.

@sebald
Last active January 9, 2018 14:08
Show Gist options
  • Save sebald/0c877c146ba3cdd541c22a5c620b6399 to your computer and use it in GitHub Desktop.
Save sebald/0c877c146ba3cdd541c22a5c620b6399 to your computer and use it in GitHub Desktop.
Update and merge branch with latest updates from "development".
function get_current_branch () {
git branch 2>/dev/null | grep '^*' | colrm 1 2
}
function branch_exists () {
git rev-parse --verify --quiet $1
}
function is_git_clean () {
git status --porcelain
}
function update_branch () {
bold=$(tput bold)
italic=$(tput sitm)
reset=$(tput sgr0)
if [ -z $1 ]; then
BRANCH=$(get_current_branch)
else
BRANCH=$1
fi
echo ${BRANCH}
if [ -z $2 ]; then
MAIN="development"
else
MAIN=$2
fi
if [[ !($(branch_exists ${BRANCH})) ]]; then
echo -e "\n\xf0\x9f\x92\x80 Branch ${bold}${BRANCH}${reset} does not exist! Please create it first."
return
fi
echo -e "\n\xf0\x9f\x8c\xb1 Start updating branch ${bold}${BRANCH}${reset}"
if [[ $(get_current_branch) != MAIN ]]; then
echo -e "\n\xf0\x9f\x94\x80 Switching to ${bold}${MAIN}${reset}"
git checkout ${MAIN}
fi
echo -e "\n\xf0\x9f\x94\xbd Getting updates"
git pull
echo -e "\n\xf0\x9f\x94\x80 Switching to ${bold}${BRANCH}${reset}"
git checkout ${BRANCH}
echo -e "\n\xf0\x9f\x91\xbe Fast-forward merging ${bold}${MAIN}${reset}${bold}${BRANCH}${reset}"
git merge ${MAIN}
# test if no conflicts
if [[ $(git diff --name-only --diff-filter=U) ]]; then
echo -e "\n\xf0\x9f\x92\xa5 Repo has conflicts. Please solve them and then do a ${italic}git push${reset}."
# test if there are changes
elif [[ -z $(git diff --name-only) ]]; then
echo -e "\n\xf0\x9f\x92\xab Repo is clean, no need to push updates!"
else
echo -e "\n\xf0\x9f\x94\xbc Pushing updates!"
git push
echo -e "\n\xf0\x9f\x8f\x81 All done! Happy coding!"
fi
}
@sebald
Copy link
Author

sebald commented Jan 8, 2018

Drop this in your .aliases! 😃

Some example usage:

  1. update_branch will update the branch you're currently work on
  2. update_branch - same as (1)
  3. update_branch foo will update a branch called foo with updates from a branch called development
  4. update_branch foo master will update a branch called foo with updates from a branch called master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment