Getting the name of the parent origin branch in git?

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?

  • Find commits that modify file names matching a pattern in a GIT repository
  • Eclipse + Egit - adding a new Java project to a remote repository
  • What happens if i push a tag for a commit that hasn't been pushed
  • On Github, how do you distinguish the good from the bad?
  • Cherry-picked commits have different hashes
  • Where does git config --global get written to?
  • What is the public key used for when working with Git?
  • What do the arrows mean in the IDEA git log screen?
  • Git merge/compare tool setup
  • How can I drop a branch from a mulitple branch merger in git?
  • How to download a folder from github?
  • Resolving same conflicts again and again when doing rebasing more times
  • 2 Solutions collect form web for “Getting the name of the parent origin branch in git?”

    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
    

    With a DVCS, there is no such thing as a “parent branch”.

    A branch is:

    • a name
    • a starting commit, and that commit can:
      • part of multiple branches, both from the local repo and the upstream repo like ‘origin‘.
      • change after a rebase of your branch

    See for more:

    • “How to tell which branch a github commit was for?”
    • “Git: How to list branches that contain a given commit?”

    The only way for your script to work is to record the name of the origin/branch, preferably as a git notes (since it doesn’t change the history and the SHA1’s, but add a metadata).

    Git Baby is a git and github fan, let's start git clone.