The project has the main branches of develop and stable . In some cases, branches of each individual feature were created from develop , and in some from stable , but now it was necessary to find out about one of the old features, from which branch it was created. How can I do that?

  • In general, no way. You can see git log --graph --all or in some graphical git-viewer and eyes to understand. Usually it is visible in the graph, but it is difficult to formalize the conditions - Alexey
  • @Alexey Ten tried it, but it would break a huge leg of a leg there, and there is no easier way to just get the name of the branch. - Pavel
  • A branch is just a pointer to the top commit. You can search for the common parent of the branch and develop / master and see who is “older” (see git merge-base) - Alexey Ten
  • @Pavel, a branch in the git program is not “a certain sequence of commits,” but just a (floating) pointer to a single commit. information about which commit at which point in time this pointer pointed in the past is, alas, not stored (yes, there is a local reflog, maybe it will help you with something). - aleksandr barakin
  • what git show says --summary 'git merge-base branch feature stable' and git show --summary 'git merge-base branch feature develop' - just change 'to apostrophes - Total Pusher

1 answer 1

As such, there is no built-in solution to this problem in git, but by combining git with bash you can find several potential solutions to the problem:

  1. git branch --contains develop | grep "brach_name" git branch --contains develop | grep "brach_name" - this command will first find all the branches created from develop, then use grep to filter it in search of the desired branch. If the specified branch is not among the branches created from develop, the output will be empty, otherwise the branch name itself will be displayed. At the same time, it is not necessary to switch to the desired branch.
  2. The option is more complicated:

     git show-branch -a \ | grep '\*' \ | grep -v `git rev-parse --abbrev-ref HEAD` \ | head -n1 \ | sed 's/.*\[\(.*\)\].*/\1/' \ | sed 's/[\^~].*//' 

    We display a list of all branches in general, then, using console magic, we are looking for the parent of the branch. Demands transition to a branch for which we look for the parent.

I’ll just say that the solutions were checked exclusively on mac OS, but given the similarity of bash with Unix / Linux, it should also work without changes. Under Windows you can easily make similar commands. More information and solutions can be found in this question on SO.