Git Configurations

I use Git almost daily but I need my memos for the uncommon operations. Therefore the posts:


When I’m using Git I need to first configure it. Well, strictly speaking, if I’m only going to clone some repository I don’t need to especially configure Git, but just about anything above that will lead me configuring Git. I’ll first show the usual settings I use and then I’ll show how to set them.

  • user.name: “Markku Leiniö
  • user.email: If I’m working with a GitHub repository then I’ll set it to “markkuleinio@users.noreply.github.com“. Otherwise I’ll use whatever identity is suitable in the case in question (for example in customer environments I’ll use my company email, and so on).
  • alias.s: “status -s
    • This will create “git s” command to show short status for the current repository
  • alias.lg: “log --oneline --all --graph --decorate
    • This will create “git lg” command to show the current repository commit log in more convenient format

The settings in Git can have different scopes:

  • local: the configuration is saved in the current repository
  • global: the configuration is saved in my home directory (to be valid for all actions I do with this user account on this system)
  • system: the configuration is saved in /etc directory (to be valid for all users on this system)

In most cases all my Git configurations are global so that they are valid in all repositories on the same server. In some repositories I may want to make the commits using a different email address (because not all of them are in GitHub but in other repository services), so in those repositories I can set the user.email setting locally to a different value.

Commands for setting and viewing the configurations:

  • git config <name-of-config-variable> <value>
    • Default scope is local
  • git config --global <name-of-config-variable> <value>
  • git config --system <name-of-config-variable> <value> (this will require proper access to modify /etc/gitconfig)
  • git config -l
    • Shows the current configuration

Examples (on a Linux system):

markku@server:~$ git config -l
markku@server:~$ git config -l --local
fatal: --local can only be used inside a git repository
markku@server:~$ git config -l --global
fatal: unable to read config file '/home/markku/.gitconfig': No such file or directory
markku@server:~$ git config -l --system
fatal: unable to read config file '/etc/gitconfig': No such file or directory
markku@server:~$

In this example there weren’t any specific Git configurations present yet, and there wasn’t a Git repository in the current directory. Let’s enter some configurations:

markku@server:~$ git config --global user.name "Markku Leiniö"
markku@server:~$ git config --global user.email markkuleinio@users.noreply.github.com
markku@server:~$ git config --global alias.s "status -s"
markku@server:~$ git config --global alias.lg "log --oneline --all --graph --decorate"
markku@server:~$ git config -l
user.name=Markku Leiniö
user.email=markkuleinio@users.noreply.github.com
alias.s=status -s
alias.lg=log --oneline --all --graph --decorate
markku@server:~$ git config -l --global
user.name=Markku Leiniö
user.email=markkuleinio@users.noreply.github.com
alias.s=status -s
alias.lg=log --oneline --all --graph --decorate
markku@server:~$ ls -l ~/.gitconfig
-rw-r--r-- 1 markku markku 145 Apr 17 14:37 /home/markku/.gitconfig
markku@server:~$ cat ~/.gitconfig
[user]
        name = Markku Leiniö
        email = markkuleinio@users.noreply.github.com
[alias]
        s = status -s
        lg = log --oneline --all --graph --decorate
markku@server:~$

This shows that all my settings are now saved in the .gitconfig file in my home directory, if I ever need to modify them directly or just back them up.

To demonstrate repository-local configurations let’s clone Pynetbox:

markku@server:~$ git clone https://github.com/netbox-community/pynetbox.git
Cloning into 'pynetbox'...
...
markku@server:~$ cd pynetbox
markku@server:~/pynetbox$ git config -l
user.name=Markku Leiniö
user.email=markkuleinio@users.noreply.github.com
alias.s=status -s
alias.lg=log --oneline --all --graph --decorate
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/netbox-community/pynetbox.git
remote.origin.fetch=+refs/heads/:refs/remotes/origin/
branch.master.remote=origin
branch.master.merge=refs/heads/master
markku@server:~/pynetbox$ git config -l --local
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/netbox-community/pynetbox.git
remote.origin.fetch=+refs/heads/:refs/remotes/origin/
branch.master.remote=origin
branch.master.merge=refs/heads/master
markku@server:~/pynetbox$ git config -l --global
user.name=Markku Leiniö
user.email=markkuleinio@users.noreply.github.com
alias.s=status -s
alias.lg=log --oneline --all --graph --decorate
markku@server:~/pynetbox$

Here it can be seen that the global configuration is still the same as earlier but the local configuration has some extra settings just for that repository. The local settings are saved in .git/config file within the repository.

Here are the defined aliases in action:

markku@server:~/pynetbox$ git s
markku@server:~/pynetbox$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
markku@server:~/pynetbox$ git lg
*   ace1061 (HEAD -> master, tag: v6.1.2, origin/master, origin/HEAD) Merge pull request #370 from digitalocean/fix-create-list
|\
| * 242caf8 Account for list returning from create
|/
1605121 Merge pull request #368 from digitalocean/update-docs
...
markku@server:~/pynetbox$ 

git s didn’t show anything because there is nothing to commit (as evidenced by the full git status command). git lg showed one line for each commit and it also tried to do some character-based graphical illustration about the merges.

There is also one configuration that I may do outside of Git: a custom Bash shell prompt to show the current branch whenever I’m inside a Git repository. This configuration goes in my ~/.bashrc:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi

Most of that is the factory-default Debian Linux configuration, but I have added the parse_git_branch() function and the calls for the function (with some coloring commands) in the Bash prompt. This is how it changes the Bash prompt when I enter the repository:

markku@server:~$ cd pynetbox
markku@server:~/pynetbox(master)$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
markku@server:~/pynetbox(master)$

This is how it looks like in color:

For more information about Git settings see git-config documentation.

Updated: February 6, 2022 — 14:25

Leave a Reply