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.
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.
Run the following command to locate the user's directory:
echo $HOMEUse 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:
git config --global user.name "Haythem"
git config --global user.email "rehoumahaythem@gmail.com"After running these commands, you can check the contents of the .gitconfig file with the following command:
cat ~/.gitconfigThe contents of the .gitconfig file will look like this:
[user]
name = Haythem
email = rehoumahaythem@gmail.comHaythem 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.
Initialize the Git repository for the MyProj project:
cd MyProj
git initSet the author's name and email at the global level:
git config --global user.name "Haythem"
git config --global user.email "haythem@gmail.com"Create a project file (for example, myfile_1.txt):
touch myfile_1.txtCheck the Git status of the file (it will be marked as untracked):
git statusAdd and commit the changes:
git add myfile_1.txt
git commit -m "Add the myfile_1.txt file"Check the commit history:
git logThe history will show the author as "Haythem" and the associated email address.
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.
cd LocalConfigProj
git initUse the following commands to set the author name and email address specific to this project:
git config --local user.name "Haythem Local"
git config --local user.email "localhaythem@gmail.com"The project's .git/config file will contain:
[user]
name = Haythem Local
email = localhaythem@gmail.comHaythem 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.
Initialize the Git repository for the LocalConfigProj project:
cd LocalConfigProj
git initSet the author's name and email at the local level:
git config --local user.name "Haythem_dev"
git config --local user.email "dev_haythem@company.com"Create a project file (for example, myfile_1.txt):
touch myfile_1.txtCheck the Git status of the file:
git statusAdd and commit the changes:
git add myfile_1.txt
git commit -m "Add the myfile_1.txt file"Check the commit history:
git logThe history will show the author as "Haythem_dev" with the corresponding email.
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.