How to determine branch parent for rebase

git

Solution

This should do it for you:

git log --oneline \
  | cut -f 1 -d' ' \
  | (while read commit ; do
       other_branches="$(git branch --contains $commit | egrep -v '^\* ')"
       if [ -n "${other_branches}" ] ; then
         echo -e "${commit} is in:\n${other_branches}"
         break
       fi
     done)

This will go through this branch's commit log and for each commit, check which branches contain (are descendants of) that commit. As soon as it finds a commit that has a descendant that is not the current branch, it prints the commit hash, the other branches that contain that commit, and breaks out of the loop.

Problem

Yes this seems very similar to "Find the parent branch of a git branch" but I didn't find my answer there. I think I want to know how to find my current branches parent, but I might be asking the wrong question. So I'll provide my scenario. Our organization has several long running release branches that do not get renamed (but eventually are deprecated after the release) Occasionally I create a feature branch from one of the release branches to work on a problem. It's a local feature branch. I get called away to work on some other item, some other release, etc. I come back to this feature and now I want to rebase it to the latest commit on which it was originally branched from. But enough time has passed that I cannot remember if this was branched from release x or release y So how do I do this? In my mind, I thought I would just figure out what release branch I created my feature from, and then rebase to the last commit on that branch. I know there are other organizational techniques that can be done to avoid this problem, such as always including the name of the base branch in my feature branch. But that ship has sailed. Is there a `git` solution to the challenge I am having?

Original source

Related problems