简体   繁体   中英

Show specific commits in git log, in context of other commits?

There are various ways to "select" commits with git log. For example:

and many others.

However, all of these show only the commits selected for on the command line. What I want is to see all the commits in my range, but highlight (with color, or a marker, or whatever) a specific subset of these commits eg the commits that changed a particular file or whatever. So when doing:

git log --oneline master..@ -- path/to/frobnitz

instead of seeing:

12ca6d863 foo
6166da1fd bar
894567343 baz

I would see something like:

46984ad11 (HEAD -> master) git is fun!
2e11a5382 cool beans
>> 12ca6d863 foo
60069036d whatever
d698663d0 something
>> 6166da1fd bar
3d2c811e3 more cool stuff
>> 894567343 baz
3d2c811e3 cool stuff

Furthermore, the ideal solution would work with --graph mode, because I also want to see the merge and branch contexts of the selected commits.

I also note that git log supports various History Simplification scenarios, which get me almost what I want in some cases, but its not easy to figure out how, nor is it exactly what I want. I already have the history I want to see, and I already have the commits I want to highlight.

Some ideas I had, but I don't like any of them:

  • Script it -- run two git logs and then use the output of one to decorate/manipulate the other. The downside of this is that its brittle and it won't work well for different sets of options I might supply to the target log eg --graph

  • For the "selected" commits, assign temporary refs eg selectedcommits to them, and then use --decorate-refs=selectedcommits to show the relevant commits. This seems messy.

Just for fun, while waiting for a better answer.

git log --oneline --decorate=no --graph | less -p $(git log --pretty=%h -- Makefile | tr '\n' '|')

The idea is to pipe the output through, for example, less for highlighting specific commits.

在此处输入图像描述

Tested in a bash on Linux and git-bash on Windows.

Here a first try for an alias:

[alias] hl-graph = !git log --oneline --graph --color | less -R -p $(git log --pretty=%h \"$@\" | tr '\n' '|') && :

Commits to be highlited can be defined as usual:

 git hl-graph --since="1 month ago" -- path/to/frobnitz

However the log format can't be modified.

Following up on the answer by @sergej (upvote it,), and recognizing that the repetitive part of the command which needs to be aliased is really just the highlighting: I've configured these aliases:

  hl = "!f() { cd -- ${GIT_PREFIX:-.}; grep --color -E \"$(git log --pretty=%h \"$@\" | tr '\n' '|')\" || true; }; f"
  hlp = "!f() { cd -- ${GIT_PREFIX:-.}; less -R -p $(git log --pretty=%h \"$@\" | tr '\n' '|'); }; f"

These are used by piping the output of any log command to the alias. For example:

git log --oneline --graph --color | git hl --since="1 month ago" -- path/to/frobnitz

(or for the paged version, use hlp )

Try the following git command:

git log --name-only --pretty=format:"%h %s" --graph

This command will show all changed files ( --name-only ), the git commit-hash ( %h ), the topic from the command (comment from git commit command -> %s ) and also the branches drawn by different color-lines.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM