How do I see a commit's path through git histo

2019-03-16 20:36发布

问题:

I'm looking in the commit history using gitk and git log and I'm trying to see how a specific commit arrived in a certain branch. I can see the commits in the history, so I know they are there.

What I want to understand is how they got merged (they were supposed to remain on their own branch). This is a very large project and there are hundreds of commits between the commit in question and the current state of the branch, so I cannot clearly decipher through the limited DAG in gitk, and the commit gets masked in other branches and merges and commit messages.

To do this, I have been trying:

gitk {sha1hashIDstring}..branch_name
gitk {sha1hashIDstring}..branch_name --ancestry-path
git log {sha1hashIDstring}..branch_name --reverse
git log {sha1hashIDstring}..branch_name --merges --reverse
git log {sha1hashIDstring}..branch_name --ancestry-path --reverse
git log {sha1hashIDstring}..branch_name --ancestry-path --merges --reverse

And I'm not understanding the results. I ONLY want to see items that include the specific commit in question, such that I see clearly how it got into the branch in question. How do I do so?

Example

What I'm looking for, in gitk preferrably but git log would suffice:

Message       Author         Date         #commit that merged branch z into current branch
Message       Author         Date         #commit that merged branch y into branch z
Message       Author         Date         #commit that merged branch x into branch y
Message       Author         Date         #commit that merged {sha1hashIDstring} commit/branch a into branch x
Message       Orig_Author    Date         #{sha1hashIDstring} original commit, on branch a

More Information

I'm not seeing any answers yet, so I'll start a bounty if none come in, but perhaps I'm not explaining the question right (I'm open to suggestions to improve and clarify).

The driver for this is that I can see the commit itself, and I'm being told it should not be on a certain branch. Here's what I'm seeing:

Message       Orig_Author    Date         #{sha1hashIDstring} commit
Message       Orig_Author    Date         #Merged into branch test_dec14 (includes original commit)
...
Message       Author         Date         # unrelated commits
Message       Author         Date         # more unrelated commits
# Stuff happened here ??? everything I do gives me hundreds of things here 
# Not all of them related to the {sha1hashIDstring} commit
# No idea how to see only the ones that are
...
Message       Author         Date         # final commit on test_jan15 branch

I'm being told commits in test_dec14 should not have made it to test_jan15 unless they were released, and as such the {sha1hashIDstring} commit SHOULD NOT BE in test_jan15, yet it is. I want to know why, how it got there, and who put it there.

回答1:

For the latter part of your question, "how it got in the current branch?", take a look at git-when-merged.

It's a Python script that will, per its readme:

Find when a commit was merged into one or more branches. Find the merge commit that brought COMMIT into the specified BRANCH(es). Specifically, look for the oldest commit on the first-parent history of BRANCH that contains the COMMIT as an ancestor.

This sounds like what you're looking for in the case of determining when the {sha1hashIDstring} commit was merged into test_jan15 branch.



回答2:

This is a classic case of git bisect. bisect help you trace bugs. In your case you simply looking for a commitId (which is misplaced into the wrong branch).

Bisect is a very simple yet powerful tool.

http://git-scm.com/docs/git-bisect http://hashrocket.com/blog/posts/finding-failure-git-bisect

Hope it will help you out.



回答3:

Have you tried the "--decorate" option to git log?

I have this alias in my .gitconfig:

[alias]

        k = log --graph --oneline --abbrev-commit  --decorate

It shows a similar graph as the one shown by gitk, with the branch names "decorated" besides the most recent commit in the branch.


OR

--

Try tig as well. It is more informative than gitk (as per my usage ;)). Refer to http://gitready.com/advanced/2009/07/31/tig-the-ncurses-front-end-to-git.html once. I think either solution will give you required results.



回答4:

I googled something and got something for you. credit goes to #vonC

git when-merged [OPTIONS] COMMIT [BRANCH...]

Find when a commit was merged into one or more branches. Find the merge commit that brought COMMIT into the specified BRANCH(es).

Specificially, look for the oldest commit on the first-parent history of BRANCH that contains the COMMIT as an ancestor.

git-what-branch

Discover what branch a commit is on, or how it got to a named branch This is a Perl script from Seth Robertson that seems very interesting:

SYNOPSIS

git-what-branch [--allref] [--all] [--topo-order | --date-order ]
[--quiet] [--reference-branch=branchname] [--reference=reference]
<commit-hash/tag>...
OVERVIEW

Tells us (by default) the earliest causal path of commits and merges to cause the requested commit got onto a named branch. If a commit was made directly on a named branch, that obviously is the earliest path.

By earliest causal path, we mean the path which merged into a named branch the earliest, by commit time (unless --topo-order is specified).

PERFORMANCE

If many branches (e.g. hundreds) contain the commit, the system may take a long time (for a particular commit in the linux tree, it took 8 second to explore a branch, but there were over 200 candidate branches) to track down the path to each commit. Selection of a particular --reference-branch --reference tag to examine will be hundreds of times faster (if you have hundreds of candidate branches).

**EXAMPLES**
 # git-what-branch --all 1f9c381fa3e0b9b9042e310c69df87eaf9b46ea4
 1f9c381fa3e0b9b9042e310c69df87eaf9b46ea4 first merged onto master using the following minimal temporal path:
   v2.6.12-rc3-450-g1f9c381 merged up at v2.6.12-rc3-590-gbfd4bda (Thu May  5 08:59:37 2005)
   v2.6.12-rc3-590-gbfd4bda merged up at v2.6.12-rc3-461-g84e48b6 (Tue May  3 18:27:24 2005)
   v2.6.12-rc3-461-g84e48b6 is on master
   v2.6.12-rc3-461-g84e48b6 is on v2.6.12-n
   [...]


回答5:

I know this isn't tagged with "github" - but if the project is on there the visual style of Blame or History (buttons in file header when viewing a specific file) can make it a lot easier to trace when things happened.

Not sure if that actually solves this (very) specific issue - but it might help others with similar issues who find this question...