Last active
January 9, 2018 14:08
-
-
Save sebald/0c877c146ba3cdd541c22a5c620b6399 to your computer and use it in GitHub Desktop.
Update and merge branch with latest updates from "development".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Drop this in your
.aliases
! 😃Some example usage:
update_branch
will update the branch you're currently work onupdate_branch -
same as (1)update_branch foo
will update a branch called foo with updates from a branch called developmentupdate_branch foo master
will update a branch called foo with updates from a branch called master