2

I have the problem, that we login to the systems as our own users johndoe and then have to do a sudo su - root to be able to do administrative tasks. We have the system config files in a git repository (which makes everything better). But now I lose the settings in the user's (johndoe) /home/johndoe/.gitconfig file. I tried to set the $GIT_CONFIGvariable which works, but this is only for setting the repo configuration, not the global one and therefore the for me important variables won't be set:

$ sudo su - root
Hello johndoe. Switching your .gitconfig to /home/johndoe/.gitconfig
# git config --get user.name
John Doe
# git config --get user.email
[email protected]
# git config --global --get user.name
# git config --global --get user.email
#

This is the trick I tried:

$ cat .bashrc
--- snip ---

# load git config of user
LOGINUSER=$(who am i|awk '{print $1}')
if [ "$LOGINUSER" != "root" ]; then
    GITCFGFILE="/home/$LOGINUSER/.gitconfig"
    if [ -f $GITCFGFILE ]; then
        echo "Hello $LOGINUSER. Switching your .gitconfig to $GITCFGFILE"
        export GIT_CONFIG=$GITCFGFILE
    fi
fi

Is there a way to set the path for the global git configuration?

6
  • Using something like linking the .gitconfig file (as suggested in different other posts) is not a good solution because the possibility exists, that more than one user is logged in concurrently. Commented Sep 19, 2014 at 6:46
  • It seems to be strange to ask whether to use git or su. You should probably write down a problem statement, i.e. what exactly are you trying to achieve at the beginning of your question. Btw, sudo su - is just a more complicated way to do sudo -i? Also git config --global is actually local to the user, [sudo] git config --system handles the system-wide config. Commented Sep 19, 2014 at 7:33
  • 1
    Wouldn't it be better to create a bunch of "root users", like 'johndoeroot', who are members of the root group and have root privileges? Then everyone could su to his own rootuser and everyone can have his own git-config.
    – Campfire
    Commented Sep 19, 2014 at 9:05
  • +1 for @Campfire, that way you can also distinguish who did what and when.
    – Jan
    Commented Sep 19, 2014 at 10:57
  • @PavelŠimerda: Title corrected and theoretically yes it is just more complicated. But thats the way "they" want us to do this, so i have to follow this rule. Commented Sep 19, 2014 at 11:55

2 Answers 2

1

To answer my Question: No

This is the solution i ended up with: I added a file /etc/profile.d/gitsetup.sh with the following content:

[ -z "$PS1" ] && return
[ -z "$BASH_VERSION" -o -z "$PS1" ] && return
CFGFILE="/home/$(who am i|awk '{ print $1 }')/.gitconfig"
[ $USER == "root" -a -f "$CFGFILE" -a -n "$CFGFILE" ] && cat $CFGFILE > $HOME/.gitconfig

It tests on line one and two for an interactive shell, reads the source .gitconfig path on line three and then replaces the roots config file if the source file exists.

0

I jumped through all the same hoops and found the same issue with user information stored in $HOME/.gitconfig not having environment variable counterparts for our version of git.

I came across a situation that may affect you -- the .gitconfig file is clobbered when a second user is invovled -- and my own solution is questionable enough for me (a newbie git user) that it can be found posted here:

Dynamic user config for git with wrapper script?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .