How to tell git branch name from commit hash?

前端 未结 4 1506
旧时难觅i
旧时难觅i 2021-02-19 01:52

I have a bash script which accepts a string of either a branch name (e.g., \"master\", or \"feature/foo\") or a commit hash (e.g. \"1234abcd\").

I have the repository c

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-19 02:07

    Imo you can't check this reliably, since a hash is also a valid branch name. Try:

    git checkout -b 0c8158f47d7dda89226d4e816fee1fb9ac6c1204
    

    This means there can be a situation where a branch with that name exists but also a commit.

    Since you can pass a branch name or a commit to most of the git commands, you don't need to differentiate between them.

提交回复
热议问题