代码之家  ›  专栏  ›  技术社区  ›  Tom Hale

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

  •  3
  • Tom Hale  · 技术社区  · 6 年前

    我的化名是:

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

    这非常有效,除非我正在处理一个提交少于20次的新存储库,在这种情况下,我会得到以下消息:

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

    HEAD~20 or else the earliest commit

    2 回复  |  直到 6 年前
        1
  •  2
  •   eftshift0    6 年前

    我可能会这样做(在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 )
    

    调整你的食谱。

        2
  •  0
  •   Tom Hale    6 年前

    eftshift0's answer

    # 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个父级,则返回提交本身(或头)。

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

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