如何说 HEAD~20 如果存在,否则先提交

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

我有别名:

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

这很好用,除非我正在处理提交少于 20 次的新存储库,在这种情况下我收到消息:

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

我怎么说:HEAD~20 or else the earliest commit

我可能会这样做(在 bash)$( git log -n 20 --pretty="%h" --first-parent | tail -n 1 )。所以,例如....

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

根据您的食谱进行调整。

基于,我想到了以下别名:

# Print the -th first-parent of commit 
# or the earliest existing commit if the beginning of history is reached
parent = !"[ \"${1:-1}\" -eq \"${1:-1}\" ] && c=$(git rev-parse \"${2:-@}\") && git log -n\"$(expr \"${1:-1}\" + 1)\" --first-parent --pretty=\"%H\" --reverse \"$c\" | head -n1 #"

它有两个可选参数:

  • </code> - 要返回多少 parent。默认为 <code>1,或直接 parent
  • </code> - 要操作的 commit-ish,默认为 <code>HEAD

如果请求第 0 个 parent,则返回提交本身(或 HEAD)。

这是在我的 rebase --interactive 别名中使用的:

rbi = !"git rebase --interactive --autostash --autosquash --root \"$(git rev-parse \"${1:-$(git parent 50)}\")\" #"