开发者

git diff branch --name-status --ignore

开发者 https://www.devze.com 2023-02-27 16:19 出处:网络
Is there a way to ignore a path(s) when doing git diff? such as git diff branch --name-status --ignore=thi开发者_StackOverflows/pathThis is a bit of a hack but you could do this.

Is there a way to ignore a path(s) when doing git diff?

such as git diff branch --name-status --ignore=thi开发者_StackOverflows/path


This is a bit of a hack but you could do this.

git diff banch --name-status | grep -v this/path

Not entirely git, but does the job.


As they ~say here: Exclude a directory from git diff, you can use something like this:

git diff --name-only $branch_name | grep -v -E "$excluding" | xargs git diff $branch_name --

...where branch_name is the other branch and excluding is the pattern you'd like to exclude. I'm not sure if git-diff will eventually provide a parameter for something like that, and I didn't find such an option in the git documentation, so this command is what I use for now.

The only caveat is that you have to be at the top level of your working directory (i.e., the same folder that contains your .git folder) for this command to work, since the paths you pass in to git diff -- at the end via xargs are relative to that.

You can make a script out of this to do the work for you in your favorite shell, e.g.

#!/bin/zsh

if [[ $# < 2 ]] ; then
    echo "Usage: \n   gdx [git-diff-params] branch_name extended_reg_exp\n"
    echo "\nExamples:\n   gdx development jpg \n   gdx --name-only master origin/master jar \n   gdx development '[a-zA-Z]+'"
    exit 1
fi

excluding="$@[-1]"
branch_name="$@[-2]"
diff_options="$@[1,-3]"

## Get a list of all the paths to include
git_paths=$(git diff --name-only $branch_name | grep -v -E "$excluding")

if [[ ${#git_paths} = 0 ]] ; then
    #no diffs
    exit 0
else
    ## Escape special characters
    git_paths=(${(f)git_paths})
    for i in {1..${#git_paths}}
    do
        git_path=$git_paths[i]
        git_path=\"${git_path//\"/\"}\"
        git_paths[i]=$git_path
    done

    ## Run the diff
    eval "git diff ${=diff_options} $branch_name -- $git_paths"
fi

This takes arguments and prints usage and whatnot. I use it like so:

gdx HEAD^ "*.jar"


You can add the paths you don't care about to your .gitignore file.

EDIT: You can use git diff files/you/care/about to see the diffs you are interested in.

For example, if git status shows that a bunch of files are changed:

modified: foo/bar
modified: foo/baz
modified: some/more/stuff
modified: and/some/more

you can use git diff foo to just look at the changes in the foo directory, or git diff some/more/stuff and/some/more to look at everything except foo.

This will be fine if you want to look at the diffs for a few files, but won't work so well if you want to look at everything except one or two files. I don't think there's a way to tell git diff to use a negation of the files you specify.

Another alternative would be to use git add --interactive, but it's probably a bit cumbersome for what you're doing.

It also turns out that .gitignore doesn't ignore files that are already under version control, so that idea was never going to work. Forget I mentioned it. :(

0

精彩评论

暂无评论...
验证码 换一张
取 消