Notes on git

This page is managed by StJohn Piano.

Notes on git

If you have any questions, comments, or suggestions - please contact StJohn Piano on Tela:

Show colored history of all branches

git log --graph --oneline --all --decorate --abbrev-commit --date=format:'%Y-%m-%d %H:%M:%S' --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ad)%C(reset) %C(white)%s%C(reset) %C(bold white)- %an%C(reset)%C(bold yellow)%d%C(reset)'

To then use the shortcut git history, add this function to .zshrc:

git() {
    if [[ $@ == "history" ]]; then
        command git log --graph --oneline --all --decorate --abbrev-commit --date=format:'%Y-%m-%d %H:%M:%S' --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ad)%C(reset) %C(white)%s%C(reset) %C(bold white)- %an%C(reset)%C(bold yellow)%d%C(reset)'
        command git "$@"

.gitconfig file

% cat ~/.gitconfig
  name = StJohn Piano
  email =
  co = checkout
  br = branch
  ci = commit
  st = status
  log1 = log --oneline
  who = blame
  defaultBranch = main
  autoSetupRemote = True

.zshrc file

alias g="git"
alias gs="git status"
alias ga="git add --patch"
alias gc="git commit"
alias gr="git reset HEAD"

Set git user globally and locally

# List git config
git config --list

# Set global git user
git config --global "StJohn Piano"
git config --global ""

# Set git user for local repo
git config "StJohn Piano @ CompanyX"
git config ""

# Get git user from current config
# Note: Local config supersedes global config.
git config --get
git config --get

# See config for local repo
cd path/to/repo
cat .git/config

Create a new SSH key for your Github account

# Create a new SSH key
# Note: No passphrase is used.
ssh-keygen -t rsa -b 4096 -C $SSH_EMAIL -f $SSH_FILEPATH -q -N ""

# Start SSH agent
eval "$(ssh-agent -s)"

# Add the SSH private key to the ssh-agent

Now log on to Github and add your SSH key to your account. Here's the guide:

Github / Settings / SSH and GPG keys / New SSH key

Use this format for the key title:

If it's a workplace key, name it companyname_username@computername instead.

Print the public key for pasting into the Key textarea:
cat $

Configure git repo to use SSH to connect to the remote

If you've cloned a repo from Github using HTTPS, then when you try to push, you'll be prompted for your Github username and password. It's more convenient to configure git to use your SSH key to connect, so that you avoid the prompt.

# See the name of the remote
git remote

# See the links to the remote
git remote --verbose

# See the current URL for the remote repo
# We assume that the remote's name is 'origin'.
git remote get-url origin

# Set the remote URL to use SSH.
# Format:
# Note: Double-check that you've used a colon (not a slash) after "".
git remote set-url origin

Configure git repo to use a different SSH key to connect to Github

Situation: You're at work, and your default git config uses the work Github SSH key, but you need to make some commits on a personal Github repo, using a personal Github key. However, you don't want to use your actual personal key (which should stay on your personal machine), so instead it's best to create a new personal key specifically for your workplace machine.

Create a new SSH key (following earlier guide in this article), using this format for the name:

Adjust your .ssh/config file to contain these Host entries:

  IdentityFile ~/.ssh/id_ed25519.github.companyname_username.computername

  IdentityFile ~/.ssh/id_rsa.github.companyname_username_personal.computername

You can test the connection by running:

ssh -T

ssh -T

If you want to confirm that the correct key is used for each host, you can grep the debug output:

ssh -T -v 2>&1 | grep 'identity file'

ssh -T -v 2>&1 | grep 'identity file'

Now, in the local git repo, change the remote URL to use the "personal" github Host entry in your SSH config.

# Set the remote URL to use the personal SSH host.
git remote set-url origin

# Double-check
git remote get-url origin

Double-check that ssh-agent is running and that you've added the new key to it. Check the list of keys in ssh-agent by running:
ssh-add -l

You should now be able to git push to your personal repo with your new workplace personal key.

You can test by running:
git push --dry-run