Git uses the tool git config to determine and change non-default behaviour. git config sets configuration variables to control all aspects of how Git looks and operates. Three configuration files in different places store configuration variables on different levels. Git checks these files one after the other to find these variables.
-
The first file git checks:
/etc/gitconfig
This contains settings that are applied to every user on the system + all their repositories
system-wide -
next file Git checks:
~/.gitconfigor~/.config/git/config
is specific to each user
affects all repositories the user works with on the system
global -
last file git checks: configuration file in the Git directory of currently used repo:
.git/config
values are specific to that single repository
This is default if not specified by user
local -
Each level (system, global, local) overwrites values of previous level
-
The user can pass options to git config (--system, --global, --local), so git reads and writes to each file specifically.
-
In general it makes sense to pass most variables globally, and use local level for project-specific options.
-
As first step, set user name and email.
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com -
Specify default editor of choice (e.g. for commit messages) with
core.editor
By default, Git uses the editor set by shell environment variables. -
Create your own template for commit-messages using
commit.template -
Specify the tool to output pages given with commands as log and diff, using
core.pager
lessby default -
Save your signing key for annotated tags using
user.signingkey -
Change the default branch name to e.g. main with
init.defaultBranch main -
For Git to not only point out but also correct a typo, use
help.autocorrect -
color.uilets the user choose whether they want colored output or not
set this either to false or true (default) -
custom merge resolution and diff tools
merge.toolto tell Git what strategy to use,
mergetool.<tool>.cmdto specify how to run the command,
mergetool.<tool>.trustExitCodeto tell Git if the exit code of that program indicates a successful merge resolution or not, and
diff.externalto tell Git what command to run for diffs.
These four commmands can be skipped by adding a few lines to to~/.gitconfig -
Formatting and whitespaces are often an isue when sharing code, especially with different operating systems
Git has configuration options to help with this.
- check all current settings
- Pass
--listand--show-originto git config to find all settings and their origins
-
There are less configuration options available for the server side than user side.
-
Making sure that every object received during a push still matches its SHA-1 checksum and points to valid objects is not done by default
Setreceive.fsckObjects trueto force Git to check this
Is computationally expensive though! -
force git to refuse force pushes:
The user can force push with a -f flag if git denies their push
Setreceive.denyNonFastForwards trueto prevent this
Can also be done via server-side receive hooks -
Possible workaround the denyNonFastForwards policy:
deleting the branch and pushing with a new reference
To prevent users from doing this, setreceive.denyDeletes trueto avoid this
Deletion of branches or Tags is then denied for every user
-
It makes sense to omit version control for non-essential files:
maybe they can be produced from other files (intermediate and result files) that are being version controlled.
Some editors produce duplicates of files, which do not make sense to commit. -
With the git config commmand
core.excludesfilepatterns to be ignored can be specified
Files matching patterns will not be shown as untracked or suggested for staging -
Usage:
create a .gitignore_global file, which can be filled with patterns to ignore or
set up .gitignore_global to ignore all files, and include only particular file types
passcore.excludesfiles /path/to/file/.gitignore_globalto git config --global
- Aliases in Git are custom shortcuts that define commands that expand to longer or combined commands
- Git provides its own alias system through the use of the git config command and the Git configuration files
- Git aliases are stored in Git configuration files in [alias] sections
- use the git config command to configure aliases
- Reasons for using Aliases in Git:
- Aliases enable more efficient workflows
- practical for frequently used commands
- Creating the aliases will not modify the source commands
- There is no direct git alias command
- Creation of Git Aliases
- direct editing of Git config files or usage of git config tool
- possible popular aliases:
alias.co checkoutalias.br branchalias.ci commitalias.st status- reference other aliases within alias
alias.amend ci --amend
- common Git pattern is to remove recently added files from the staging area
alias.unstage 'reset HEAD --'
https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup
https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-config