bash 无法配置 git,缺少 .gitconfig 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28143027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:15:37  来源:igfitidea点击:

Can't configure git, missing .gitconfig file

macosgitbash

提问by garythegoat

when I try to config Git I receive the following error:

当我尝试配置 Git 时,我收到以下错误:

$ git config --global user.name "John Smith"
error: could not lock config file /Users/John/.gitconfig: No such file or directory

My root directory is completely missing a .gitconfigfile. When I try to make a new one I receive this error:

我的根目录完全缺少一个.gitconfig文件。当我尝试制作一个新的时,我收到此错误:

$ touch ~/.gitconfig
touch: /Users/John/.gitconfig: No such file or directory

My git's location:

我的 git 的位置:

$ which git
/usr/local/git/bin/git

I've tried reinstalling Git several times (running OSX Yosemite) and I'm still having the same issue. I also cannot locate any .gitconfig.lock files, as mentioned in some posts regarding this issue.

我试过多次重新安装 Git(运行 OSX Yosemite),但我仍然遇到同样的问题。我也找不到任何 .gitconfig.lock 文件,如有关此问题的一些帖子中所述。

回答by garythegoat

So I ended up solving this issue by reading the Customizing Gitsection of the documentation.

所以我最终通过阅读文档的自定义 Git部分解决了这个问题。



While I could not toucha new .gitconfigfile in my root directory as stated above, I read that the first place Git looks for configuration values is in etc/gitconfig(note the lack of a .in the name).

虽然如上所述我无法在我的根目录中touch创建新.gitconfig文件,但我读到 Git 查找配置值的第一个位置etc/gitconfig(注意.名称中缺少 a )。

So I created a file called gitconfiginside of the etcdirectory and filled it in with a sample gitconfigI found online, and added my userinformation to the top like so:

所以我创建了一个名为gitconfiginsideetc目录的文件,并用gitconfig我在网上找到的示例填充它,并将我的user信息添加到顶部,如下所示:

[user]
  name = John Smith
  email = [email protected]

I was then able to commit as my usual self, however I still don't have a .gitconfigin my root, so I am unable to config Git using git config --global, and must do the configurations manually in etc/gitconfig.

然后我可以像往常一样提交,但是.gitconfig我的根目录中仍然没有,所以我无法使用 配置 Git git config --global,并且必须在etc/gitconfig.

回答by M.Saha

For me the .gitconfig file was missing.I tried reinstalling Git several times, however it din't help me. I resolved the issue by performing the below steps.

对我来说,.gitconfig 文件丢失了。我尝试重新安装 Git 几次,但它对我没有帮助。我通过执行以下步骤解决了这个问题。

  1. create a folder structure c:/dev/home if already not present.
  2. Open the Windows command prompt. (Run as administrator)
  3. From the Windows command prompt, run the command "git init"
  4. Run the following command: git config --global user.name "Example"
  5. Run the following command: git config --global user.email "[email protected]"
  1. 如果尚未存在,则创建一个文件夹结构 c:/dev/home。
  2. 打开 Windows 命令提示符。(以管理员身份运行)
  3. 在 Windows 命令提示符下,运行命令“git init”
  4. 运行以下命令: git config --global user.name "Example"
  5. 运行以下命令: git config --global user.email "[email protected]"

The above steps will create the .gitconfig file in the path c:/dev/home and you will be able to set your desired username and email id.

上述步骤将在路径 c:/dev/home 中创建 .gitconfig 文件,您将能够设置所需的用户名和电子邮件 ID。

On Windows systems, Git looks for the .gitconfig file in the $HOME directory (C:\Users\$USER for most people). So copy the file created in the above step to your Home directory by providing the required permission.

在 Windows 系统上,Git 在 $HOME 目录(对于大多数人为 C:\Users\$USER)中查找 .gitconfig 文件。因此,通过提供所需的权限,将在上述步骤中创建的文件复制到您的主目录。