Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.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"';
# 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';
# 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