If by any chance you use zsh, oh-my-zsh, and its git plugin, I can offer you this function:
function stash_and_rebase() {
local current_branch="$(git_current_branch)"
if (( ${#current_branch} == 0 )); then
echo "There is no current branch. This script must be executed in git repo."
return 1
fi
if (( $# == 0 )); then
local target_branch="$(git_main_branch)"
else
local target_branch="$1"
fi
git stash push
git fetch --all
git rebase origin/${target_branch}
echo "Don't forget to execute 'git stash pop' after rebase!"
}
Let's say you want to rebase onto `main`. Checkout your feature branch and run `stash_and_rebase`. If you want to rebase onto non-main branch, run `stash_and_rebase branchX`. If everything went OK, do `git stash pop` in the end.
N.B. This doesn't do squash!