I want the code snippet (using JGit), to get the branch on which a commit was made.
This code is needed because when a checkout happens from a commit ID, the normal api of getbranch returns the commit id (because it will be detached No_branch state). so i need to find the actual branch from commit ID

I don't think that Git can tell that always. With the help of reflog you may find out for locally created versions (only for a certain period of time until old reflog entries are deleted), but not for versions which you have received from remote. With which native git commands would you get this information?

'git branch --contains <commit>' will report every branch which contains this commit. In the standard push/fetch workflows for all commits you get from remote only "origin/master" is reported. And even for local commits: Imagine you have merged your feature branch back to master. Then this command will report also the master branch and all feature branches created after merging. Git simply doesn't store for a revision on which branch it was created.

After these warnings: a rough implementation of "git branch --conttains <commit>" could look like this: