Git shortcut to rebase onto another branch

Updated . Posted . Visible to the public.

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.

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

Michael Leimstädtner
Last edit
Michael Leimstädtner
Attachments
License
Source code in this card is licensed under the MIT License.
Posted by Michael Leimstädtner to makandra dev (2021-10-06 11:21)