Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine branch parent for rebase

Tags:

git

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.

  1. Our organization has several long running release branches that do not get renamed (but eventually are deprecated after the release)

  2. Occasionally I create a feature branch from one of the release branches to work on a problem. It's a local feature branch.

  3. I get called away to work on some other item, some other release, etc.

  4. 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?

like image 652
John Rocha Avatar asked Oct 26 '16 15:10

John Rocha


People also ask

How can I see the parent branch in git?

How do I find the origin of a branch? To see local branches, run this command: git branch. To see remote branches, run this command: git branch -r. To see all local and remote branches, run this command: git branch -a.

How do you know which branch is created from which branch?

Use Status Command Your branch is up to date with 'origin/master'. The first line shows which branch are you on. The second shows your upstream branch if there are any set.


1 Answers

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.

like image 56
onlynone Avatar answered Oct 01 '22 19:10

onlynone