Git Pills: avoid committing with an unwanted user name and email

Do you use different users for different repos?
Have you ever committed your code to a repository using a wrong user name and email?

I did it many times. Here a way to stop it :)

I used to set up my default user.name and user.email in Git global configuration, like this:

For using different user info in different repos, as you may know, you can set up them locally by repository in this way, overriding global ones:

But I usually forget to do it, until I do a git log and I see my commit with wrong user and email… :|
Fortunately, there’s a way to avoid this.

First, delete global user config, this is pure evil.

Using Git in a shell, these are the commands for deleting user name and email:

The more drastic way is to completely delete the user section:

 

Alternatively, you can directly edit your user’s .gitconfig file (e.g C:\Users\<username>\.gitconfig file in Windows).
If you don’t know where configuration are stored, there’s a Git command to show them all:

You have to delete these two lines under the [user] section (you can delete even the [user] itself, as we have seen before):

 

Well, even doing this, while committing Git tries to auto-detect user and email from your system.

To avoid this, set this configuration variable:

Now, if you try to commit for the first time in a repo, Git will force you to set up a user name and email before:

Remember to omit –global, otherwise you will be in trouble again :)

 

Leave a Reply

Your email address will not be published. Required fields are marked *