1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

.gitを消してもuser.emailとuser.nameは残るので注意

Last updated at Posted at 2018-08-22

組み込み系のLinuxを使っている人は注意してください。

# .git/ディレクトリを削除  
$ rm -r .git

# git configすると、user.emailとuser.nameは残っている
$ git config -l
core.editor=nano
user.email=tanaka@abc.co.com
user.name=tanaka

# この状態では、設定変更もできません
$ git config --global user.email "dammy@example.com"
error: could not lock config file .git/config: そのようなファイルやディレクトリはありません

# なので、一旦git initします
$ git init
Initialized empty Git repository in /.git/

# 変更できるようになるので適当な設定値で上書きします
$ git config --global user.email "dammy@example.com"
$ git config --global user.name "dammy"

# .git/ディレクトリはもう必要ないので消します
$ rm -r .git

# 上書きできてるか確認します
$ git config -l
core.editor=nano
user.email=dammy@example.com
user.name=dammy

追記

コメントで指摘がありましたので、記事を追記しました。コメントありがとうございます。

コメントでご指摘いただいたとおり、--globalでユーザー毎の設定をした場合は、"$HOME/.gitconfig"に設定値が書かれていますので、それを編集する方法もあります。
たとえば以下のような状況を考えます。

# リポジトリの初期設定
$ git init

# --localの設定
$ git config --local user.email localman@co.com
$ git config --local user.name localman

# --globalの設定
$ git config --global user.email globalman@co.com
$ git config --global user.name globalman

# --systemの設定
$ git config --system user.email systemman@co.com
$ git config --system user.name systemman

この状態で、それぞれの設定を削除する方法を考えます。

--localの設定

--localの設定はリポジトリ毎に管理され、.git/configに保存されます。つまり.gitディレクトリを消したら--localの設定は削除できます。

# .gitディレクトリを削除する
$ rm -r .git

# 設定の確認(localの設定は消える)
$ git config -l
user.email=systemman@co.jp
user.name=systemman
core.editor=nano
user.email=globalman@example.com
user.name=globalman

--globalの設定

--globalの設定はユーザ毎の設定となります。なので.gitディレクトリを消しても--globalの設定は削除できません。

--globalの設定は"$HOME/.gitconfig"に保存されていますので、それを書き換えるか削除します。

$ vi $HOME/.gitconfig
[core]
        editor = nano
[user]
        email = globalman@example.com
        name = globalman

--systemの設定

--systemの設定はシステム(OS)毎の設定となります。なので.gitディレクトリを消しても--globalの設定は削除できません。

--systemの設定ファイルの場所は、OSによって異なるようです。
Linux系などでは(私のRaspbian環境では)
・/etc/gitconfig
にあります。

一応Windowsについても言及すると、
・$HOME.gitconfig(大抵のユーザの$HOMEはC:\Users$USER)
Git for Windows v2.x 以降を使っている場合は、
・C:\Documents and Settings\All Users\Application Data\Git\config(Windows XP)
・C:\ProgramData\Git\config(Windows Vista以降)
にあります。

上記の設定ファイルを書き換えるか削除します。当然書き込み権限が必要になります。

$ vi /etc/gitconfig
[user]
        email = systemman@co.jp
        name = systemman

参考URL

Git for Windows の設定ファイルの場所 1.6 Getting Started - First-Time Git Setup
1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?