Skip to content

Instantly share code, notes, and snippets.

@matthewpwatkins
Last active April 1, 2024 19:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save matthewpwatkins/79093d4bf0d55c2e7269f534f1b6a689 to your computer and use it in GitHub Desktop.
Save matthewpwatkins/79093d4bf0d55c2e7269f534f1b6a689 to your computer and use it in GitHub Desktop.
Git aliases script

This script sets up your Git environment by creating/setting several common aliases and shortcuts for them.

# Remove all existing aliases
current_aliases=$(git config --get-regexp alias);
if [ ${#current_aliases} -gt 0 ]; then
echo 'Removing all existing aliases';
git config --global --remove-section alias;
fi
# Add aliases
echo 'Creating custom aliases';
git config --global alias.amend '!git commit --amend --no-edit';
git config --global alias.sync '!git pull && git push';
git config --global alias.show-aliases '!git config --get-regexp alias';
git config --global alias.pretty-log '!git log --pretty=format:"%ad%C(red)%d %C(yellow)[%h] %C(green)%cn: %Creset%s " --decorate --date=short';
git config --global alias.trash '!git checkout . && git reset --hard HEAD && git clean -fd';
git config --global alias.changed-files '!git show --pretty="" --name-only';
git config --global alias.show-summary '!git show -s $1 && echo "----------------------------------------" && git changed-files $1';
git config --global alias.changed-files-diff '!git difftool $1^ $1';
git config --global alias.list-untracked-files '!git ls-files --others --exclude-standard';
git config --global alias.delete-untracked-files '!git list-untracked-files | xargs -n 1 rm -fr';
git config --global alias.list-staged-files '!git diff --cached --name-only';
git config --global alias.list-unstaged-files '!git diff --name-only';
git config --global alias.unlock '!rm ./.git/index.lock';
git config --global alias.nuke '!git trash && git clean -fxd';
git config --global alias.delete-other-branches '!git branch | grep -v "\*" | xargs -I {} git br -D {}';
git config --global alias.all '!find . -maxdepth 1 -mindepth 1 -type d | xargs -L1 -I {} sh -c "echo && echo && echo ========== {} ========== && git -C {} $1"';
git config --global alias.delete-remote-branch '!git push origin --delete';
# Add shortcuts
echo 'Creating shortcut aliases';
git config --global alias.co '!git checkout';
git config --global alias.st '!git status';
git config --global alias.br '!git branch';
git config --global alias.sa '!git show-aliases';
git config --global alias.pl '!git pretty-log';
git config --global alias.cp '!git cherry-pick';
git config --global alias.tr '!git trash';
git config --global alias.cf '!git changed-files';
git config --global alias.ss '!git show-summary';
git config --global alias.cfd '!git changed-files-diff';
git config --global alias.luf '!git list-untracked-files';
git config --global alias.duf '!git delete-untracked-files';
git config --global alias.lsf '!git list-staged-files';
git config --global alias.lusf '!git list-unstaged-files';
git config --global alias.dob '!git delete-other-branches';
git config --global alias.drb '!git delete-remote-branch'
# Add typo correction
echo 'Creating typo aliases';
git config --global alias.commti 'commit';
git config --global alias.sttaus 'status';
git config --global alias.reste 'reset';
echo 'Done';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment