Lesson 7 of 60 – Configuring Git
12%

Configuring Git

After installing Git, the next important step is to configure your identity and some basic Git settings. Git configuration allows you to define information such as your name and email address that Git can associate with your commits.

Note: Git configuration can be applied at different levels, such as global, local and system configuration.

1. What is Git Configuration?

Git configuration means setting options that control how Git behaves and how it identifies you when creating commits.

The main configuration command is:

git config

2. Why Configure Git?

Git uses configuration information for different operations. A common requirement is configuring your name and email address.

These values are commonly stored with commit metadata.

3. Configure User Name

You can configure the name that Git associates with your commits.

git config --global user.name "Your Name"

Replace Your Name with the name you want Git to use.

4. Configure User Email

You can configure an email address for your Git commits using:

git config --global user.email "you@example.com"

Use an email address appropriate for your development workflow.

5. The --global Option

The --global option applies the configuration to your user account on the computer.

git config --global user.name "Your Name"

This setting can be used by repositories for that user unless a more specific repository configuration overrides it.

6. Checking User Name

You can check the configured user name with:

git config user.name

If a value is configured, Git displays the current effective value for that setting.

7. Checking User Email

To check the configured email address, use:

git config user.email

8. Viewing All Git Configuration

You can view available Git configuration values using:

git config --list

This displays configuration settings that Git can access from the applicable configuration levels.

9. Global Configuration

Global configuration applies to repositories for your user account, unless a more specific configuration overrides a value.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

10. Local Repository Configuration

A repository can have its own local configuration. To set a value for only the current repository, omit --global.

git config user.name "Project Name"
git config user.email "project@example.com"

Local configuration takes precedence over the corresponding global configuration for that repository.

11. System Configuration

Git also supports system-level configuration. The --system option applies configuration for the system installation and may require administrator privileges.

git config --system

Most beginners will normally work with global and local configuration.

12. Configuration Levels

Git commonly works with three configuration levels:

  • System – applies to the system installation
  • Global – applies to the user's repositories
  • Local – applies to one repository

More specific configuration can override a value from a broader level.

13. Checking Configuration Sources

Git can show where configuration values are coming from. One useful command is:

git config --list --show-origin

This can help when you need to understand why a particular value is being used.

14. Changing Your Git Name

You can change the global Git user name at any time.

git config --global user.name "New Name"

The new value will be used for future commits where the global value applies.

15. Changing Your Git Email

You can also change the configured global email address.

git config --global user.email "new@example.com"

The changed configuration affects future commits.

16. Default Branch Name

Git allows you to configure the default initial branch name used when creating a new repository.

git config --global init.defaultBranch main

This means newly initialized repositories can start with main as their initial branch name.

17. Checking the Default Branch Setting

You can inspect the configured default branch setting using:

git config --global init.defaultBranch

If it has been configured, Git will display its value.

18. Git Configuration Editor

Git configuration can be opened in a text editor using:

git config --global --edit

This opens the global configuration file using Git's configured editor.

19. Removing a Configuration Value

A configuration value can be removed using the --unset option.

git config --global --unset user.name

After removing a value, Git may use another applicable configuration level if one exists.

20. Git Aliases

Git configuration can also be used to create command aliases. For example:

git config --global alias.st status

After this configuration, you can use:

git st

as an alias for git status.

21. Useful Git Alias Example

Another common example is creating a short alias for the log command.

git config --global alias.lg "log --oneline"

Then you can use:

git lg

22. Git Configuration File

Git stores configuration values in configuration files at different levels.

For example, global user configuration is commonly stored in the user's Git configuration file.

You normally do not need to edit these files manually because git config provides a safer command-line interface.

23. Git Configuration and GitHub

Git user name and email are used as commit metadata. They are separate from the authentication method you use to connect to GitHub.

Configuring your Git email does not by itself sign you into GitHub.

24. Git Configuration Example

git config --global user.name "Rahul Kumar"

git config --global user.email "rahul@example.com"

git config --global init.defaultBranch main

After setting these values, you can verify them:

git config --global --list

25. Repository-Specific Identity

Sometimes a developer may want different Git identity information for a particular repository.

Inside that repository, use local configuration:

git config user.name "Project Name"
git config user.email "project@example.com"

This local configuration can override the global value for that repository.

26. Checking the Effective Value

To check the effective value used for a setting, you can run:

git config user.name
git config user.email

These commands are useful when troubleshooting configuration issues.

27. Common Configuration Mistakes

  • Forgetting to configure user name
  • Forgetting to configure user email
  • Using the wrong configuration level
  • Accidentally overriding global settings locally
  • Entering an incorrect email address
  • Changing configuration without checking the current value

28. Recommended Basic Configuration

A beginner can start with a simple configuration:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

Then verify:

git config --global --list

29. Configuration Checklist

  • Install Git
  • Open a terminal
  • Set your Git user name
  • Set your Git email
  • Optionally set the default branch name
  • Check your configuration
  • Test Git inside a repository
git config --global user.name
git config --global user.email

30. Summary of Git Configuration

Git configuration allows you to customize Git and provide identity information for your commits. The most important beginner settings are your user name and email address.

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

In the next lesson, we will learn about Git Bash and basic terminal commands used while working with Git.

📌 Key Points

  • git config is used to configure Git.
  • user.name sets the name associated with commits.
  • user.email sets the email associated with commits.
  • --global applies a setting to the user's repositories by default.
  • Local configuration can be applied to a specific repository.
  • Git supports system, global and local configuration levels.
  • git config --list displays configuration values.
  • git config --global --edit opens the global configuration.
  • Git aliases can create shorter commands.
  • Git configuration is separate from GitHub authentication.

🧠 Quick Quiz

Question: Which Git command is used to configure the user name globally?