05 Apr 2023

Configure Git

See Pro Git if you are not familiar with Git.

1. Basic Configuration

1.1. Transfer Existed Configuration

  1. Copy the global git configuration file to ~/.gitconfig.
  2. Copy SSH keys to ~/.ssh/.
  3. Add SSH key to ssh-agent.

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
    

1.2. First-Time Set up

See also Pro Git - Section 1.6

  1. Set up your identity

    git config --global user.name "John Doe"
    git config --global user.email johndoe@example.com
    
  2. Set up default editor

    git config --global core.editor vim
    
  3. Set up default branch name

    git config --global init.defaultBranch master
    
  4. Set up git aliases

    git config --global alias.co checkout
    git config --global alias.br branch
    git config --global alias.ci commit
    git config --global alias.st status
    
  5. (Optional) Review your settings

    git config --list --show-origin
    
  6. Set up SSH.
    1. See Generating a new SSH key and adding it to the ssh-agent
    2. See Adding a new SSH key to your GitHub account

2. Add-ons

2.1. Prompt

bash-git-prompt: A bash prompt that displays information about the current git repository. In particular the branch name, difference with remote branch, number of files staged, changed, etc.

gitprompt.png
Figure 1: Example of bash-git-prompt

2.2. Pager

delta: A syntax-highlighting pager for git, diff, and grep output.

delta.png
Figure 2: Example of delta

3. An Example of .gitconfig

[user]
    name = John Doe
    email = johndoe@example.com
[core]
    editor = code --wait
    pager = delta
[init]
    defaultBranch = master
[alias]
    a = add
    b = branch
    co = checkout
    ci = commit
    ca = commit -a
    cm = commit -m
    d = diff
    l = log --oneline -15
    la = log --oneline --all -15
    rb = rebase
    s = status

    last = log -1
    squash = rebase --autosquash -i
[interactive]
    diffFilter = delta --color-only
[delta]
    light = true      # set to true if you're in a terminal light background color
    side-by-side = true
[merge]
    conflictstyle = diff3
[diff]
    colorMoved = default
Tags: tool
Created by Org Static Blog