Search code examples
gitsudogit-config

How do I use my personal git config when committing as root?


I use etckeeper for revision control of my /etc directory.

Due to the owership of the files under /etc, I need to run git as root.

How can I do this and still easily have access to all my beloved git aliases and other config?


Solution

  • git -c include.path=<file> will include the configuration in <file>.

    To automatically pull in my non-root user's config files, I use the bash alias:

    alias gsudo='sudo git -c "include.path='"${XDG_CONFIG_DIR:-$HOME/.config}/git/config\" -c \"include.path=$HOME/.gitconfig\""
    

    Then I use gsudo instead of git to both:

    • Run as root
    • Have access to all non-root user git configuration

    Check that the config is indeed being imported:

    gsudo config --list --show-origin --includes | less
    

    Of course, you should be careful if you have any root-unsafe configuration.

    Perhaps having both a "safe" and a "potentially dangerous" config file is the way to go. The user's ~/.config/git/config could be the "potentially dangerous" one which [include]s the "safe" root-only config file (which is the only one used in the gsudo alias).