Finding what branch a Git commit came from

后端 未结 14 2380
失恋的感觉
失恋的感觉 2020-11-22 10:43

Is there a way to find out what branch a commit comes from given its SHA-1 hash value?

Bonus points if you can tell me how to accomplish this using Ruby Grit.

相关标签:
14条回答
  • 2020-11-22 11:16

    As an experiment, I made a post-commit hook that stores information about the currently checked out branch in the commit metadata. I also slightly modified gitk to show that information.

    You can check it out here: https://github.com/pajp/branch-info-commits

    0 讨论(0)
  • 2020-11-22 11:18

    For example, to find that c0118fa commit came from redesign_interactions:

    * ccfd449 (HEAD -> develop) Require to return undef if no digits found
    *   93dd5ff Merge pull request #4 from KES777/clean_api
    |\
    | * 39d82d1 Fix tc0118faests for debugging debugger internals
    | * ed67179 Move &push_frame out of core
    | * 2fd84b5 Do not lose info about call point
    | * 3ab09a2 Improve debugger output: Show info about emitted events
    | *   a435005 Merge branch 'redesign_interactions' into clean_api
    | |\
    | | * a06cc29 Code comments
    | | * d5d6266 Remove copy/paste code
    | | * c0118fa Allow command to choose how continue interaction
    | | * 19cb534 Emit &interact event
    

    You should run:

    git log c0118fa..HEAD --ancestry-path --merges
    

    And scroll down to find last merge commit. Which is:

    commit a435005445a6752dfe788b8d994e155b3cd9778f
    Merge: 0953cac a06cc29
    Author: Eugen Konkov
    Date:   Sat Oct 1 00:54:18 2016 +0300
    
        Merge branch 'redesign_interactions' into clean_api
    

    Update

    Or just one command:

    git log c0118fa..HEAD --ancestry-path --merges --oneline --color | tail -n 1
    
    0 讨论(0)
  • 2020-11-22 11:18

    khichar.anil covered most of this in his answer.

    I am just adding the flag that will remove the tags from the revision names list. This gives us:

    git name-rev --name-only --exclude=tags/* $SHA
    
    0 讨论(0)
  • 2020-11-22 11:21

    If the OP is trying to determine the history that was traversed by a branch when a particular commit was created ("find out what branch a commit comes from given its SHA-1 hash value"), then without the reflog there aren't any records in the Git object database that shows what named branch was bound to what commit history.

    (I posted this as an answer in reply to a comment.)

    Hopefully this script illustrates my point:

    rm -rf /tmp/r1 /tmp/r2; mkdir /tmp/r1; cd /tmp/r1
    git init; git config user.name n; git config user.email e@x.io
    git commit -m"empty" --allow-empty; git branch -m b1; git branch b2
    git checkout b1; touch f1; git add f1; git commit -m"Add f1"
    git checkout b2; touch f2; git add f2; git commit -m"Add f2"
    git merge -m"merge branches" b1; git checkout b1; git merge b2
    git clone /tmp/r1 /tmp/r2; cd /tmp/r2; git fetch origin b2:b2
    set -x;
    cd /tmp/r1; git log --oneline --graph --decorate; git reflog b1; git reflog b2;
    cd /tmp/r2; git log --oneline --graph --decorate; git reflog b1; git reflog b2;
    

    The output shows the lack of any way to know whether the commit with 'Add f1' came from branch b1 or b2 from the remote clone /tmp/r2.

    (Last lines of the output here)

    + cd /tmp/r1
    + git log --oneline --graph --decorate
    *   f0c707d (HEAD, b2, b1) merge branches
    |\
    | * 086c9ce Add f1
    * | 80c10e5 Add f2
    |/
    * 18feb84 empty
    + git reflog b1
    f0c707d b1@{0}: merge b2: Fast-forward
    086c9ce b1@{1}: commit: Add f1
    18feb84 b1@{2}: Branch: renamed refs/heads/master to refs/heads/b1
    18feb84 b1@{3}: commit (initial): empty
    + git reflog b2
    f0c707d b2@{0}: merge b1: Merge made by the 'recursive' strategy.
    80c10e5 b2@{1}: commit: Add f2
    18feb84 b2@{2}: branch: Created from b1
    + cd /tmp/r2
    + git log --oneline --graph --decorate
    *   f0c707d (HEAD, origin/b2, origin/b1, origin/HEAD, b2, b1) merge branches
    |\
    | * 086c9ce Add f1
    * | 80c10e5 Add f2
    |/
    * 18feb84 empty
    + git reflog b1
    f0c707d b1@{0}: clone: from /tmp/r1
    + git reflog b2
    f0c707d b2@{0}: fetch origin b2:b2: storing head
    
    0 讨论(0)
  • 2020-11-22 11:26

    A poor man's option is to use the tool tig1 on HEAD, search for the commit, and then visually follow the line from that commit back up until a merge commit is seen. The default merge message should specify what branch is getting merged to where :)

    1 Tig is an ncurses-based text-mode interface for Git. It functions mainly as a Git repository browser, but it can also assist in staging changes for commit at chunk level and act as a pager for output from various Git commands.

    0 讨论(0)
  • 2020-11-22 11:27

    While Dav is correct that the information isn't directly stored, that doesn't mean you can't ever find out. Here are a few things you can do.

    Find branches the commit is on

    git branch -a --contains <commit>
    

    This will tell you all branches which have the given commit in their history. Obviously this is less useful if the commit's already been merged.

    Search the reflogs

    If you are working in the repository in which the commit was made, you can search the reflogs for the line for that commit. Reflogs older than 90 days are pruned by git-gc, so if the commit's too old, you won't find it. That said, you can do this:

    git reflog show --all | grep a871742
    

    to find commit a871742. Note that you MUST use the abbreviatd 7 first digits of the commit. The output should be something like this:

    a871742 refs/heads/completion@{0}: commit (amend): mpc-completion: total rewrite
    

    indicating that the commit was made on the branch "completion". The default output shows abbreviated commit hashes, so be sure not to search for the full hash or you won't find anything.

    git reflog show is actually just an alias for git log -g --abbrev-commit --pretty=oneline, so if you want to fiddle with the output format to make different things available to grep for, that's your starting point!

    If you're not working in the repository where the commit was made, the best you can do in this case is examine the reflogs and find when the commit was first introduced to your repository; with any luck, you fetched the branch it was committed to. This is a bit more complex, because you can't walk both the commit tree and reflogs simultaneously. You'd want to parse the reflog output, examining each hash to see if it contains the desired commit or not.

    Find a subsequent merge commit

    This is workflow-dependent, but with good workflows, commits are made on development branches which are then merged in. You could do this:

    git log --merges <commit>..
    

    to see merge commits that have the given commit as an ancestor. (If the commit was only merged once, the first one should be the merge you're after; otherwise you'll have to examine a few, I suppose.) The merge commit message should contain the branch name that was merged.

    If you want to be able to count on doing this, you may want to use the --no-ff option to git merge to force merge commit creation even in the fast-forward case. (Don't get too eager, though. That could become obfuscating if overused.) VonC's answer to a related question helpfully elaborates on this topic.

    0 讨论(0)
提交回复
热议问题