Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:31:06+00:00 2026-06-13T17:31:06+00:00

I’ve cloned some git repository, and did git branch abcd, whcih switched me to

  • 0

I’ve cloned some git repository, and did “git branch abcd”, whcih switched me to a branch derived from origin/abcd. abcd is not the default origin branch.
Then, I have created a feature branch “my_feature” out of abcd.
I want to do “git merge origin/abcd” into my_feature in a script that would be applicable regardless of the name of the origin branch used (or at least applicable to easy cases when there are no complex branch structures described in some other answers about git).

How do I find what is the “closest”/”parent” origin branch off which the current branch was created?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:31:07+00:00Added an answer on June 13, 2026 at 5:31 pm

    This is hard to do well. In git, a branch is just an auto-advancing pointer to a commit, and a commit can have any number of branch names on it. Consider this case:

    your master:      y1---y2---y3
                     /
    master: a---b---c---d---e
                     \
    feature:          f1---f2---f3---f4
    

    You checked out branch “master” at c, and committed y1, y2, and y3. Your history thus looks like a b c y1 y2 y3. Meanwhile master has advanced to d and e, but someone created a feature branch and committed f1 through f4 based on c. Git has no way to determine that your branch came from master instead of feature, so at best you will have a choice of branches to merge.

    Were you to do this automatically you would have to apply a heuristic about picking the shortest branch, or the longest branch, or the one with the most/least commits, or something else like that. Naturally, since there are so many options, it’s not really a good choice for a git built-in function. However, using git’s “plumbing” functions, you can write your own:

    #!/bin/bash
    
    # Tries to determine a good merge base from among all local branches.
    # Here used, a "good" merge base is the one sharing the most recent commit
    # on this branch. This function will exit 1 if no branch is found,
    # or exit 2 in a tie.
    #
    # Untested - use at your own risk.
    
    MAX_SEARCH=20   # only search the last 20 commits on this branch
    FOUND=0
    LAST_BRANCH=
    
    # iterate through the commits, most recent first
    for COMMIT in $(git rev-list --max-count=$MAX_SEARCH HEAD); do
      # check every local branch
      for BRANCH in $(git for-each-ref --format="%(refname)" refs/heads); do
        # look for the commit in that branch's history
        if (git rev-list $BRANCH | fgrep -q COMMIT); then
          echo $BRANCH
          FOUND=$((FOUND + 1))
          LAST_BRANCH="$BRANCH"
        fi
      done
      if [ $FOUND -gt 1 ]; then
        # more than one choice; exit
        exit 2
      elif [ $FOUND -eq 1 ]; then
        git merge $LAST_BRANCH
        exit 0
      fi
    done
    exit 1   # could not find a parent
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.