Changing .gitconfig location on Windows

后端 未结 13 2358
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 11:49

By default on Windows Git places global .gitconfig in c:\\documents and settings\\user\\

How can I change that position so .gitconfig is stored in

相关标签:
13条回答
  • 2020-11-27 12:03

    Solution without having to change Windows HOME variable
    OS: Windows 10
    git version: 2.27.0.windows.1

    I use portable version of Git, so all my config files are on my pen-drive(E:). This is what worked for me:

    1. Download portable Git from https://git-scm.com/. Run the file and install it in the required location. I installed it in E:\git.
    2. Run git-bash.exe from E:\git.
    3. I wanted to put .gitconfig and other bash files in E, so I created a folder called home where I want them all in.
      mkdir home
    4. Go to etc folder and open the file called profile (In my case, it's E:\git\etc\profile)
    5. Add absolute path to the home directory we created (or to the directory where you want to have your .gitconfig file located) at the end of the profile file.
      HOME="E:\git\home"

    Now it no longer searches in the C:\Users<username> directory for .gitconfig but only looks in your set path above.

    $ git config --global --list
    fatal: unable to read config file 'E:/git/home/.gitconfig': No such file or directory
    

    It gave an error because there isn't a .gitconfig file there yet. This is just to demonstrate that we have successfully changed the location of the .gitconfig file without changing the HOME directory in Windows.

    0 讨论(0)
  • 2020-11-27 12:08

    Change HOME directory for this is wrong. Better is create symbolic link for gitconfig to HOME directory.

    1. Move your .gitconfig from user home directory, to the directory where you want.
    2. Run command line as Administrator
    3. Go to your user home directory
    4. Enter mklink .gitconfig \PathForNewLocationOfConfig.gitconfig
    0 讨论(0)
  • 2020-11-27 12:12

    As someone who has been interested in this for a VERY LONG TIME. See from the manual:

    $XDG_CONFIG_HOME/git/config - Second user-specific configuration file. If $XDG_CONFIG_HOME is not set or empty, $HOME/.config/git/config will be used. Any single-valued variable set in this file will be overwritten by whatever is in ~/.gitconfig. It is a good idea not to create this file if you sometimes use older versions of Git, as support for this file was added fairly recently.

    Which was only recently added. This dump is from 2.15.0.

    Works for me.

    0 讨论(0)
  • 2020-11-27 12:16

    If you set HOME to c:\my_configuration_files\, then git will locate .gitconfig there. Editing environment variables is described here. You need to set the HOME variable, then re-open any cmd.exe window. Use the "set" command to verify that HOME indeed points to the right value.

    Changing HOME will, of course, also affect other applications. However, from reading git's source code, that appears to be the only way to change the location of these files without the need to adjust the command line. You should also consider Stefan's response: you can set the GIT_CONFIG variable. However, to give it the effect you desire, you need to pass the --global flag to all git invocations (plus any local .git/config files are ignored).

    0 讨论(0)
  • 2020-11-27 12:18

    Look in the FILES and ENVIRONMENT section of git help config.

    0 讨论(0)
  • 2020-11-27 12:18
    1. Change to folder %PROGRAMFILES%\Git\etc
    2. Edit file profile
    3. Add to first line HOME="c:\location_were_you_want_gitconfig"
    4. Done

    Note: The file permissions are usually restricted, so change them accordingly or you won't be able to save your changes.

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