Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can git be told to use spaces in ~/.gitconfig?

When I use git config --global <some option> <some value>, Git will write the relevant entry in my ~/.gitconfig, indented by one tab. Since my ~/.gitconfig is version-controlled and I want it to not be a mess, I then have to go in by hand and replace the tab with spaces.

Can Git be told to use spaces automatically when adding entries to ~/.gitconfig?

(Please note that this is not about the indentation in the code that I'm committing with Git, but rather the indentation in Git's own configuration file.)

like image 429
Radon Rosborough Avatar asked Oct 17 '16 16:10

Radon Rosborough


People also ask

What is ~/ Gitconfig?

gitconfig is used to store a per-user configuration as fallback values for the . git/config file. The file /etc/gitconfig can be used to store a system-wide default configuration. The configuration variables are used by both the Git plumbing and the porcelains.

How do I change Gitconfig?

The global git config is simply a text file, so it can be edited with whatever text editor you choose. Open, edit global git config, save and close, and the changes will take effect the next time you issue a git command. It's that easy.

Where is Gitconfig saved?

The file at %USERPROFILE%\. gitconfig is considered the master global file where you make all your changes.

Why is there no Gitconfig file?

The reason they can't find gitconfig is simply because it's nowhere to be found. When developers install Git, the various Git configuration files won't automatically create. Files like gitconfig and . gitconfig are only created when they're first used.


1 Answers

This is what worked for me (note that as @rasjani has pointed out in a comment, there is no option to prevent git from inserting a tab in the first place when using git config.)


Create the filter

Create a filter to convert tabs to spaces automatically as soon as you do a git add for a file. This filter is created by

git config --global filter.spacify.clean 'expand --tabs=4 --initial'

The expand command says that convert each tab character at the beginning of line to 4 space characters

Therefore the definition of the filter includes both what it does and for when it does it (i.e. for which git operation).

(On OSX, you would need to use gexpand after installing coreutils by doing a brew install coreutils)

Of course, you would need to decide the scope (--system, --global or the default --local) of the above configuration.


Specify the files/path patterns to which the filter will be applied

For instance, for your repository, create a .git/info/attributes with the following content:

.* filter=spacify

This says that apply the spacify filter to any files that match the pattern .* before these files are committed to the repository.


Note that the above will only affect new files being added to the repository. If you want this to be done for all existing files, then you could either run expand manually OR, could get git to do it for us like so:

git config --global filter.spacify.smudge 'expand --tabs=4 --initial'
git checkout HEAD -- **

Using the spacify filter for smudge will cause the filter to be applied to files being checked out. After the checkout, you should see a bunch of changes to the dot files which had leading tabs converted to spaces. Re-commit these and from henceforth, the smudge and clean duo shall keep your dotfiles tab-free!


Update -- The pull request!

Here is a pull request for your repo : https://github.com/raxod502/radian/pull/156

like image 167
Ashutosh Jindal Avatar answered Sep 29 '22 17:09

Ashutosh Jindal