Read more

Git shortcut to rebase onto another branch

Michael Leimstädtner
October 06, 2021Software engineer at makandra GmbH

Inspired by recent "git shortcut" cards I figured it would be nice to have one of these for rebasing a few commits onto another branch. The usual notation is prone to of-by-one errors as you have to either specify the commit before the ones you want to move or count the number of commits.

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

You may add this rebase-onto function to your ~/.bashrc:

function rebase-onto {
  commit=$(git log --oneline | fzf --prompt 'Select the first commit you want to move' | awk '{print $1}')
  branch=$(git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/ | fzf --prompt 'Select the branch to rebase onto' | awk '{print $1}')
  test_command="git rebase -i --onto $branch $commit~"
  read -p "Execute command '$test_command' (Y/n)? " choice
  case "$choice" in
    n|N ) echo "aborted.";;
    * ) eval "$test_command";;
  esac
}

It should reduce the amount of brain cells involved when rebasing onto a branch:

Image

See the first card below for more context on this example.

See also

Posted by Michael Leimstädtner to makandra dev (2021-10-06 13:21)