2

I would like a way to log certain commits and also submit path arguments so that the diff on those paths, if any, is shown for each commit as if those path arguments were supplied like git diff -- <path args>.

git log -p <path args> almost accomplishes this. Here's how I'd like it to work:

$ git log develop..mybranch -p mydir
1010101 my commit msg
(diff of files in mydir)
2323232 my other commit msg
(nothing here because the commit doesn't touch mydir)

The problem is that the second commit wouldn't be shown, because it does not touch mydir.

How can I achieve the equivalent of git log -p <path args>, but not skipping commits that don't touch the given paths?

1
  • 1
    Just log everything with patches, and filter out all file diffs that are not among the paths that interest you? Commented Oct 29, 2024 at 13:53

1 Answer 1

3

git log <path args> automatically limits commits to those touching <path args> and there is no way to list other commits. Hence there is no simple way to do what you want. Can be done by listing all commits, parsing the list, looking into every commit and processing commits one by one. Like this:

git rev-list develop..mybranch |
    while read sha1; do
        git show -s $sha1 # show the commit
        git diff $sha1~ $sha1 -- mydir # will be empty if the commit doesn't touch mydir
    done

Two commands (git show, git diff) for every commit. On a big repository it could be slow so the code would require optimization.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It's not quite what I was looking for, since your example shows the whole diff instead of only the diff on mydir. Rather, the if-else should be replaced by git show -s $sha1 and then git diff $sha1~ $sha1 -- mydir. (P.S. It seems that git show -- ... works the same way as git log ... in that it doesn't show the "log line" if the diff is empty, so that's why we need to combine show and diff.)
The diff line doesn't need an if. show + diff is sufficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.