Skip to content

Instantly share code, notes, and snippets.

@cj
Created April 8, 2011 02:54
Show Gist options
  • Save cj/909201 to your computer and use it in GitHub Desktop.
Save cj/909201 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# merge [branchname]
#
# Tries to merge local branch "branchname" into master,
# regardless of what branch you are currently in.
# It will stop you if you are behind & need to pull 1st
#
remote="origin"
branch=$1
current_branch=$(git branch 2>/dev/null|grep -e ^* | tr -d \*//)
if [ -z $branch ]; then
echo "Usage: $0 [branchname]"
exit 1
fi
# If there is a remote version of this branch, rebase onto current_branch first
# If there's a remote (public) branch, we do not want to be rewriting histories
tracking=$(git branch -vv | egrep "^\*" | awk '{ print $4 '})
echo "tracking=$tracking"
if [[ ! "$tracking" =~ "$remote" ]]; then
echo "* Local-only branch, rebasing $branch onto $current_branch first..."
git checkout $branch
git rebase $current_branch || exit 1
else
echo "* This branch exists remotely, not rebasing"
fi
echo "* Merge $branch into $current_branch"
git checkout $current_branch
git merge $branch || exit 1
# Idea: delete local and/or remote branch (?)
# echo "* Deleting merged local branch..."
# git branch -d $branch || exit 1
echo "* Done"
exit 0
@jamiew
Copy link

jamiew commented Jun 30, 2011

Thanks cj, I finally merged this into git-friendly mainline

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