3

In GitKraken, we report from one branch to another commits with the same reference.

Problem : It's difficult to detect in which branch the commit is located if we do a search with CTRL+F.

Is there a way to know the name of the branch from a commit?


We have 7 commits with the same reference enter image description here

If I select one of them, I'm positioned on a commit. From this view, it is impossible for me to know the name of the branch. enter image description here

kowsky
  • 12,647
  • 2
  • 28
  • 41
Harvey
  • 120
  • 7
  • 4
    Commits are not on branches. This is a misunderstanding of the branch metaphor in git. In *some* contexts, yes, you will have a branch contaning a given commit. But you could also have multiple branches equally relevant, or none. You're searching for *the* name of *the* branch but this is not a given. – Romain Valeri May 15 '19 at 12:24
  • Are all your commit message only a reference such as **HMER-B6WLDS**? – padawin May 15 '19 at 12:44
  • @RomainValeri Thank you for the clarification. – Harvey May 15 '19 at 12:44
  • @padawin Yes and they do not necessarily have the same content, the change depends on the version of the application. – Harvey May 15 '19 at 12:46

5 Answers5

8

Try this :

 git branch --contains <commit>

And read this: How to list branches that contain a given commit?

Mohammad Ansari
  • 1,076
  • 1
  • 11
  • 22
  • Thank you, this is a good workaround but I would have to make this command on each commit to know the related branch. Is it possible to know this directly from GitKraken? – Harvey May 15 '19 at 12:49
  • This isn't a "workaround", it's how Git works. Like RomainValeri says above, Git commits aren't on or in branches. – ChrisGPT was on strike May 15 '19 at 12:51
  • It is a workaround if you're not using the command line, as OP seemingly does. – kowsky May 15 '19 at 12:52
  • We are not using the command line method, some of us have no knowledge of the git and use everything in GUI with GitKraken. – Harvey May 15 '19 at 13:02
2

For a commit $commit, to find the branches that exactly point at it:

git for-each-ref refs/heads --format="%(refname:lstrip=2)" --points-at $commit

And to find the branches from which it is reachable:

git for-each-ref refs/heads --format="%(refname:lstrip=2)" --contains $commit
ElpieKay
  • 27,194
  • 6
  • 32
  • 53
1

In short: No.

As @RomainValeri said in his comment: a commit does not "belong" to any branch. A branch is nothing more than a pointer to a commit. There may be a hundred branches from which a commit is reachable, or there may be none.

The git command git branch --contains <commit>, suggested by @MohammadAnsari, will show you a list of all branches from which the commit is reachable.

As to GitKraken: There is no immediate solution for your problem. If the commit is further down the graph, it's hard to tell to which strain of ancestor commits it leads. Also, there is no GitKraken command that does what git branch --contains <commit> does (or none I know of).

Maybe you should overthink your wokflow of having commits with the same name, or rethink your branching strategy. Do you have several long running branches? If all branches would converge against a single master, the question would be trivial.

kowsky
  • 12,647
  • 2
  • 28
  • 41
  • As you point out, it is difficult to get the name of the branch if it is far enough from the end (like my screenshot). We have several branches "Releases" and only one master branch. All our developments are on the master branch and some commits are cherrypick (or adapted) in other branch (which are different versions to maintain). The best solution for us was to have additional information on the commit panel on the right "Related branch: ..." or something like that. I hope the explanations are clear. – Harvey May 15 '19 at 13:00
1

You can find it if you know the commit id. Use "ctrl+f" and it will open "Search Commits" dialog where you need to enter commit id. After you press enter, the tree graph will navigate to that commit and you can see visually on which branch it is placed.

Ryukote
  • 767
  • 1
  • 10
  • 26
-1

I am definitely the less knowledgeable in git from all of you here, but what about:

  1. copying the given commit hash
  2. running git show [commit-hash] in a git bash
  3. the beginning of the (long) output should look like

enter image description here

The info displayed at origin/version-210627 seems to show the branch (version-210627 in my case).

Veverke
  • 9,208
  • 4
  • 51
  • 95