
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
- Fix [oh-my-zsh] Cant update: not a git repository
- [fix] fatal: pathspec index.html did not match any files error
- How to ignore files in git using .gitignore file
- Git Fix: fatal: refusing to merge unrelated histories Error
- Install GitHub Command Line Tool on Mac
- How to change directory in Git bash
- Get List of all local branches git command
- fix fatal: --local can only be used inside a git repository error
- [git] fatal: your current branch 'main' does not have any commits yet
- Git Revision Questions Before the Interview
- Change the default git branch name from master to main
- git command to remove/unstage files from staging area
- Remove git config at Local, Global or System Levels?
- [fix] fatal: this operation must be run in a work tree in git
- Clone a particular remote brach using git clone command
- How do I get a list of all branches in Git?
- How to check your installed version of Git
- Step-by-Step: How to delete a git branch from local as well as remote origin
- Git Commit - Author identity unknown, Please tell me who you are email
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier.
- How to Rename a Git Local Branch Using the --move Option
- Perform an Empty Commit in Git without anything in Staging Area
- [Fixed] Git Clone git@github.com: Permission denied (publickey) Could not read from remote repository Fatal error
- GitHub: How to Search Code
- Rename git branch on Local and GitHub Remove using Command
- How to Transpose a Square Matrix - Java Program - Java
- How to Run all Cells at Once Jupyter Notebook - Python
- How to verify if java is installed on the computer and get version detail - Java
- Queue Implementation in Java with Examples - Java
- How to copy Chrome alert popup text to clipboard - Chrome
- CSS: Apply opacity only for div background and not text - CSS
- Advanced ways to set Custom Settings for a Website on Safari for Mac - MacOS
- Resolve System.IO.PathTooLongException [Sharepoint C# .Net] - SharePoint