Search code examples
gitgit-branchgit-rebase

find branch name during git rebase


What?

Is there a way to find the name of the branch-being-rebased during an interactive rebase, that is better than parsing .git/rebase-merge/head-name?

Details

Normally I use git rev-parse --abbrev-ref HEAD to get the branch name. But during a rebase the branch is in a detached head state and rev-parse returns HEAD.

So now I'm parsing the .git/rebase-merge/head-name file if it exists to pull out the branch name. Is there a method (porcelain or otherwise) to get this data?

Usage:

git checkout "the_branch_name_I_want"
git rebase -i "some_other_branch_sha_etc"
# mark commit for edit ...
git magic-command # I'd like to get back "the_branch_name_I_want"
git rebase --continue

Why?

Why do I want to do this?

I store metadata about the branch and fetch it in my commit-msg hook to append to my commit message. I want this to work when rewording or editing my commit messages during interactive rebase.

Code:

branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ "$?" -ne 0 ]; then
    echo "not in a git repo!"
    exit 2
fi
if [ "$branch" = "HEAD" ]; then
    # check if we are in a rebase
    head_name_file="$(git rev-parse --git-dir)/rebase-merge/head-name"
    if [ -f "${head_name_file}" ]; then
        branch=$(cut -f3- -d/ $head_name_file)
    else
        # ignore DETACHED HEAD state.
        exit 1
    fi
fi
## do something with branch

Solution

  • Is there a way to find the name of the branch-being-rebased during an interactive rebase, that is better than parsing .git/rebase-merge/head-name?

    Note that with Git 2.18 (Q2 2018), "git branch --list" during an interrupted "rebase -i" now lets users distinguish the case where a detached HEAD is being rebased, and a normal branch is being rebased.

    So you still need to parse, but the output of the command itself is now useful.

    See commit 1f537be (03 Apr 2018) by Eric Sunshine (sunshineco).
    See commit a236f90 (03 Apr 2018) by Kaartic Sivaraam (sivaraam).
    (Merged by Junio C Hamano -- gitster -- in commit 4cbaa6b, 25 Apr 2018)

    branch --list: print useful info whilst interactive rebasing a detached HEAD

    When rebasing interactively (rebase -i), "git branch --list" prints a line indicating the current branch being rebased.
    This works well when the interactive rebase is initiated when a local branch is checked out.

    This doesn't play well when the rebase is initiated on a detached HEAD.
    When "git branch --list" tries to print information related to the interactive rebase in this case it tries to print the name of a branch using an uninitialized variable and thus tries to print a "null pointer string".
    As a consequence, it does not provide useful information while also inducing undefined behavior.

    So, print the point from which the rebase was started when interactive rebasing a detached HEAD.

    See the test.


    For some things, the git log is a better reference than the documentation. (I wonder why they don’t set an environment variable for the --exec command to use.)

    True, git log can indeed provide valuable insights, especially for understanding the evolution of features and bug fixes in Git. By examining commit messages and the detailed explanations often included, you can gain a more in-depth understanding of the context and reasoning behind changes, which might not be as apparent in official documentation. That can be particularly useful for advanced users or those developing tools that integrate closely with Git.

    Suggesting an environment variable for the --exec command speaks to a broader need for making information readily available in various Git operations, such as during a rebase.
    Currently, Git does not set a specific environment variable that exposes the current branch name during a rebase for use with the --exec command in interactive rebases.