config are files that store Git configurations, there are three files that are used to define configurations at three different levels,
- Local
- Global
- 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]
--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.
See list from the local config file:% 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 global 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 system config file:% git config --list --global --show-origin file:/root/.gitconfig global.message=hello-global
% git config --list --system --show-origin file:/etc/gitconfig system.message=hello-system
View & Set a values using git config:
View value example:% git config section.key value
% 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:
Example: Setting your default git editor:% git config --global user.email "you@example.com" % git config --global user.name "Your Name"
Example: Setting your default git merge tool:% git config --global core.editor "nano -w"
Example: Create alias:% git config --global merge.tool kdiff3
Set a boolean value:% git config --global alias.ci commit
Set an int value:% git config --bool env.isStage true
Add new values without altering any existing ones:% git config --int env.num 2
% 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
--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
--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:
Edit system config file:% git config --global --edit
% git config --system --edit
--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
- Change the default git branch name from master to main
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- [Solution] fatal: not a git repository (or any of the parent directories): .git
- [fix] zsh: command not found: git
- How to Install Git on Windows
- Setup Git + Visual Studio Code Tutorial
- How to undo last Git Commit on Local Repository?
- Install GitHub Command Line Tool on Mac
- How to Create a Git Branch in 6 Different ways
- Fix Git: Warning: could not find UI helper GitHub.UI on Windows
- Git Config Command - A Deep Dive
- Step-by-Step: How to delete a git branch from local as well as remote origin
- How to Add Git Bash Option to Windows Terminal List
- Command to Clone Repository Using Git Bash
- How to remove or unstage a file from git staged area
- Get the file location of git config values
- GitHub: How to Search Code
- How to check your installed version of Git
- Fix: error: src refspec master does not match any failed to push some refs to Git
- Git: Step-by-Step - How to Push Local Brach to GitHub
- How to Merge Branch into Master Branch
- fix fatal: --local can only be used inside a git repository error
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken
- Change the default diff or commit editor for git
- Git Fix: fatal: refusing to merge unrelated histories Error
- Spring Boot: Transactions Management with JDBCTemplate Example - Java
- 30+ Zoom video communications application shortcuts for macOS - MacOS
- Java JDK 21 LTS Version Release Date (General Availability) - Java-JDK-21
- Add Blank Lines Between Each Lines in Notepad++ - NotepadPlusPlus
- Share Multiple Images in WhatsApp using Android Intent - WhatsApp
- The default interactive shell is now zsh. [macOS] - MacOS
- How to Download Microsoft Excel on Mac - MacOS
- How to URLEncode a Query String in Python - Python