Skip to content

Instantly share code, notes, and snippets.

@RobinBastiaan
Last active March 13, 2024 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobinBastiaan/743c3e36199ccca44a537fa755713609 to your computer and use it in GitHub Desktop.
Save RobinBastiaan/743c3e36199ccca44a537fa755713609 to your computer and use it in GitHub Desktop.

Configuration & Aliases

Various configuration and alias commands you can directly run from the command line.

Aliases are convenient shotcuts making your everyday work a little easier. This is especially true for commands that you use a lot and thus save you a lot of keystrokes, and for commands that are so long making them hard to remember and tidious to type. When designing an alias I make sure the abbreviation makes sence to me. In that way they are not too cryptic and makes sure I remember them more easily. I try to use the following methodology:

  • First, I abbriviate git commands with a 2 character representation. If there is a clash with another command, some opinionated judgement is needed to decide the most logical resolution.
  • To extend these basic abbriviations, I add a 3rd character to include some flags. Or maybe a 4th to extend these even more.
  • Then, for the most used commands (possibly with flags), I will make a 1 character representation. These are quick wins!
  • Lastly, there are some commands that are rarely used and thus harder to remember. These can use a more rememberable representation with the use of a full word.
  • However, some commands can rewrite or delete your work. These commands always need your full attention in order to prevent problems. For these kind of commands, an alias should only be made with great consideration. Therefor, when an alias is still considered usefull, the abbriviation should not be shorter than 4 characters.

GIT CONFIGURATION

git config --global core.commentchar ?
# Remove local branches that were deleted remotely when git fetch or git pull was performed
git config --global fetch.prune true
# Output line numbers in the result of git grep
git config --global grep.lineNumber true
git config --global init.defaultBranch main
# Reduce the need for --track or --set-upstream-to, possible since git 2.37.0
git config --global push.autoSetupRemote true
git config --global push.default current
git config --global tag.sort -v:refname
# Set the default date-time mode for the log command, which has a default value of `default`
git config --global log.date rfc
# Push git tags with commits
git config --global push.followTags true
git config --global rebase.abbreviateCommands true

# Disabled git configurations
# git config --global help.autocorrect 20
# Prevent unintentional creation of a merge commit if the branch being pulled has been modified locally
# git config --global pull.rebase true

GIT ALIASES

# First things first; lets make it easier to manage aliases

git config --global alias.aliases "config --get-regexp '^alias\.'"

# Quick wins; short commands with short notation

git config --global alias.a 'add .'
git config --global alias.d diff
git config --global alias.s 'status --short --branch'
git config --global alias.l 'log --oneline'

# Some basics

git config --global alias.br branch
git config --global alias.di diff
git config --global alias.ci commit
git config --global alias.cb count-objects
git config --global alias.cl clone
git config --global alias.co checkout
git config --global alias.cp cherry-pick
git config --global alias.fe fetch
git config --global alias.pl pull
git config --global alias.ps push
git config --global alias.rb rebase
git config --global alias.rl reflog
git config --global alias.rs restore
git config --global alias.sh stash
git config --global alias.st status
git config --global alias.sw switch

# Some extended basics

# Stash
git config --global alias.shc 'stash clear'
git config --global alias.shd 'stash drop'
git config --global alias.shl 'stash list'
git config --global alias.shp 'stash pop'
git config --global alias.sha 'stash push'
git config --global alias.shs 'stash save'
# Pull with rebase
git config --global alias.plr '!git pull --rebase=merges; git push';
git config --global alias.plrsh '!git stash; git pull --rebase=merges; git push; git stash pop'; # stash changes before git pull with rebase and unstash afterwards
# Push with tags
git config --global alias.pst '!git push && git push --tags'
git config --global alias.brc 'branch --show-current' # since git 2.22

# Other usefulness

git config --global alias.amend 'commit --amend --no-edit'
git config --global alias.aamend '!git add . && git commit --amend --no-edit'
git config --global alias.cm 'log -1 -1 --pretty=%B' # commit message without indent

# Find commits yet to be applied to upstream, with option to show the commit subjects next to the SHA1s
git config --global alias.cherryv 'cherry -v develop'
git config --global alias.cv 'cherry -v develop'

# Print the full commit hash, but highlight the hexits necessary to identify the commit
git config --global alias.commit-hash '!git rev-parse --verify HEAD | GREP_COLORS="ms=34;1" grep $(git rev-parse --short=7 HEAD)'
git config --global alias.ch '!git rev-parse --verify HEAD | GREP_COLORS="ms=34;1" grep $(git rev-parse --short=7 HEAD)'

git config --global alias.ds 'diff --staged'
git config --global alias.dss 'diff --staged --stat'
git config --global alias.dsss 'diff --staged --shortstat'
git config --global alias.du 'diff --name-status --diff-filter=U'
git config --global alias.pushd 'push -u origin HEAD'
git config --global alias.pst 'push --tags'
git config --global alias.unstage 'reset HEAD --'
git config --global alias.nah '!git reset --hard && git clean -df'
git config --global alias.nahh '!f(){ git reset --hard; git clean -df; if [ -d ".git/rebase-apply" ] || [ -d ".git/rebase-merge" ]; then git rebase --abort; fi; }; f' # https://laravel-news.com/the-ultimate-git-nah-alias
git config --global alias.laf 'fsck --lost-found'
git config --global alias.last 'log -1 HEAD'
git config --global alias.lastv 'log -1 HEAD --stat'

git config --global alias.set-upstream '!git branch --set-upstream-to=origin/$(git symbolic-ref --short HEAD)'
git config --global alias.su '!git branch --set-upstream-to=origin/$(git symbolic-ref --short HEAD)'

# Short log: show Git commit logs in a compact view
git config --global alias.slog "!git log --graph --all --topo-order --pretty='format:%h %ai %s%d (%an)'"
git config --global alias.parent "!git show-branch | grep '*' | grep -v \"$(git rev-parse --abbrev-ref HEAD)\" | head -n1 | sed 's/.*\\[\\(.*\\)\\].*/\\1/' | sed 's/[\\^~].*//' #"
git config --global alias.tagn 'tag -l [0-9]*' # Only list those tags which start with a number
git config --global alias.tagl 'describe --abbrev=0 --tags' # Last git tag (query all tags, not just annotated ones)
git config --global alias.tags 'tag -n99' # List tags with description lines
git config --global alias.standup 'log --since=yesterday.midnight --oneline'
git config --global alias.recent '!git for-each-ref --sort=-authordate | grep "refs/remotes/origin/" -m 10'
git config --global alias.commits '!git rev-list --full-history --all | wc -l'

# Based on tj/git-extras
git config --global alias.guiltt '!f() { git guilt `git log --until="$1" --format="%H" -n 1` HEAD; }; f'
# e.g. "git guiltt '1 week ago'"

# Potentially dangerous
git config --global alias.rest '!git restore . --staged && git restore .'
# Remove local branches that have already been merged into the current branch
git config --global alias.cleanbr '!git branch -d `git branch --merged | grep -v "^*\\|main\\|master\\|staging\\|develop"`'

# View graph of branches and their commits
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

# View branches with last change interval (the 10 most recent branches)
git config --global alias.lb !git reflog show --pretty=format:'%gs ~ %gd' --date=relative | grep 'checkout:' | grep -oE '[^ ]+ ~ .*' | awk -F~ '!seen[$1]++' | head -n 10 | awk -F' ~ HEAD@{' '{printf("  \033[33m%s: \033[37m %s\033[0m\n", substr($2, 1, length($2)-1), $1)}'

LINUX SHELL ALIASES

# If you don’t want to apply them and use the raw tool, you can add a backslash \ before its name.
alias cp='cp -i -v'
alias mv='mv -i -v'
alias rm='rm -i -v'

alias pa='php artisan'
alias parc='php artisan route:list -c'

DOCKER ALIASES

alias docker-cli="docker rmi $(docker images -qf dangling=true)"
alias docker-clc="docker rm -v $(docker ps -aqf status=exited)"
alias docker-clv="docker volume rm $(docker volume ls -qf dangling=true)"
# Remove stopped containers
alias docker_clean_ps='docker rm $(docker ps --filter=status=exited --filter=status=created -q)'

BASH ALIASES

Edit nano ~/.bashrc to add the following:

alias sagi="sudo apt-get install"

Links

https://www.atlassian.com/blog/git/advanced-git-aliases

@RobinBastiaan
Copy link
Author

RobinBastiaan commented Jun 7, 2023

TODO Find alias for:
git ls-files Path/To/My/Class | xargs wc -l | sort -r

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment