Want to exclude file from “git diff”

后端 未结 13 1902
梦谈多话
梦谈多话 2020-12-07 07:27

I am trying to exclude a file (db/irrelevant.php) from a Git diff. I have tried putting a file in the db subdirectory called .gitattributes

相关标签:
13条回答
  • 2020-12-07 07:32

    This one-line solution requires no other utils/downloads:

    git diff `git status -s |grep -v ^\ D |grep -v file/to/exclude.txt |cut -b4-`
    

    Where file/to/exclude.txt is the name of the file you would like to exclude, of course.

    Edit: credit ksenzee for fixing deleted files breaking the diff.

    0 讨论(0)
  • 2020-12-07 07:35

    Relative to the git root directory

    git diff accepts an optional exclude

    git diff -- ":(exclude)thingToExclude"
    

    You might want to add some wild cards

    git diff -- ":(exclude)*/thingToExclude/*"
    

    Target specific file types

    git diff -- ":(exclude)*/$1/*.png"
    

    Or drop a little script for your dotfiles

    Such as .bash_profile or .zshrc

    gde() {
        # git diff exclude files or folders 
        # usage: 
        # gde fileOrFolderNameToExclude
        git diff -- ":(exclude)*/$1/*"
    }
    
    0 讨论(0)
  • 2020-12-07 07:38

    Building on existing answers, if you want to exclude a file pattern no matter where it is in the directory, use the corresponding git pathspec value of **:

    git diff -- ':!**/yarn.lock'
    
    0 讨论(0)
  • 2020-12-07 07:41

    You can also use filterdiff program of the patchutils program collection to exclude some parts of a diff. For example:

    git diff | filterdiff -p 1 -x db/irrelevant.php
    
    0 讨论(0)
  • 2020-12-07 07:42

    similar to Ben Roux's solution, but sharing anyway:

    git status --porcelain | grep -v $PATTERN_TO_EXCLUDE | awk '{print $2}' | xargs git diff
    

    or, if the changes have already be committed locally:

    git diff --name-only origin/master | grep -v $PATTERN_TO_EXCLUDE | xargs git diff origin/master
    

    Examples:

    git status --porcelain | grep -v docs | awk '{print $2}' | xargs git diff origin/master
    
    git diff --name-only origin/master | grep -v docs | xargs git diff origin/master
    
    0 讨论(0)
  • 2020-12-07 07:43

    This method is shorter than the accepted answers.

    git diff 987200fbfb 878cee40ba --stat -- ':!*.cs'
    

    For more information about the different inclusion/exclusion possibilities read this other post

    0 讨论(0)
提交回复
热议问题