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 online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
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)