How to find the current git branch in detached HEAD state

前端 未结 6 1791
遥遥无期
遥遥无期 2020-12-04 12:58

I can find the current git branch name by doing either of these:

git branch | awk \'/^\\*/ { print $2 }\'
git describe --contains --all HEAD
<
6条回答
  •  伪装坚强ぢ
    2020-12-04 13:40

    Here's git nthlastcheckout, it gets the exact string you used for your nth last checkout from the reflog:

    git config --global alias.nthlastcheckout '!nthlastcheckout'"() {
            git reflog |
            awk '\$3==\"checkout:\" {++n}
                 n=='\${1-1}' {print \$NF; exit}
                 END {exit n!='\${1-1}'}'
    }; nthlastcheckout \"\$@\""
    

    Examples:

    $ git nthlastcheckout
    master
    $ git nthlastcheckout 2
    v1.3.0^2
    

提交回复
热议问题