Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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

  • How to list all commits that changed a specific file?
  • How can I view a git log of just one user's commits?

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 e.g. 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 e.g. --graph

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

like image 587
Raman Avatar asked Apr 29 '20 19:04

Raman


People also ask

How do I find a specific commit in git?

To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

How do you see changes in a specific commit?

If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

How do I see commits in a specific file?

On Linux you can use gitk for this. It can be installed using "sudo apt-get install git-gui gitk". It can be used to see commits of a specific file by "gitk <Filename>".


2 Answers

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.

enter image description here

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.

like image 129
sergej Avatar answered Oct 21 '22 16:10

sergej


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)

like image 24
Raman Avatar answered Oct 21 '22 15:10

Raman