How to know what branch I am on git when checkout origin/

前端 未结 3 1599
醉话见心
醉话见心 2021-01-27 19:33

We have a script that actually does git fetch; git checkout origin/ to deploy a certain feature. The reason why we do this is that we wan\'t to avoid

相关标签:
3条回答
  • 2021-01-27 20:08

    git symbolic-ref --short HEAD should tell you what branch you are on, or print an error if you are not on a branch.

    0 讨论(0)
  • 2021-01-27 20:12

    Here's git nthlastcheckout, it gets the exact string you used (or everything after its last space) 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
    
    0 讨论(0)
  • 2021-01-27 20:19

    The best solution for us, pointed by @SébastienDawans, was git show -s --pretty=%d HEAD. The output is like remotes/origin/<branch name>, so maybe a cleaning is required, but for our needs it's just fine.

    0 讨论(0)
提交回复
热议问题