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 Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)