Git Config Commandd

Learn via video courses
Topics Covered

Overview

Git is a version control system that tracks the changes in the code. To configure Git, we use the git config command which is one of the most powerful and basic commands used in Git Bash. We can use the git config command for configuring our username, email address, etc. The git config command is also used to configure the git's configuration file so that we can customize how the git will work for us. We can use the --global flag and --local flags with the git config command to apply the changes globally and to the local repository respectively.

Pre-requisites

The prerequisites for learning the git config command can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git rebase command.

Version Control Systems

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. There are two types of Version Control systems namely - Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS).

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash on the other hand the GUI version of Git is known as Git GUI. Git tracks the changes in a project and saves a certain state that is known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a certain commit if we want.

Branching

A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.

So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

GitHub

GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects. GitHub tracks the changes made in the project workflow and we can also revert ba a certain state of the project (as GitHub saves the history of the project). GitHub supports open-source development where several developers can collaborate and support each other in the development process.

Anyone can create an account on the GitHub platform to host their code, files, and documents. GitHub sells hosted private code repositories, and other collaborative business model plans to make money as an organization.

What is git config?

One of the most basic and primary steps of using any application is to configure it. Now to start the development process with Git, we can do so in 3 steps:

  1. Installing the Git application on our local system.
  2. Configuring git with some important and necessary git settings using the git config command.
  3. Initializing a repository and start working on it.

So, to configure Git, we use the git config command. The git config command is one of the most powerful and basic commands used in Git Bash. We can use the git config command for configuring our username and email address. So for configuration, we can type the following command:

AND

In the above command, we need to specify our user name and our email address along with the git config command so that both can be used in our future git commits (a commit is a snapshot of the current version of the file).

Note: The above command, --global is a flag.

The git config command is not only used to specify our user name and our email address but it can be used as a command to configure the configuration file so that we can customize how the git will work for us. Now if we try to run just the git config command without any flag like --global, the Git will show us all the usage of git config along with that it will show us the various command line options.

GIT CONFIG

One of the important things to keep in mind regarding the git config command is that this command can be used to apply the configurations either locally to a specific Git repository or it can be used to configure the settings globally.

We can use the --global flag to apply the changes or the settings globally similarly we can use the --local flag flag to apply the changes to the local repository. Not only these, but we can also even set the configuration to a specific file using the flag -f filename.

:::

Where is the Git config file stored?

As we have discussed above, the git config command is used to apply the configurations either locally to a specific Git repository or it can be used to configure the settings globally.

So to open the global configuration file of the current operating system is found in the home directory of the user folder. So the name of the folder is: ~/.gitconfig.

  • In Mac operating system (macOS) or the LINUX operating system, the path of the folder is: ~/.gitconfig.
  • In the Windows operating system, the path of the folder is: C:\Users\username.gitconfig.

How Do You Open the Default Git Config File?

We can open the default config file using an editor in the Git bash itself. We can use the -e flag that stands for editor and the --global flag with the git config command to open the global configuration file.

The overall command for the same is:

Output:

DEFAULT GIT CONFIG

As we have previously discussed, we can also use the --local flag with the -e flag to open the local configuration file. But before using the --local flag, we must be sure that we are inside a git repository else our git will throw an error saying: fatal: --local can only be used inside a git repository.

We can also open the git configuration file using our favorite text editor. For opening the configuration file using the text editor, we can use a command prompt or windows power shell in the windows operating system and write the necessary command. In Linux or the macOS, we can use vim for the same.

Command to open the local git config file is:

Command to open the global git config file is:

The Git Config Levels and Files

In git world, we have three configuration levels and file levels. Let us learn about them and their use cases.

1. --global

When we talk about the global-level execution of commands and settings, we mean the configuration related to the operating system user on which we have installed the Git application. To open the global level configuration file we can open the home directory of the operating system user and then do the changes accordingly. Let us now look at the commands for the same.

a. Windows:

b. MacOS and LINUX Distributions:

2. --local

When we talk about the local-level execution of commands and settings, we mean the configuration related to the current working git repository of the Git application. To open the local level configuration file we can open the .git directory which contains the local configuration details and then do the changes accordingly. Let us now look at the commands for the same.

a. Windows:

b. MacOS and LINUX Distributions:

3. --system

When we talk about the system-level execution of commands and settings, we mean the configuration related to all the working git repositories of the Git application as well as the configuration related to the operating system user on which we have installed the Git application.

To open the system-level configuration file we can go to the system root path which contains the system configuration details and then do the changes accordingly. Let us now look at the commands for the same.

a. Windows:

b. MacOS and LINUX Distributions:

How to Write a Value

An example of writing a value to the configuration setting can be adding our email id to the Git Bash application using the git config --global user.email "our-email-id" command.

Refer to the image for more clarity.

VALUE

Change Settings and Values in Git Config

Let us now learn how we can change some of the most famous and widely used settings of Git using the git config command along with some flags.

1. Git pruning during fetch. Pruning is a method that is used to clean up the deleted, outdated remote references in our .git repository. To do so we can use the git fetch --prune command or what we can do is we can set this setting in our global or local config file to perform pruning every time a git fetch command is executed.

The overall command for the same is:
```git
git config --global fetch.prune true
```

In the above command, we are setting the ``fetch.prune`` setting ``true`` so that Git will start pruning every time during the ``git fetch`` operation.

2. Set a default branch. As we know that whenever we initialize an empty git repository, the default name of the working branch is the master branch. Now we can change the name of the master branch to something of our choice. Now, to do so, we can set the new desired value of the defaultBranch in the git config file.

So, open the git config file and add the following value (in this code snippet, we are setting the default branch name as scalerTopics)
```git
[init]
    defaultBranch = scalerTopics
```

3. Show short status. As we know, whenever we use the git status status command, we are shown a large view status of the git repository that looks like:

Now, this is pretty big and if we want short details about the tracked files, untracked files, etc., we can add the following value in the git config file:

4. Setting aliases in git. We can even set the aliases for various commands in the git config file for the global, local, as well as for system users. For example, if we want to add an alias for an empty added commit. We can add the following value in the git config file:

Or, we can write the following command:

We can even write the command:

We can do much more with the git config file, we can always refer to the git official documentation for the details.

The Git Config Usage

So far we have discussed some of the use cases of the git config command, let us now list down all the usage of the git config command.

  • We can set the username and email for the git application globally, locally, and system-wise using the git config command.
  • We can update the username and email as well using the git config command.
  • We can enable, disable, as well as change the editor for the git using the same git config command along with the -e flag.
  • We can delete the previously added configuration using this command along with the --unset flag.
  • We can also remove a whole section like the user section using the --remove flag with the git config command.
  • We can rename a section using the --rename flag with the git config command.
  • We can set alias names for numerous git commands using the git config command.
  • We can change values as well as default settings (add new settings as well) using the git config command.

Using Git Config With GitHub

We can even use the git config command with GitHub. As we know we can work with a remote repository and can push or push data from GitHub. A repository that is hosted or stored on the remote server is known as a git remote repository. As we have compared a git repository with a file system similarly we can compare a git remote repository with the remote file system.

We have a remote server GitHub where we can host our local git repository. This will allow fellow developers to view our work. The other developers can also contribute to our projects as our git remote repository (public) is visible to them.

So for pulling, pushing, cloning, fetching, etc. we have commands on git as well which we can use to add and perform things manually.

Global Git Configuration Tips

Let us now look at some of the things (or we can say tips) that we should keep in mind in case of global git configuration.

  • Whenever we make commits in the git, the default editor provided by git is vi. This vi editor can be difficult to use for beginners so we should change our preferred editor for convenience.
  • As we have discussed that pruning can be a handy thing so we can add a global value so that every time we perform the git fetch operation, pruning can happen in the background.
  • We can add a short status value to the git config file so that the status shown to us can be short and crisp.
  • We should also set aliases for the most frequently used commands for our convenience.
  • Apart from these, we can also change settings and values according to personal preference so that our working and development experience can be enhanced and productivity can be increased.

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc.
  • To configure Git, we use the git config command. The git config command is one of the most powerful and basic commands used in Git Bash.
  • We can use the git config command for configuring our username and email address.
  • The git config command is also used to configure the git's configuration file so that we can customize how the git will work for us.
  • We can use the --global flag to apply the changes or the settings globally similarly we can use the --local flag flag to apply the changes to the local repository.
  • In Mac operating system (macOS) or the LINUX operating system, the path of the folder is: ~/.gitconfig and in the Windows operating system, the path of the folder is: C:\Users\username.gitconfig.
  • When we use the --global flag with the git config command, we mean the configuration related to the operating system user on which we have installed the Git application.
  • When we use the --global flag with the git config command, we mean the configuration related to the current working git repository of the Git application.
  • When we use the --global flag with the git config command, we mean the configuration related to all the working git repositories of the Git application as well as the user of the operating system.
  • We can enable, disable, as well as change the editor for the git using the same git config command along with the -e flag.
  • We can rename, remove, and delete any section from the git configuration file using the --rename, --remove, and --unset flag with the git config command.