Table of Contents

4 min

Table of contents

  1. Introduction
  2. Global configuration
  3. Local configuration
  4. Conclusion

Introduction

Git configuration lets you define global or local settings for a Git project. When you create a project and save it in a local repository, it is essential to specify the author's name and email address. This information can be stored in a configuration file that can be global or local.

⬆️ Back to table of contents


1 - Global configuration

The global configuration file is shared by all Git projects on the same local system. If no local configuration is defined, Git uses the global configuration. A .gitconfig file is created in the user's folder.

Example: Create a user name and email address in the global configuration file

1.1- Find the user's directory

Run the following command to locate the user's directory:

bash
echo $HOME

1.2- Create a global configuration file and set an author

Use the following commands to create a global configuration file (the .gitconfig file in the user's directory) and set a name and email address for the author:

bash
git config --global user.name "Haythem"
git config --global user.email "rehoumahaythem@gmail.com"

1.3- Checking the configuration file

After running these commands, you can check the contents of the .gitconfig file with the following command:

bash
cat ~/.gitconfig

The contents of the .gitconfig file will look like this:

[user]
    name = Haythem
    email = rehoumahaythem@gmail.com

⬆️ Back to table of contents


2 - Case study 1: Global configuration

Context:

Haythem is a software developer and works on a project called MyProj on his laptop. He uses Git to manage his project repository. Haythem wants to set his name and email address in the global configuration so that his contributions are recorded correctly.

Solution:

  1. Initialize the Git repository for the MyProj project:

    bash
    cd MyProj
    git init
  2. Set the author's name and email at the global level:

    bash
    git config --global user.name "Haythem"
    git config --global user.email "haythem@gmail.com"
  3. Create a project file (for example, myfile_1.txt):

    bash
    touch myfile_1.txt
  4. Check the Git status of the file (it will be marked as untracked):

    bash
    git status
  5. Add and commit the changes:

    bash
    git add myfile_1.txt
    git commit -m "Add the myfile_1.txt file"
  6. Check the commit history:

    bash
    git log

    The history will show the author as "Haythem" and the associated email address.

⬆️ Back to table of contents


3 - Local configuration

Local configuration is specific to a particular Git project. If several developers work on the same machine, each project can have its own name and email settings. Local configuration is stored in the project's .git/config file.

Example: Create a user name and email address in the local configuration

Step 1: Initialize a Git project

bash
cd LocalConfigProj
git init

Step 2: Set the author's name and email at the local level

Use the following commands to set the author name and email address specific to this project:

bash
git config --local user.name "Haythem Local"
git config --local user.email "localhaythem@gmail.com"

Step 3: Check the local configuration file

The project's .git/config file will contain:

[user]
    name = Haythem Local
    email = localhaythem@gmail.com

⬆️ Back to table of contents


4 - Case study 2: Local configuration

Context:

Haythem now has to work on a development server where several developers collaborate. He does not want to use the global configuration, but prefers to set a name and email address specific to the project so that his contributions are recorded correctly.

Solution:

  1. Initialize the Git repository for the LocalConfigProj project:

    bash
    cd LocalConfigProj
    git init
  2. Set the author's name and email at the local level:

    bash
    git config --local user.name "Haythem_dev"
    git config --local user.email "dev_haythem@company.com"
  3. Create a project file (for example, myfile_1.txt):

    bash
    touch myfile_1.txt
  4. Check the Git status of the file:

    bash
    git status
  5. Add and commit the changes:

    bash
    git add myfile_1.txt
    git commit -m "Add the myfile_1.txt file"
  6. Check the commit history:

    bash
    git log

    The history will show the author as "Haythem_dev" with the corresponding email.

⬆️ Back to table of contents


5 - Conclusion

Git configuration is an essential step to ensure that each commit is correctly attributed to the author with the appropriate information. Local settings always override global ones for the same key. You can configure Git at the global or local level, depending on the development context. Global configuration is useful for personal projects or on a dedicated machine, while local configuration is practical when several developers share the same environment. This lesson showed you how to configure Git at both the global and local levels, with case studies to illustrate the different usage scenarios.

⬆️ Back to table of contents