73

I'm getting this error when trying to set the global config:

$ git config --global user.name "Your Name Here"
error: could not lock config file /pathto/file/.gitconfig: No such file or directory

and the file .gitconfig is already exists, anyone have any ideas??

codepros
  • 878
  • 1
  • 6
  • 12

22 Answers22

42

A bit like in "Trouble setting up Tower with my GitHub Account - error: could not lock config file", check how that ~/.gitconfig file has been created.
Ie: with which rights associated to it?

Make also sure your $HOME variable is correctly set when you are executing the git config --global command.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
28

just do Run as Administrator.......you need to run the program in the run as administrator mode in windows

praveen
  • 281
  • 3
  • 2
26

Check if you have a .git directory in your home folder and if you don't:

mkdir ~/.git

Solved the problem in my case.

przemek
  • 451
  • 4
  • 6
23

I've gotten this error when a lock file exists for gitconfig. Try and find and remove .gitconfig.lock (on my linux box it was in my home dir)

Andrew Cassidy
  • 2,940
  • 1
  • 22
  • 46
  • 3
    Similar here - I had a "config.lock" file in my .git directory. I renamed it back to just "config". Not sure what caused it to lock, I was just doing normal branching/push/pull. Edit: Might have been because I use git bash alongside a Visual Studio git plugin... – Lyall Nov 26 '13 at 16:09
  • Had to delete `../.git/svn/.metadata.lock`. Looks like any `.lock` file can cause problems. – Tobi G. Dec 14 '16 at 12:28
22

Windows Users: Ensure your Environment Variables are correctly setup.

I had the following for my account username 'paperclip':

My Computer (right-click) > Properties > Advanced (tab) > Environment Variables (under System Variables):

HOME -> %HOMEPATH%

HOMEPATH -> C:\Documents and Settings\paperclip

It seems like Git could not resolve / expand %HOME% to %HOMEPATH% as you would it expect it to. Instead I needed to make %HOME% the same as %HOMEPATH% by changing it to:

HOME -> C:\Documents and Settings\paperclip

paperclip
  • 2,280
  • 6
  • 28
  • 39
  • 2
    I don't think you need to do that. You can simply remove the home from environment variables and it will work. – Troy Woo Dec 01 '14 at 18:10
  • My case was something similar to this. I created a variable home that had a misspelling in my user name. I reinstalled git several times, but no help. I could not even save any configurations. Finally, seeing this answer, I deleted the variable home, and everything is fine. – Sнаđошƒаӽ Apr 03 '16 at 11:21
  • I had the same issue - TortoiseGit couldn't resolve the "double indirection" `%HOME% -> %USEPROFILE% -> C:\Users\andre`, and had to directly set `%HOME% = C:\Users\andre` . – Andre Dec 22 '17 at 11:33
14

In Windows: Right click on "Git Bash" icon -> Run as Administrator. it helped me.

Git tries to create a config file on disk C:/ but it has no permission to do that.

Jeyanthan I
  • 1,611
  • 15
  • 28
savelich
  • 151
  • 1
  • 3
  • 1
    Thank you. Running Command Prompt as Administrator worked for me. I don't understand, however, why this extra step, when the user I'm currently using is already an administrator. – ITWitch Apr 23 '17 at 16:47
7

This will happen, if the user home directory is not writable by the user. git config --global needs to create a "lock" file (~/.gitconfig.lock) in user home directory.

Check the permissions and try to create one.

# cat ~/.gitconfig.lock
cat: /home/users/developer/.gitconfig.lock: No such file or directory
# touch ~/.gitconfig.lock

## Now check, if a file has been created?
# cat ~/.gitconfig.lock
#

NOTE: If it succeeds, You must delete this file, otherwise git will throw another error!

If a user has no permission of creating this file, You must check and change permissions for the home directory.

Arnis Juraga
  • 1,027
  • 14
  • 31
  • Thank you I had issues with reconfiguring and it wouldn't take my command as my own user when using --system option and even-though I have everything configured in environment variables my commands such as maven or git does not work on admin console while java does. I just gave myself permission to anything in Git which is not a good practice but it works. – 猫IT Aug 24 '17 at 12:13
3

Just use the following command if you wanna set configuration in system level:

$ sudo git config --system user.name "my_name"
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Paul Liu
  • 31
  • 3
3

I rename the .gitconfig file as xyz.gitconfig, then git will generate a new .gitconfig file, that wokrd

Amber Zhang
  • 182
  • 1
  • 6
2

Update you Git client plugin. It worked for me =) https://issues.jenkins-ci.org/browse/JENKINS-21016

Akira Yamamoto
  • 4,685
  • 4
  • 42
  • 43
2

I have "experienced" this error on Windows because my name (and hence %HOMEPATH%) contains a non ascii character (é). Either git or cmd.exe or anything else could not cope with this.

René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
2

This could be caused by the presence of ~/.gitconfig.lock It's possible this file could be an artifact of a previously running git that was aborted for some reason, e.g. ansible timed out or ^C

Joseph Tingiris
  • 176
  • 1
  • 6
2

I am old to the party but may be this will help some one. Thanks to @paperclip

In Windows 10:

Step 1: Go to This PC > Right click Properties

step 2: Click Advanced System Settings and click Environment Variables

Step 3: Under System Variables create new variable called HOME and input the value as %USERPROFILE% like below

enter image description here

Step 4: Important You must restart your PC to take effect

Step 5: Install Git for Windows now and optional Tortoise Git for windows if you prefer.

Make a git clone request or try pushing something in to your repo. Magic it will work. All should work fine now.

Ragavan Rajan
  • 4,171
  • 1
  • 25
  • 43
2

This is what worked for me,

  1. open Start Menu
  2. type "environment" without quotes
  3. choose edit environment variables (system or user, any)
  4. under the user variables, select HOME
  5. click edit
  6. add the missing % prefix of %USERPROFILE%
  7. keep clicking OK for all open windows

[Step by step instruction to current the user variable on windows 10]

user145657
  • 31
  • 1
  • 1
  • 3
1

For me, the problem was not git config, but .git directory at my current repo was created by root and I was trying to do something with my other user. I changed the perm

bn00d
  • 1,126
  • 3
  • 15
  • 21
1

This is an old post, but I had this issue recently, but I solved the issue differently from everyone else here. I received the same error because I did not have permission to the $HOME path on my computer. Simply by accessing the folder via double-clicking through the folders in your files, I was prompted with boxes saying that I would gain permanent access to these folders after entering through them for the first time. After I accessed the deepest folder in my $HOME variable, the command worked.

FlamePrinz
  • 490
  • 1
  • 5
  • 20
1

You can also try issuing the command while in your home directory.

kmiklas
  • 13,085
  • 22
  • 67
  • 103
1

In my case, the .git/config file was created not in my $HOME directory, but inside my repository on .git/config.lock

Deleting the file fixed the problem. The file is created, when I switch branches with git checkout -f branchname and files from a recently created submodule were overwritten by git checkout.

$ git branch --set-upstream-to=origin/branchname
error: could not lock config file .git/config: File exists
error: Unable to write upstream branch configuration
hint: 
hint: After fixing the error cause you may try to fix up
hint: the remote tracking information by invoking
hint: "git branch --set-upstream-to=origin/branchname".
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
0

I started getting this error after windows upgrade. My Home variable somehow removed after windows upgrade. Issue resolved by adding HOME variable as "C"\Users\myusername".

0

So I know this thread is Old, but I had the same issue and fixed it. Hopefully this works for someone else.

When i tried using "sudo" or anything in powershell/cmd it was an unrecognized command. So i reinstalled git for windows, during the install it failed and pointed me to C:/ProgramFiles/git/etc/gitconfig I deleted that file, and reinstalled. Same Error when saving credentials, So i moved the newly created gitconfig from programfiles, to my HomePath location C:/Users/Name

Now I can save credentials under file-->Options-->git Finally, I can commit/push on githubdesktop

0

I faced the same problem and tried several solutions provided here and here is what works for me

Deleting both .gitconfig and .gitconfig.lock files and then run the setting of the global config again fix that for me, just run this in order

rm -rf ~/.gitconfig.lock ~/.gitconfig

then

git config --global user.name "YOUR NAME"

medhatdawoud
  • 1,168
  • 2
  • 12
  • 18
0

You just go to the main folder of your repository and use the commands:

git config user.name "UserName"

git config user.email "aName@someMail.com"

This worked for me.