Debug School

rakesh kumar
rakesh kumar

Posted on

How to display and change git configuration setting

The git config --list command is used to display the Git configuration settings for the current repository. It shows all the configuration options and their values that have been set at various levels of the Git configuration hierarchy. These configurations are used to customize the behavior of Git and can be set at three different levels:

Local Configuration: Configuration specific to the current Git repository. This level of configuration is stored in the .git/config file within the repository directory. Settings at this level override the global and system configurations for that particular repository.

Global Configuration: Configuration that applies to all Git repositories on the current user account. This level of configuration is stored in the ~/.gitconfig file in the user's home directory. Settings at this level override the system-level configurations.

System Configuration: Configuration that applies to all users and repositories on the system. This level of configuration is stored in the /etc/gitconfig file or a file specified by the GIT_CONFIG environment variable. Settings at this level are overridden by both the local and global configurations.

Using git config --list is helpful in several scenarios:

Checking Current Configuration: It allows you to quickly view the current configuration settings for the repository, which helps in understanding how Git behaves in that particular context.

Troubleshooting: When experiencing issues or unexpected behavior in Git, git config --list can be used to inspect the configuration and identify any potential misconfigurations or conflicts.

Changing Configuration: By using git config with appropriate options, you can modify the Git configuration at the local or global level.

Here's an example output of

git config --list
Enter fullscreen mode Exit fullscreen mode
user.name=John Doe
user.email=john@example.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/example/repo.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
Enter fullscreen mode Exit fullscreen mode

Image description

As you can see, the output shows various configurations like user.name, user.email, and some core and remote settings that define the behavior of the Git repository.

To change the user.name and user.email configuration settings in Git, you can use the git config command with the --global option to update the global configuration or without the --global option to update the local configuration within a specific repository. Here's how you can do it:

Change user.name globally:
If you want to change the user.name globally, i.e., for all your Git repositories on your user account, use the following command:

git config --global user.name "New Name"
Enter fullscreen mode Exit fullscreen mode

Replace "New Name" with the desired name you want to use as the new user name.

Change user.email globally:
To change the user.email globally, use the following command:

git config --global user.email "newemail@example.com"
Enter fullscreen mode Exit fullscreen mode

Replace "newemail@example.com" with the new email address you want to use.

Change user.name and user.email locally (per repository):
If you want to change the user.name and user.email for a specific Git repository only, navigate to that repository's directory in the terminal and execute the following commands:

git config user.name "New Name"
git config user.email "newemail@example.com"
Enter fullscreen mode Exit fullscreen mode

Again, replace "New Name" with the desired name and "newemail@example.com" with the new email address you want to use for that repository.

here is a list of some common git config commands with examples:

Set user.name globally:

git config --global user.name "John Doe"
Enter fullscreen mode Exit fullscreen mode

Set user.email globally:

git config --global user.email "john@example.com"
Enter fullscreen mode Exit fullscreen mode

Set core.editor to change the default text editor for commit messages:

git config --global core.editor "vim"
Enter fullscreen mode Exit fullscreen mode

Set color.ui to enable colored output in Git:

git config --global color.ui true
Enter fullscreen mode Exit fullscreen mode

Set alias to create a Git shortcut:

git config --global alias.st status
Enter fullscreen mode Exit fullscreen mode

With this, you can use git st instead of git status.

Set push.default to define the default push behavior:

git config --global push.default simple
Enter fullscreen mode Exit fullscreen mode

Set diff.tool to define an external diff tool:

git config --global diff.tool vimdiff
Enter fullscreen mode Exit fullscreen mode

Set merge.tool to define an external merge tool:

git config --global merge.tool kdiff3
Enter fullscreen mode Exit fullscreen mode

Set core.ignorecase to control case sensitivity in file names:

git config --global core.ignorecase true
Enter fullscreen mode Exit fullscreen mode

Set credential.helper to store Git credentials:

git config --global credential.helper cache
Enter fullscreen mode Exit fullscreen mode

List all configurations in the local repository:

git config --list
Enter fullscreen mode Exit fullscreen mode

List all configurations in the global configuration file:

git config --global --list
Enter fullscreen mode Exit fullscreen mode

List all configurations in the system configuration file:

git config --system --list
Enter fullscreen mode Exit fullscreen mode

Unset a specific configuration:

git config --unset user.email
Enter fullscreen mode Exit fullscreen mode

Unset a specific configuration globally:

git config --global --unset user.email
Enter fullscreen mode Exit fullscreen mode

Remember that using the --global flag will modify the global configuration file (usually located at ~/.gitconfig), while omitting the flag will modify the local configuration file (.git/config in the current repository). Use these commands to customize your Git environment according to your preferences and workflows.

Top comments (0)