Skip to content

Instantly share code, notes, and snippets.

@innocarpe
Last active September 6, 2019 09:24
Show Gist options
  • Save innocarpe/283bbd09e10a0a99434e7b36bc49175f to your computer and use it in GitHub Desktop.
Save innocarpe/283bbd09e10a0a99434e7b36bc49175f to your computer and use it in GitHub Desktop.
Rebase latest remote base branch when you working on a feature branch
#!/bin/bash
# Rebase latest remote base branch when you are working on a feature branch
# + delete merged remote branches
#
# `git rbb` is a abbreviation of `git ReBase Base branch` :-)
#
# This will...
# 1. Commit current changes temporarily if there are uncommitted changes
# 2. Pull base branch with '--prune' option without checkout
# 3. Rebase develop
# 4. Reset temporary commit to restore
#
# Instruction
# 1. Move this file to ~/.gitsh/
# 2. Add directory path($YOUR_HOME_PATH/.gitsh) to environment variable file
# (like ~/.bash_profile or ~/.zshrc)
# (This would be like 'export PATH=${PATH}:/Users/YOUR_HOME_PATH/.gitsh'
# 3. chmod 0755 git-rbb
# 4. Use the command 'git rbb BASE_BRANCH'
UNCOMMITTED_CHANGES=$(git diff HEAD --name-only | wc -l)
CURRENT_BRANCH=$(git branch --list | awk '{if ($1 == "*") print $2}')
BASE_BRANCH=$1
if [ $UNCOMMITTED_CHANGES -gt 0 ];
then
git commit -am "__WIP__"
fi
git fetch origin $BASE_BRANCH:$BASE_BRANCH
git rebase $BASE_BRANCH
if [ $UNCOMMITTED_CHANGES -gt 0 ];
then
git reset HEAD~1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment