52

In my git repositiory, whenever I do any git commands like git status or git log, I get the error:
fatal: bad config file line 1 in .git/config
How do I rebuild that file or a new one?

Spencer
  • 1,476
  • 1
  • 14
  • 26
  • 1
    May you post the content of the file? – iltempo Mar 01 '12 at 01:01
  • According to notepad, it's empty. – Spencer Mar 01 '12 at 01:03
  • Are there any commits in your repository? If not simply remove the `.git` directory and reinitialize an empty git repository with `git init` – iltempo Mar 01 '12 at 07:57
  • There were commits, I ended up re-cloning from github and copying the changes into the new directory. – Spencer Mar 01 '12 at 15:20
  • 1
    I think you can go to .git folder directly and edit the config file in any editor and save it. i have the same case when i trying to edit the file on command line. *fatal: bad config file line 1 in .git/config (this issue may you have left the '[' brackets).* – Amit kumar Jul 15 '19 at 06:47
  • Delete the blank spaces from C:\ProgramData/Git/config ....it will work fine. – Akhilesh Sinha Jul 16 '19 at 12:23

31 Answers31

25

For those hitting into this issue (I believe it's due to crashing out git mid init?) in windows, if you have a recent backup of the config file from your git repo's .git/ folder you can fix it by replacing the existing with it (any ref's added since the copy will obviously need re-adding) - not at all ideal but better than loosing all the commits.

Woody Hayday
  • 336
  • 3
  • 8
  • 1
    Spot on woody. Checked and sure enough my .gitconfig file was empty. Restored the file from a backup and all is well on the farm. – JSWilson Jul 16 '13 at 18:36
  • But, I did not backup that config file until I saw this answer. Any alternatives for newbies? I was on a search for looking for a solution same problem as mine. :-\ – David Dimalanta Jan 22 '16 at 03:39
  • what if we don't have the backup of file? – Narendra Jaggi Sep 26 '16 at 07:20
  • 20
    I don't have the backup file under .git folder. However, I successfully resolved the issue by directly removing the file "config" (renaming it to config.bak in case that it's still needed). – Zhaoxing Lu Nov 01 '17 at 08:45
  • 3
    @ZhaoxingLu-Microsoft also work for me I rename .gitconfig to something else. – Windo Mar 27 '19 at 11:50
  • I also renamed the file and it worked. I did not loose anything :-) – Zaheer Nov 12 '20 at 14:42
23

I know on some tutorials they ask you to put the below code, but what they actually mean is that they're the commands you type into the terminal: git config --global user.name "NewUser" git config --global user.email newuser@example.com

If you were modifying the file ( $ nano ~/.gitconfig ), you would put this into your file instead:

[user]
name = WilliamQLiu
email = WilliamQLiu@myemailaddress.com
Seriously
  • 884
  • 1
  • 11
  • 25
Will
  • 11,276
  • 9
  • 68
  • 76
17

My issue was related to my global config which Git resolved to $HOME/.gitconfig, however the underlying cause happened to be irrespective of the location.

> git config --global -l
fatal: bad config file line 1 in C:\Users\<user>/.gitconfig

I have a habit of symlink-ing the config files in my home directory to a nested Git repository for maintaining the files across my multiple development environments. It turns out that Cygwin's implementation of a symbolic link doesn't make much sense to Git running under Windows.

I changed the symbolic link to a hard link and somehow this works ok. I was able to verify this by opening the .gitconfig link within Windows; when using a symbolic link the file contained binary data however the hard-linked file contains the content as expected.

adgoudz
  • 441
  • 4
  • 7
13

Its better to execute the two commands

git config --global user.name "NewUser"
git config --global user.email newuser@example.com

in the terminal. This in turn will update the config file.

user3216139
  • 139
  • 1
  • 2
  • Can you explain why it is better? –  Jan 20 '14 at 17:53
  • Careful while executing commands above as you are changing the user.name and user.password for all git repositories. It will surely create .gitconfig at HOME directory(~) in MacOS though. – realPK Sep 01 '14 at 00:56
  • Once the above error occurred, if we run the above commands, there will be no success. But open the .gitconfig file, remove the existing lines and try running the above commands in the terminal will solve this issue. – maniempire Apr 20 '15 at 05:49
  • 6
    when I try this, I get the same error `$ git config --global user.name "JuanPablo" fatal: bad config line 1 in file C:/cygwin/home/pablo/.gitconfig ` – JuanPablo Nov 24 '16 at 13:41
  • This worked for me only once I had deleted the original gitconfig file from c:/Program Files/Git/etc/ – borizzzzz Jul 23 '20 at 08:59
10

I had the same problem (Notepad++ showed only NULL characters in the file).

I managed to solve it by creating a new repo (in a seperate folder) with git init and copying .git/config over from there.

Remotes were missing after that, but everything worked after readding them manually.

  • I had a similar problem but in my case there was a single line full of NULL characters at the bottom of the file. Removing that line using Notepad solved the issue. – Augusto Barreto Dec 12 '16 at 19:31
8

I too had same issue, managed to fix it by below steps.

  1. deleted .gitconfig file from, c:\users[username] (created backup incase not worked, but its not needed.)
  2. restarted source tree and File->Open->[your repository].
mehul9595
  • 1,925
  • 7
  • 32
  • 55
  • This is what I did and it worked. My `.gitconfig` file was empty so I wasn't concerned with deleting it (and I have no backups of it). – bcr Jul 07 '16 at 15:52
3

I get the same symptom. I'm using Cygwin on Windows, and when I launch a process "git clone" for instance the error message is "fatal: bad config line 1 in c:/cygwin/home/myhome/.getconfig"

The problem is obviously on the file name which is a mix between windows c: and cygwin/unix path. ;-) What is misleading is the "line 1" message while it should say "config file not found".

I don't know how this is produced as I do have neither %HOMEPATH% neither $HOME set

  • I have the same issue, but I cannot seem to find any way to solve it. The problem seems to be aggravated by the fact that I created my git repository on my D: partition, while the %HOMEPATH% is located on the C: partition, and git doesn't seem to like this... (not to mention the not-very-helpful error message...) – Sorin Postelnicu Apr 23 '13 at 21:20
  • And of course, as I suspected, after creating the repository on the C: drive, I've no longer got this error... – Sorin Postelnicu Apr 23 '13 at 21:28
3

I had the same problem right after initial installation.

"fatal: bad config line 1 in file C:\ProgramData/Git/config"

I opened notepad as administrator, navigated to the file listed in the error message. The file was also blank but it had spaces/tabs so I deleted them saved the file. And now it works

jordanrh
  • 35
  • 3
3

For me the problem was that I edited the file in Mac TextEdit app, which is set by default to transform "standard" double-quotes into “angled” double-quotes.

In .git/config, the correct double-quotes are: " (UNICODE U+0022 QUOTATION MARK)

In my case, I fixed the following line:

WRONG: [remote “origin”]

CORRECT: [remote "origin"]

PS: If you want that Mac TextEdit doesn't "smartly" change the double-quotes, go to "Preferences" and un-check "smart quotes".

ofri cofri
  • 886
  • 10
  • 13
1

What worked for me on Windows was just to copy the config files present in C:\ProgramData\Git\config from another machine. It's new content became what's shown below, which replaced the gibberish that was there before:

[core]
    symlinks = false
    autocrlf = true
    fscache = true
[color]
    diff = auto
    status = auto
    branch = auto
    interactive = true
[help]
    format = html
[rebase]
    autosquash = true
Hakim
  • 3,225
  • 5
  • 37
  • 75
1

I don't see a way to find the exact error, git only shows a line number, so I'm posting my troubleshooting steps in case it helps anyone.

I had this problem trying to add an alias to my ~/.gitconfig global file via a text editor (emacs). I discovered my problem by instead using this command to edit the config:

git config --global alias.<my_alias> 'my new command'

I was creating an alias to show just the branch that I am on:

$ git config --global alias.thisbranch '!thisbranch() { git branch | grep \* | cut -d " " -f2; }; thisbranch'

The problem was that the * character needed to have a \\ double escape in the ~/.gitconfig file since the \ escape character needed to be escaped (the command was now nested in a string), but I was using only the single escape character \.

So my git config looks like this now:

[alias]
        ...
        ...
        thisbranch = "!thisbranch() { git branch | grep \\* | cut -d \" \" -f2; }; thisbranch"
  • 1
    Thanks. I had a sequence of back-slashes in my command, so it was better to add alias with `git config`. – igor Jun 16 '22 at 09:14
0

I was getting this error in Windows 8.1 with GitHub for Windows client 2.14.*

None of the git repositories cloned in my local were accessible. Branches names, changed files, history, etc. were not visible.

Opening the Git Shell and trying to check repository status using git status was giving a error message fatal: bad config file ... at C:\Users\MyUser\AppData\Local\GitHub\PortableGit_xxxxx06exx6fdf878271f7fe636a147ff37326ad\etc\gitconfig

So I deleted gitconfig file at this path and closed Git shell and Git client. Starting Git client again and the repository is back to normal.

Vinay Vemula
  • 3,855
  • 1
  • 21
  • 24
0

I deleted .git/config at the root of my repository folder because that's the bad config file name that was given in the error message.

I then re-opened SourceTree (that's what I normally use) and everything except the remotes were there.

bcr
  • 1,983
  • 27
  • 30
0

For me, i opened the config file and deleted everything in it. I backed up the repository and re-cloned the repository. I then manually applied my changes from the backed-up repository.

I should be noted that remote tracking information will be lost when the config is cleared. You can re-add the remote host but proved tedious for me.

William
  • 648
  • 8
  • 8
0

The best they can do is copy all their files in another route except the folder .git then make clone of the last commit that managed to upload and paste the files that moved to the other route so that they are overwrite and allow the new commit.

0

For Cygwin it messed up my username in the .gitconfig file. My username was created as domain\username and apparently that backslash is escaping the 's'.

So, if after you type git status you get this:

fatal: bad config line 2 in file /cygdrive/c/Users/yourname/.gitconfig

Then do this:

  1. vi /cygdrive/c/Users/yourname/.gitconfig
  2. Edit the variable name to show a normal name (i.e. Donald Trump)
  3. Edit the variable email to show a valid email value (i.e. dtrump@whouse.gov)
  4. Edit the variable username to show a simple username with no backslash (i.e. dtrump)

So the suggestion above is useful... Right before you create your git repository (git init), set up these variables right away:

git config --global user.name "NewUser"
git config --global user.email newuser@example.com

Because the default values are most likely not correct.

Salvador Valencia
  • 1,330
  • 1
  • 17
  • 26
0

If your .gitconfig file seems to be fine (having correct data) you can copy and paste the same data again into it so that the file edit date is modified. This seemed to work fine for me. And just to avoid the issue in future take a backup of the same file and save as "backup.gitconfig_bak" or any name you like.

Akshunya
  • 153
  • 1
  • 2
  • 8
0

Search for the config file and delete all the whitespaces in it worked for me.

Patricia
  • 2,885
  • 2
  • 26
  • 32
0

On Jenkins, check the error message for the git-hash-string. Just delete the /var/jenkins/home/caches/git-hash-string git-hash-string@tmp. You may need root permissions to delete all files from the git-hash-string/.git folder.

When you restart the build, everything works fine again.

tm1701
  • 7,307
  • 17
  • 79
  • 168
0

I just had the same problem after I changed the email which was on the config file. I signed up for git using xxxx@gmail.com, then git generated an email for me like xxxxxx@users.noreply.github.com. The original email on the config file was xxxxxx@users.noreply.github.com. Changing it to the email I signed up with xxxx@gmail.com caused the problem. To solve it, I logged into my git account online, visited Settings and navigated to the Email section and located the GitHub generated email from this section that reads like:

Primary email address Because you have email privacy enabled, xxxxx@gmail.com will be used for account-related notifications as well as password resets. xxxx@users.noreply.github.com will be used for web-based Git operations (e.g. edits and merges).

From the console, I run sudo nano ~/.gitconfig Entered my computer password, then changed the email from xxxx@gmail.com back to the Github email xxxx@users.noreply.github.com. Then hit (ctr + O) to save, then hit enter, then hit (ctr + x) to close the terminal. To be sure it worked, I cloned the repo again using git clone https://github.com/xxxxx where xxxx is the repo and it worked. :)

kingnanaprempeh
  • 137
  • 1
  • 4
0

This can be happen if you manually edit .gitconfig file . in the C:\Users\.gitconfig (you can edit it using Notepad++ ) open it and check these lines.

 [user]
        name = <your username>
        email = <your email>
    [core] 
        editor = <editor location>

you can change those line. if you add wrong line to editor variable it can lead to an error. simply you can delete this line. but make sure to get back up of it. Or you can check right code/path to connect with your text editor and replace it with new working code/path.

Then save it. (this can be more helpful to android studio users)

HeshanHH
  • 653
  • 7
  • 9
0

I had the same issue for my Android project. So I just did following in Android Studio: File -> Invalidate Cache and Restart. It helped me.

Alektas
  • 556
  • 4
  • 11
0

I had similar issues with push and I could repair it using:

git config --global push.default matching
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
ojo
  • 1
  • 3
0

First of all, a file corruption may be a symptom of file system problems (it was in my case), so to prevent further data loss, boot into the rescue mode, and before mounting the partition run disk check utility on it (e.g. fsck -n or xfs_repair -n).

But if you are using Jenkins, then finding the corrupted .git/config file may not be easy. It is stored locally in at least two copies:

  • in the local copy of the remote repo cloned by Jenkins into <JENKINS_HOME>/<remote_repo_name> AND
  • cached in one of the many git-* folders in <JENKINS_HOME>/caches.
mirekphd
  • 4,799
  • 3
  • 38
  • 59
0

For my was remove file .gitconfig and it that resolve

perrukozsh
  • 34
  • 1
  • 6
0

i delete the [.git] from my directory and re-initialize again using [git init] and everything works for me

RaySun
  • 139
  • 1
  • 5
0

I was facing the same issue!

The simple solution is that: Delete your flutter SDK and download the latest version of flutter SDK.

You can download it by the following link: https://docs.flutter.dev/get-started/install

It happens when Flutter releases the new version of flutter SDK but you are running a lower version, so some libraries show you an error.

0

In my case I used a basic command to resolve the .config error:

rm -rf .git

then

git-init

and it works properly.

alamoot
  • 1,966
  • 7
  • 30
  • 50
0

first delete all contents of config file then save and exit then in a new terminal git config --global user.name "username" git config --global user.email "mail address" after doing these steps look inside the config file and you will see the problem is solved

0

I had the same error trying to do anything in my git repository, it was not even recognizing the repository, this was the output: "bad config line 1 in file .git/config". And my config file had blank spaces and tabs, i just deleted all and it worked just fine right after.

-3

I was also facing the same issue,so i went to the file path and c:\User\.gitconfig and deleted all contents inside it and saved it. Now it is working $ git --version git version 2.22.0.windows.1