Git Config Command - A Deep Dive


Git-Config-Deep-Dive.

config are files that store Git configurations, there are three files that are used to define configurations at three different levels,

  1. Local
  2. Global
  3. System


1. --local git config file:

📁 local file location: Inside the root directory of your repository.

Example: /<repo-name>/.git/config

Let's consider myrepo is the repo I created locally using the git init command,

# cd myrepo
# ls -la
total 68
drwxr-xr-x 4 root root 4096 Aug 25 02:00 .
drwxr-xr-x 1 root root 4096 Aug 24 00:42 ..
drwxr-xr-x 8 root root 4096 Aug 24 10:33 .git
-rw-r--r-- 1 root root   14 Aug 20 14:12 .gitignore
....
-rw-r--r-- 1 root root 1058 Aug 25 02:00 dynamo-db.txt
-rw-r--r-- 1 root root 4652 Aug 21 04:50 git-notes.txt
-rw-r--r-- 1 root root 6324 Aug 20 13:52 s3-notes.txt

# cd .git

#  ls -l
total 48
-rw-r--r--  1 root root   18 Aug 24 10:33 COMMIT_EDITMSG
-rw-r--r--  1 root root    0 Aug 21 03:44 FETCH_HEAD
-rw-r--r--  1 root root   21 Aug 17 03:56 HEAD
-rw-r--r--  1 root root   41 Aug 19 07:41 ORIG_HEAD
drwxr-xr-x  2 root root 4096 Aug 17 03:56 branches
-rw-r--r--  1 root root  320 Aug 21 03:48 config
-rw-r--r--  1 root root   73 Aug 17 03:56 description
drwxr-xr-x  2 root root 4096 Aug 17 03:56 hooks
-rw-r--r--  1 root root  916 Aug 24 10:33 index
drwxr-xr-x  2 root root 4096 Aug 17 03:56 info
drwxr-xr-x  3 root root 4096 Aug 17 03:58 logs
drwxr-xr-x 68 root root 4096 Aug 24 10:33 objects
drwxr-xr-x  5 root root 4096 Aug 17 03:59 refs

# cat config
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = https://..../mynotes.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
	remote = origin
	merge = refs/heads/main

If you do not provide any options along with git config command, the action will be performed against the local config file.



2. --global git config file:

📁 global file location: Inside the root directory of your user.

on macOS/Unix/Linux/Ubuntu: ~/.gitconfig

on Windows: C:\<user-folder>\.gitconfig

As you may have guessed, the global config file is used to write user-specific configurations.



3. --system git config file:

📁 system file location: Inside the root directory of your system.

on macOS/Unix/Linux/Ubuntu: /etc/gitconfig

on Windows: C:\Documents and Settings\All Users\Application Data\Git\config

The configurations set at the system level are applicable to all users within the system and all the git repositories.



Git config commands and options

usage: git config [--options]
  1. --list option: git config --list

    The --list option will display all the configuration values from global and system (and local if you are running the command from within a local repo)

    % git config --list
    core.repositoryformatversion=0
    core.filemode=true
    core.bare=false
    core.logallrefupdates=true
    

    Add another option --show-origin to even display from where these properties are displayed, it could show that you may have set the same property in multiple levels with the same name.

    % git config --list --show-origin 
    file:/etc/gitconfig     system.message=hello-system
    file:/root/.gitconfig   global.message=hello-global
    file:.git/config        core.repositoryformatversion=0
    file:.git/config        core.filemode=true
    file:.git/config        core.bare=false
    file:.git/config        core.logallrefupdates=true
    file:.git/config        local.message=hello-local
    See list from the local config file:
    % git config --list --local --show-origin
    file:.git/config        core.repositoryformatversion=0
    file:.git/config        core.filemode=true
    file:.git/config        core.bare=false
    file:.git/config        core.logallrefupdates=true
    file:.git/config        local.message=hello-local
    See list from the global config file:
    % git config --list --global --show-origin
    file:/root/.gitconfig   global.message=hello-global
    See list from the system config file:
    % git config --list --system --show-origin
    file:/etc/gitconfig     system.message=hello-system
    


  2. View & Set a values using git config:

    % git config section.key value
    View value example:
    % git config env.name --local

    Note: If you do not pass any level details (global/local/system), the values will be set at the local level, provided you are within your repo.

    Below are the first commands that you would need to execute to set the email and user values before you can commit your first code.

    Set value example:
    % git config --global user.email "you@example.com"
    % git config --global user.name "Your Name"
    Example: Setting your default git editor:
    % git config --global core.editor "nano -w"
    Example: Setting your default git merge tool:
    % git config --global merge.tool kdiff3
    Example: Create alias:
    % git config --global alias.ci commit
    Set a boolean value:
    % git config --bool env.isStage true
    Set an int value:
    % git config --int env.num 2
    Add new values without altering any existing ones:
    % git config --add sample.name Chris
    % git config --add sample.name Mike
    % git config --add sample.name Sam
    % git config sample.name
    
    Chris
    Mike
    Sam


  3. --unset: Remove the set config values:

    % git config --unset env.name

    To remove a set value make use of the --unset option.

    --unset: will remove the first matching value of the key.

    --unset-all: will remove all matching values of the key.

    Example: Unset global value
    % git config --unset --global env.message


  4. --edit: Edit the config files in editor:

    In order to edit the text contents of the config files, you do not need to know the location of the files, you can simply use the --edit option along with the global, local and system options, it should open you set editor to edit the text,

    Edit local config file:
    % git config --local --edit
    
    Opens Nano for me:
    
    core]
            repositoryformatversion = 0
            filemode = true
            bare = false
            logallrefupdates = true
    [local]
            message = hello-local
    [section]
            key = value
    [a]
            isBool = true
    [env]
            no1 = 1
            no2 = 2

    Note: You must be within the repo to run this command.

    Edit global config file:
    % git config --global --edit
    Edit system config file:
    % git config --system --edit
Below are the list of all options that you can use along with git config.
--add                    -- add new value without altering any existing ones                               
--blob                   -- read config from given blob object                                             
--bool                   -- setting is a boolean                                                           
--bool-or-int     --int  -- setting is an integer                                                          
--default                -- with --get, use specified default value when entry is missing                  
--edit                   -- open config file for editing                                                   
--expiry-date            -- setting is an expiry date                                                      
--file                   -- use given config file                                                          
--get                    -- get the first matching value of the key                                        
--get-all                -- get all matching values of the key                                             
--get-color              -- find color setting                                                             
--get-colorbool          -- check if color should be used                                                  
--get-regexp             -- like "--get-all", but interpret "name" as a regular expression                 
--get-urlmatch           -- get value specific for the URL                                                 
--global                 -- use user-global config file                                                    
--includes               -- respect "include.*" directives in config files when looking up values          
--list                   -- list all variables set in config file                                          
--local                  -- use local config file                                                          
--name-only              -- show variable names only                                                       
--no-includes            -- don't respect "include.*" directives                                           
--null                   -- end values with NUL and newline between key and value                          
--path                   -- setting is a path                                                              
--remove-section         -- remove the given section                                                       
--rename-section         -- rename the given section                                                       
--replace-all            -- replace all values of the given key                                            
--show-origin            -- show origin of config                                                          
--system                 -- use system-wide config file                                                    
--type                   -- ensure that incoming and outgoing values are canonicalize-able as the given typ
--unset                  -- remove the first matching value of the key                                     
--unset-all              -- remove all matching values of the key                                          
--worktree               -- use per-worktree config file 
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.