How to pragmatically check with bash script if a branch in git needs to be rebased? -
i'm working on bash script team enforce regular rebasing of working branches. problem facing @ moment how determine whether branch behind master and/or if needs rebased, rather blindly attempting rebase branch.
here simplified version of have far:
#process each repo in working directory. repo_dir in $(ls -1); # if working branch clean ... # begin update of local master git checkout master git fetch origin git merge remotes/origin/master # end update of local master sync_branch in $(git branch | cut -c 3-); if [ "$sync_branch" != "master" ]; # begin rebase working branch git checkout $sync_branch git rebase master # not push working branch remote. # end rebase working branch fi done fi done
any ideas appreciated. thanks!
to tell if need rebase branch, need find out latest commit , last commit 2 branches share.
to find latest commit on branch:
git show-ref --heads -s <branch name>
then find last commit branches have in common:
git merge-base <branch 1> <branch 2>
now need find out if these 2 commits same commit. if are, don't need rebase. if not, rebase required.
example:
hash1=$(git show-ref --heads -s master) hash2=$(git merge-base master foo/bar) [ "${hash1}" = "${hash2}" ] && echo "ok" || echo "rebase required"
though stated in comment, if attempt rebase branch date. git gracefully handle situation , exit.
Comments
Post a Comment