zdenko.s
zdenko.s

Reputation: 1032

git for windows - does not load %USERPROFILE%\.gitconfig when launched "run as"

When I run command git config --list --show-origin it shows entries from .gitconfig located in home directory. If I launch command prompt using runas, same command does not show entries from %USERPROFILE%.gitconfig. I verified that file exist. My aim is to run git as different user. All commands works fine except user name/e-mail is not taken from .gitconfig but default values are used. When I run git config --global -e vi shows path to .gitconfig in my home directory. When I run same command from command prompt launched using runas, vi shows path c:\windows\system32\.gitconfig

How to force git to load .gitconfig from home folder when command prompt launched runas?

Upvotes: 3

Views: 3946

Answers (2)

zdenko.s
zdenko.s

Reputation: 1032

I found a solution. On Windows, the user's home directory is defined by the environment variable USERPROFILE. Git doesn't actually use it and instead reads the value of the env variable HOME. If it is not set, it combines HOMEDRIVE and HOMEPATH to build HOME variable. When running runas, HOMEPATH is set to Windows\system32.

Git should use USERPROFILE if you set the HOME variable accordingly:

set HOME=%USERPROFILE%

Upvotes: 8

it3xl
it3xl

Reputation: 2672

One more bug here, and I would recommend to use

in CMD

CD /D %SYSTEMDRIVE%\
@REM  Or just use "CD .."

SET HOME=%USERPROFILE%

I.e. when you start CMD by using "Run as a different user" you must move out of the home directory of your Windows logon user.

But in bash will be enough to use

HOME=$USERPROFILE

because bash has another default startup folder.

Bug Description

Suppose you are logged into Windows as an ITretyakov account.
Your user profile directory would be C:/Users/ITretyakov.
You have a service account named as some_automoation.
And you're opening CMD console by using "Run as a different user" for the some_automoation account.
But what's important, your startup directory will be C:/Users/ITretyakov.
And some_automoation account has no any access rights for this directory.

Run

git config --global --list

and you'll receive the following error

fatal: failed to stat 'C:/Users/ITretyakov': Permission denied

Git wants to read current working directory and fails on it.
Now set working directory to the system drive (C:) or to any another location.

CD /D %SYSTEMDRIVE%\
git config --global --list

and you'll receive another expecting error

fatal: unable to read config file 'C:/Windows/system32/.gitconfig': No such file or directory

and finally we use the fix of zdenko.s

SET HOME=%USERPROFILE%

Now you are ready to access .gitconfig of the some_automoation account that is located in
C:/Users/some_automoation

Conditions:
git version 2.21.0.windows.1;
Windows 10

Upvotes: 0

Related Questions