How to find git branch name from commit hash?

I have a bash script that accepts a branch name string (like "master" or "feature / foo") or commit hashes (like "1234abcd").

I have a repository and I can call git.

What is the best way to determine if a string is a branch name or a commit hash?

#!/bin/bash commit_or_branch="$1" cd /path/to/my_repo git fetch if <is_branch $commit_or_branch> then echo "it a branch" else echo "it a commit" fi 
+6
source share
4 answers

You can use git show-ref :

 git show-ref --head | grep refs 

If it is empty, it is SHA1 (or an invalid object, which is not very good).
If not, this is the name of the branch.


The best method comes from Validate if commit exists "using git merge-base :

The branch name will lead to another line (SHA1)

 C:\Users\vonc\prog\b2d>git merge-base master master de4accfd28c5f25fcc057d56996b83450be5dc60 

SHA1 will produce the same result (or at least start with the same result ):

 C:\Users\vonc\prog\b2d>git merge-base 03949c3d3f88a378c6a08e57daa97059b52813f1 03949c3d3f88a378c6a08e57daa97059b52813f1 03949c3d3f88a378c6a08e57daa97059b52813f1 

foobar will fail:

 C:\Users\vonc\prog\b2d>git merge-base xxx xxx fatal: Not a valid object name xxx 

This means something like:

 if [[ git merge-base $string $string ]]; then if [[ $(git merge-base $string $string) == $string* ]]; then echo "SHA1" else echo "branch" fi else echo "Not a valid object name '$string'" fi 
+2
source

If you want a reliable mechanism to specify relative names (for example, you may be one of SHA1 commits a named branch), you can use git name-rev to resolve it.

Examples:

 $ git config remote.upstream.url https://github.com/RobotLocomotion/drake.git $ git log --oneline -n 5 7530a95 Merge pull request #5743 from soonho-tri/pr-reformat-mathematical_program ebc8f25 Suppresses console output of speed_bump.obj genrule. (#5726) d8b9a0b Merge pull request #5735 from david-german-tri/namespaces 79e10e8 Remove redundant 'symbolic::' prefix from mathematical_program code b68b590 Clean up mathematical_program code by adding using std::* $ git name-rev HEAD HEAD master $ git name-rev 79e10e8 79e10e8 master^2 $ git name-rev HEAD~20 HEAD~20 remotes/origin/issue/5646_return_binding~3 

`` ``

Link: Git Tips

+1
source

Imo you cannot verify this reliably since the hash is also a valid branch name. Try:

 git checkout -b 0c8158f47d7dda89226d4e816fee1fb9ac6c1204 

This means that there may be a situation where there is a branch with this name, as well as a commit.

Since you can pass the branch name or commit to most git commands, you do not need to distinguish between them.

0
source

As mentioned in @VonC and @ hek2mgl, this may not be one or the other test. You can slightly change your script to something like this (borrowed from this SO answer :

 #!/bin/bash commit_or_branch="$1" cd /path/to/my_repo git fetch if git branch | grep $commit_or_branch 2> /dev/null then echo "it a branch" fi if git cat-file -e $commit_or_branch 2> /dev/null then echo "it a commit" fi 

Please note that these are only tests for local branches .. see this post if you are interested in remote branches.

0
source

Source: https://habr.com/ru/post/985373/


All Articles