Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using git attributes from command line

I have this configuration on my repo:

echo '*.java diff=java' >> .gitattributes

and when I do git diff ... I get the expected results.

However, I'm doing a script to analyse other git repositories, so I usually do:

git --git-dir='/path/to/repo/.git' diff ...

What I would like to do is to run the script like that, and have the same results as if the attribute was present. I don't want to set global attributes or add a .gitattributes to the git project I want to analyse. I wouldn't mind something like

git --git-dir='/path/to/repo/.git' diff --word-diff-regex=<java-rexeg> 

But I don't know how to get the java regex git uses.

like image 347
Nico Avatar asked Aug 08 '26 04:08

Nico


1 Answers

From git help config:

core.attributesfile

In addition to .gitattributes (per-directory) and .git/info/attributes, git looks into this file for attributes (see gitattributes(5)). Path expansions are made the same way as for core.excludesfile. Its default value is $XDG_CONFIG_HOME/git/attributes. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/attributes is used instead.

In other words, you can create your own attributes file at $XDG_CONFIG_HOME/git/attributes, and pass XDG_CONFIG_HOME to your git invocation like this:

XDG_CONFIG_HOME=config_dir git --git-dir='/path/to/repo/.git' diff ...

Because you control exactly what is XDG_CONFIG_HOME, it would not be truly global - you can do it totally on a fly.

Or, it may be simpler to create global git attributes file in $HOME/.config/git/attributes instead.

like image 180
mvp Avatar answered Aug 10 '26 17:08

mvp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!