Configuration Git : 3e partie, notre gabarit

Par Maxime Bréhin • Publié le 27 février 2025 • 4 min

Git “souffre” malheureusement d’un besoin de compatibilité ascendante entre chaque version. De ce fait, une partie des comportements intéressants, pour ne pas dire “optimaux”, sont seulement disponibles en renseignant les clés qui vont bien dans la configuration.

Pour compléter nos deux premiers articles (1 et 2) qui décrivent comment configurer Git, voici donc notre configuration type commentée. Libre à vous d’y piocher les éléments qui vous semblent utiles.

Gabarit de configuration

# Put this in your ~/.gitconfig or ~/.config/git/config
# Windows users: "~" is your profile's home directory, e.g. C:\Users\<YourName>
[user]
  name = Your Full Name
  email = your@email.tld
[color]
  # Enable colors in color-supporting terminals
  ui = auto
[alias]
  aliases = !git config --get-regexp alias | sed -re 's/alias\\.(\\S*)\\s(.*)$/\\1 = \\2/g'
  # Prepare stage for commit, then ask Git to fix an old commit passing its ref
  autofixup = !GIT_EDITOR=true && git commit --fixup $1 && git rebase -r -i --autosquash $1~1 && echo \"autofixup finished\"
  # Ask Git to reword an old commit passing its ref
  autoreword = !git commit --no-verify --fixup reword:$1 && GIT_EDITOR=true && git rebase -r -i --autosquash $1~1 && echo \"autoreword finished\"
  ci = commit
  # You might prefer "git switch" instead
  co = checkout
  # Auto-open global configuration file
  edit-global-conf = config --global --edit
  fixup = commit --fixup
  g = grep --extended-regexp --line-number --break --heading
  # Prevent editor opening while continuing rebase after a conflict
	keep-rebasing = !git add -A && GIT_EDITOR=true git rebase --continue
  # Print a nice graph of the project history
  lg = log --graph --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ar)%Creset'
	# Add staged files to last/current commit
  oops = commit --amend --no-edit
  # Ensure that force-pushing won't lose someone else's work (only mine).
  push-with-lease = push --force-with-lease --force-if-includes
  # Rebase won’t trigger hooks on each "replayed" commit.
  # This is an ugly hack that will replay each commit during rebase with the
  # standard `commit` command which will trigger hooks.
  rebase-with-hooks = rebase -x 'git reset --soft HEAD~1 && git commit -C HEAD@{1}'
  # Print current repo full path
  repo-path = rev-parse --show-toplevel
  # Check before pushing
  review-local = !git fetch && git lg @{push}..
  # Check before fetching
  review-remote = !git fetch && git lg ..@{u}
  # Change last commit message
	reword = commit --amend
  # List ignored files
  show-ignored = ls-files --others -i --exclude-standard
  # This is an example, check my personal directory ~/scripts/bin
  spull = !git pull && git submodule sync --recursive && git submodule update --init --recursive
  squash = commit --no-edit --squash
  st = status
  # Undo last commit but keep changed files in stage
  uncommit = reset --soft HEAD~1
  # Remove file(s) from Git but not from disk
  untrack = rm --cache --
  unstage = restore --staged
[core]
  # Don't paginate output by default
  # pager = cat
  # Global shared hooks directory
  # hooksPath = ~/.githooks
  #
  # Out of luck: on Windows w/o msysGit? You may have Notepad++…
  # editor = 'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin
  #
  # VSCode
  editor = code --wait
  # Don't consider trailing space change as a cause for merge conflicts
  whitespace = -trailing-space
[diff]
  # Use better, descriptive initials (c, i, w) instead of a/b.
  mnemonicPrefix = true
  # Show renames/moves as such
  renames = true
  # When using --word-diff, assume --word-diff-regex=.
  wordRegex = .
  # Display submodule-related information (commit listings)
  # submodule = log
  # Use VSCode as default diff tool when running `git diff-tool`
  tool = vscode
[difftool "vscode"]
  cmd = code --wait --diff $LOCAL $REMOTE
[fetch]
  # Auto-fetch submodule changes (sadly, won't auto-update)
  recurseSubmodules = on-demand
[grep]
  break = true
  heading = true
  lineNumber = true
  # Consider most regexes to be ERE
  extendedRegexp = true
[log]
  # Use abbrev SHAs whenever possible/relevant instead of full 40 chars
  abbrevCommit = true
  # Automatically --follow when given a single path
  follow = true
  # Disable decorate for reflog
  # (because there is no dedicated `reflog` section available)
  decorate = false
[merge]
  # Display common-ancestor blocks in conflict hunks
  # conflictStyle = diff3
  # Disable fast-forward merges as default merge strategy.
  # Force explicit call of `git merge --ff …`.
  ff = false
  # Custom/dedicated merge drive for npm lock files (`package-lock.json`).
  # Also works with Yarn.
  # Use Git attributes to set targeted files (located at `~/.config/git/attributes``
  # when installed with npm).
  # See https://www.npmjs.com/package/npm-merge-driver
  # [merge "npm-merge-driver"]
  # name = automatically merge npm lockfiles
  # driver = npx npm-merge-driver merge %A %O %B %P
[mergetool]
  # Clean up backup files created by merge tools on tool exit
  keepBackup = false
  # Clean up temp files created by merge tools on tool exit
  keepTemporaries = false
  # Put the temp files in a dedicated dir anyway
  writeToTemp = true
  # Auto-accept file prompts when launching merge tools
  prompt = false
[page]
  # Use custom pager to get an better log on terminal.
  # As this is an external tool it has to be installed.
  # See https://github.com/so-fancy/diff-so-fancy
  show = diff-so-fancy | less --tabs=4 -RFX
[pull]
  # This is GREAT… when you know what you're doing and are careful
  # not to pull --no-rebase over a local line containing a true merge.
  # rebase = true
  # This option, which does away with the one gotcha of
  # auto-rebasing on pulls, is only available from 1.8.5 onwards.
  # rebase = preserve
  # WARNING! This option, which is the latest variation, is only
  # available from 2.18 onwards.
  rebase = merges
[push]
  # Default push should only push the current branch to its push target, regardless of its remote name
  default = upstream
  # When pushing, also push tags whose commit-ishs are now reachable upstream
  followTags = true
[rebase]
  # Rebase advanced usage.
  # Automagically reorder and prefix your commands while doing an interactive
  # rebase. This has to be used with `--fixup` and/or `--squash` options for
  # `git commit`.
  # autoSquash = true
  # Automatically stash current WD and stage when running rebase
  # then you won't have to manually `git stash push` and `git stash pop`
  # after and before your rebase.
  autoStash = true
[rerere]
  # If, like me, you like rerere, uncomment these
  # autoupdate = true
  # enabled = true
[status]
  # Display submodule rev change summaries in status
  # submoduleSummary = true
  # Recursively traverse untracked directories to display all contents
  showUntrackedFiles = all
[color "branch"]
  # Blue on black is hard to read in git branch -vv: use cyan instead
  upstream = cyan
[tag]
  # Sort tags as version numbers whenever applicable, so 1.10.2 is AFTER 1.2.0.
  sort = version:refname
[versionsort]
  prereleaseSuffix = -pre
  prereleaseSuffix = .pre
  prereleaseSuffix = -beta
  prereleaseSuffix = .beta
  prereleaseSuffix = -rc
  prereleaseSuffix = .rc

Vous pouvez retrouver cette configuration et son historique sous forme de Gist.

Vous voulez aller plus loin et maîtriser pleinement les fondamentaux de Git ou être accompagné pour garantir la qualité de vos projets grâce à une bonne mise en place de Git ? On peut vous aider ou vous former, il suffit de nous décrire votre besoin !
Vous pouvez aussi regarder le programme de notre formation "Comprendre Git" ou nous poser vos questions sur notre forum discord.