#!/usr/bin/env bash # SPDX-License-Identifier: 0BSD # bash <(curl --proto '=https' --tlsv1.2 -sSf https://ftp.silversoft.se/git-setup.sh) ################################################################################ # Helper script to set up some sane defaults for git configuration. # ################################################################################ echo -n "$(tput setaf 121)" && clear cat < /dev/null then echo "$1 is not installed. Exiting..." exit 1 fi } # Function to ask for confirmation with a message. ask_confirmation() { read -p "$1 [y/N]: " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]] then echo "Aborting and exiting..." exit 1 fi } # Check if git is installed assure_installed git # Ask if the user wants to continue ask_confirmation "Install git configuration?" # Backup the current git config if it exists. echo "Backing up current git configuration..." mv $config_path ~/.gitconfig_backup_"$timestamp" # Get name and email from current git config name=$(git config --global user.name) email=$(git config --global user.email) # This is bash magic to get the current user with the first # letter in uppercase. This will be used as global user.name. user=${USER^^${USER:0:1}} # If the old name is not empty, we will use it. if [ -n "$name" ]; then echo "Using current user name: $name" user=$name fi # Email here should be empty, which is desired, since # we do not want our email in source control. git config --global user.email "$email" git config --global user.name "$user" # These are sometimes a pain, given you need a valid ssh key for pulling. # git config --global url.ssh://git@bitbucket.org/.insteadOf https://bitbucket.org/ # git config --global url.ssh://git@github.com/.insteadOf https://github.com/ # git config --global url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/ # Set the default editor to neovim git config --global core.editor nvim # Set default pull to rebase and upstream git config --global pull.rebase true git config --global pull.default upstream # Set default branch to master. Some prefer main. # I use master for compatibility reasons. git config --global init.defaultBranch master # We want our dates to be relative git config --global log.date relative # Set autocorrect to prompt # Makes it correct typos, but asks for confirmation git config --global help.autoCorrect prompt # Sort branches by committer date, not author date # Will make ´git branch´ and ´git log --graph --oneline --all´ show branches # in the order they were last committed to, not when they were created. git config --global branch.sort -committerdate # Sort `git tag` by semver, in descending order git config --global tag.sort -version:refname ################################################################################ # Below are some settings aliases and pretty formats that I like to use. # ################################################################################ # Beware of the space placement here, it is easy to mess up. # Default pretty format for git log git config --global format.pretty \ "format:%C(auto,yellow)%h%C(auto,magenta)% G? \ %C(auto,blue)%<(14,trunc)%ad %C(auto,green)%<(8,trunc)\ %aN%C(auto,reset)%s%C(auto,red)% gD% D" # Set git alias 'l' for a pretty log with graph. git config --global alias.l \ "log --date-order --date=iso --graph --full-history --all \ --pretty='format:\ %x08%x09%C(red)%h \ %C(cyan)%ad%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08%x08 \ %C(bold blue)%<(8,trunc)%aN%C(reset)%C(bold yellow)%d %C(reset)%s'" # Set git alias 's' for a pretty status git config --global alias.s \ "status --short --branch --untracked-files --ahead-behind" # Diff with stat git config --global alias.d \ "diff --stat" git config --global alias.hotspot \ "!git log --name-only --pretty=format: | grep -v '^$' | sort | uniq -c | sort -nr | head" git config --global alias.recent \ "!git log --since='2 weeks ago' --name-only --pretty=format: | grep -v '^$' | sort | uniq -c | sort -nr" git config --global alias.topfiles \ "!git log --name-only --pretty=format: | grep -v '^$' | sort | uniq -c | sort -nr | head -n 10" ################################################################################ ################################################################################ if [ -z "$email" ]; then email=" (Empty email in git config)" fi echo echo " | Configured identity:" echo " | Name: $(git config --global user.name)" echo " | Email: $email" echo echo -e "A backup of your old config was saved to \e[33m~/.gitconfig_backup_$timestamp\e[0m" echo -e "You can view your new configuration with \e[33mgit config --global -l\e[0m" echo -e "Your git configuration has been set up." echo -e "\e[32mSuccess!\e[0m"