AlanGreene / starter

Dotfiles starter project for new machines
1 stars 1 forks source link

More complete git config #28

Open AlanGreene opened 4 years ago

AlanGreene commented 4 years ago

consider aliases in .gitconfig instead of .bash_aliases

different configs for different projects

etc.

AlanGreene commented 4 years ago

GitHub SSH keys

#!/usr/bin/env bash

file="$HOME/.ssh/id_rsa_github.pub"
if [ ! -f "$file" ]; then
  ssh-keygen -f $HOME/.ssh/id_rsa_github -t rsa -b 4096 -C "github.com@alangreene.net"
fi

pbcopy < "$file"

Not needed for GitHub but useful for other SSH keys:

ssh-copy-id -i ~/.ssh/tatu-key-ecdsa user@host

#!/bin/bash

cd "$(dirname "${BASH_SOURCE[0]}")" \
    && . "utils.sh"

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

add_ssh_configs() {

    printf "%s\n" \
        "Host github.com" \
        "  IdentityFile $1" \
        "  LogLevel ERROR" >> ~/.ssh/config

    print_result $? "Add SSH configs"

}

copy_public_ssh_key_to_clipboard () {

    if cmd_exists "pbcopy"; then

        pbcopy < "$1"
        print_result $? "Copy public SSH key to clipboard"

    elif cmd_exists "xclip"; then

        xclip -selection clip < "$1"
        print_result $? "Copy public SSH key to clipboard"

    else
        print_warning "Please copy the public SSH key ($1) to clipboard"
    fi

}

generate_ssh_keys() {

    ask "Please provide an email address: " && printf "\n"
    ssh-keygen -t rsa -b 4096 -C "$(get_answer)" -f "$1"

    print_result $? "Generate SSH keys"

}

open_github_ssh_page() {

    declare -r GITHUB_SSH_URL="https://github.com/settings/ssh"

    # The order of the following checks matters
    # as on Ubuntu there is also a utility called `open`.

    if cmd_exists "xdg-open"; then
        xdg-open "$GITHUB_SSH_URL"
    elif cmd_exists "open"; then
        open "$GITHUB_SSH_URL"
    else
        print_warning "Please add the public SSH key to GitHub ($GITHUB_SSH_URL)"
    fi

}

set_github_ssh_key() {

    local sshKeyFileName="$HOME/.ssh/github"

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    # If there is already a file with that
    # name, generate another, unique, file name.

    if [ -f "$sshKeyFileName" ]; then
        sshKeyFileName="$(mktemp -u "$HOME/.ssh/github_XXXXX")"
    fi

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    generate_ssh_keys "$sshKeyFileName"
    add_ssh_configs "$sshKeyFileName"
    copy_public_ssh_key_to_clipboard "${sshKeyFileName}.pub"
    open_github_ssh_page
    test_ssh_connection \
        && rm "${sshKeyFileName}.pub"

}

test_ssh_connection() {

    while true; do

        ssh -T git@github.com &> /dev/null
        [ $? -eq 1 ] && break

        sleep 5

    done

}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

main() {

    print_in_purple "\n • Set up GitHub SSH keys\n\n"

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    if ! is_git_repository; then
        print_error "Not a Git repository"
        exit 1
    fi

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    ssh -T git@github.com &> /dev/null

    if [ $? -ne 1 ]; then
        set_github_ssh_key
    fi

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    print_result $? "Set up GitHub SSH keys"

}

main
AlanGreene commented 4 years ago

Allow different configs for work vs. personal

.gitconfig

https://git-scm.com/docs/git-config#_conditional_includes

[includeIf "gitdir:~/personal/"]
  path = ~/personal/.gitconfig
AlanGreene commented 3 years ago
AlanGreene commented 3 years ago

https://github.com/paulmillr/dotfiles/blob/master/home/.gitconfig https://github.com/DoomHammer/dotfiles/blob/master/.gitconfig https://github.com/DoomHammer/dotfiles/blob/master/.gitmessage

AlanGreene commented 3 years ago
[diff]
    # Show blocks of moved text of at least 20 alphanumeric characters differently than adds/deletes
    # https://blog.github.com/2018-04-05-git-217-released/
    colorMoved = zebra
AlanGreene commented 1 year ago

from Wes Bos' dotfiles

# Changed your .gitignore _after_ you have added / committed some files?
# run `gri` to untrack anything in your updated .gitignore
# Put this in your .zshrc file
alias gri="git ls-files --ignored --exclude-standard | xargs -0 git rm -r"

# Open all merge conflicts or currently changed files in VS Code
# Switch out `code` for `subl`, `vim`, `atom` or your editor's CLI
alias fix="git diff --name-only | uniq | xargs code"

alias "git latest"="git for-each-ref --sort=-committerdate refs/heads/"
AlanGreene commented 1 year ago

Include workflow templates for GitHub actions? For example, deployment to GitHub Pages:

AlanGreene commented 1 year ago

.git-blame-ignore-revs https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view

AlanGreene commented 3 months ago

git worktree vs. git stash