List commits not touching specific paths

I need to list commits that touch one path in the source tree but not another.

D – head

  • TortoiseGit 1.7.8: “Couldn't load this key” on “sync” or “pull”?
  • Configuring a diff-tool for Git on Windows
  • VSTS missing certain build flag: env:BUILD_SOURCEVERSIONMESSAGE
  • Eclipse EGIT - all committed, pulled, merged, marked as merged, still on push I get “rejected - non-fast forward”, what am I missing?
  • git rm -r doesn't work in a submodule
  • Programmatic git status
  • C – touched foo and bar

    B – touched foo

    A – baseline

    Between A and D I want a list of all commits touching folder foo unless they also touch folder bar, in other words, in the example above I would expect to see B but not C.

    I tried git rev-list A..D --exclude='bar' -- foo but that gives me both B and C still.

    Please advise.

  • When using Git, how to tell which branch a new branch is created based on?
  • How to enable multiple usernames for git? ie. Specfic project can have specfic username
  • Merging develop branch into master
  • How can I show the git branch of a rails app within that app?
  • Prevent EOL normalization for CSV files
  • Python Development in multiple repositories
  • 2 Solutions collect form web for “List commits not touching specific paths”

    There is nothing built into Git to do this. However, it’s easy to construct with a small shell script using the comm command. Simply get a list of all commits that affect interesting files, and a second list of all commits that affect files that make the commit uninteresting, then use comm to extract the interesting items (those that appear in the first list, but not in both the first and second list).


    git rev-list A..D -- foo > /tmp/list1
    git rev-list A..D -- bar > /tmp/list2
    comm -23 /tmp/list[12]

    Note that the “sorted” requirement for comm is deliberately violate-able (and is violated here); if your particular comm insists on it, you may need to write your own variation.

    Edit: I tested VonC’s suggestion, in Git 2.8.1, by making a new repository with an initial commit with files A/a and B/b:

    $ git --version
    git version 2.8.1
    $ git log --oneline --name-status
    731a059 both
    M       A/a
    M       B/b
    6894130 B only
    M       B/b
    b4bb2d8 A only
    M       A/a
    f134515 initial
    A       A/a
    A       B/b

    Adding -- A correctly discards the B only commit:

    $ git log --oneline --name-status -- A
    731a059 both
    M       A/a
    b4bb2d8 A only
    M       A/a
    f134515 initial
    A       A/a

    However, adding :(exclude):B has no effect on the selected commits:

    $ git log --oneline --name-status -- A ':(exclude):B'
    731a059 both
    M       A/a
    b4bb2d8 A only
    M       A/a
    f134515 initial
    A       A/a

    Commit 731a059, which touches files A/a and B/b, still appears in the output, as does initial commit f134515. The difference is that when the two commits that also affect B/b are selected, the file B/b is omitted. The original question calls for the entire commit to be excluded.

    Edit 2 (using comm and bash’s <(...) syntax, to omit the temp files):

    [~/tmp/git-exclude]$ comm -23 <(git rev-list HEAD -- A) <(git rev-list HEAD -- B)

    This is the desired output, since only commit b4bb2 affects only the A directory.

    Since Git 1.9, and the introduction pathspec magic :(exclude) and its short form :!, the syntax should be:

     git rev-list A..D -- foo ":(exclude)bar"

    However, this would still list C, even though the files displayed for that commit would not include files with bar in its path:

     git log --oneline --name-status A..D -- foo ":(exclude)bar"

    The pathspec exclusion works, but C is still listed.

    Git Baby is a git and github fan, let's start git clone.