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



我有别名:

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 )

根据您的食谱进行调整。

根据 eftshift0 的答案,我想出了以下别名:

# Print the $1-th first-parent of commit $2
# 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 #"

它需要两个可选参数:

  • $1- 回去多少父母。默认为1或直系父级
  • $2- 要操作的提交,默认为HEAD

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

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

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

最新更新