Skip to main content

Git Configuration

To change git configurations we use the git config command.

https://git-scm.com/docs/git-config

It's necessary to configure some variables that git uses to track the user who made changes to the file and committed, remembering that variables for git are case sensitive. By default it's good to use the first letter capitalized in the name.

The configuration is associated with a level but your git configuration is the sum of all levels, with the repository (local) configuration taking precedence over the user (global) configuration, and this over the system (system) configuration.

For the git config command to know at which level it will configure the scope:

  • --system are system-level configurations, all users will have the same configuration (avoid)

    • on Linux it's in /etc/gitconfig and is only created when the first configuration is made
    • on Windows it's in C:\Documents\Git\etc\gitconfig
  • --global are user-level configurations

    • on Linux it's in ~/.gitconfig in the user's home root
    • on Windows it's in C:\Users<UserName>.gitconfig
  • --local are repository-level configurations, meaning the directory needs to have a .git and there will be a config inside this folder

git config --global user.name "Fulano Tall"

git config --global user.email "[email protected]"

git config --list --show-scope

Some other configurations can be nice, but not necessary.

                    # block name.config
git config --global core.editor vim
git config --global color.branch auto
git config --global color.diff auto
git config --global color.interactive auto
git config --global color.status auto

Everything can be configured through the cli or edited directly in the correct gitconfig file. The advantage of using the cli is to avoid errors, so be careful when editing the file.

To open the file directly and edit:

# being in the project directory
git config --edit
# for your user
git config --global --edit
# general
sudo git config --system --edit

Example of my gitconfig at my user level:

cat ~/.gitconfig
# block
[user]
# configs
name = David Puziol
email = [email protected]
[core]
editor = vim
[color]
branch = auto
diff = auto
interactive = auto
status = auto
[alias]
ec = git config --global -e
co = checkout
cob = checkout -b
c = commit
cm = commit -m
cma = commit -a --ammend
lg = log — all — graph — decorate — oneline — abbrev-commit
ac = !git add -A && git commit -m
df = diff
a = add
aa = add -A
tags = tags -l
branches = branch -a
up = !git pull --rebase --prune $@ && git submodule update --init --recursive
undo = reset HEAD~1 --mixed
wip = !git add -A && git commit -qm \"WIPE SAVEPOINT\" && git reset HEAD~1 --hard
pullall = "!git branch -r | grep -v \"\\->\" | while read remote; do git branch --track ${remote#origin/} ${remote}; done && git fetch --all && git pull --all"

Observe that core.name and core.email are in the same block [core]

Aliases are shortcuts we can create to call and replace part of the code line to make life easier, but only do this after you know the commands well.

For example in this repository we have a .git:

~/study-git/.git develop
cat config
# block name
[core]
# block config
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
editor = vim
[remote "origin"]
url = [email protected]:davidpuziol/study-git.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main

It's possible to configure a branch, in the example below main, as the default for pull (download) and push (upload):

git config --local init.defaultBranch main