Search code examples
gitgit-rebase

How to say HEAD~20 if exists, else first commit


I have the alias:

git rebase --interactive --autostash --autosquash HEAD~20

This works great except when I'm working on a new repository with less than 20 commits, in which case I get the message:

fatal: Needed a single revision
invalid upstream 'HEAD~20'

How can I say: HEAD~20 or else the earliest commit?


Solution

  • I would probably do (in bash) $( git log -n 20 --pretty="%h" --first-parent | tail -n 1 ). So, for example....

    git checkout $( git log -n 20 --pretty="%h" --first-parent | tail -n 1 )
    

    Adjust to your recipe.