Hacker News new | ask | show | jobs
by bloopernova 599 days ago
Some of my git aliases might be useful to folks here (I post them from time to time on git threads)

Time to share my gitconfig aliases again :D

  lol = !git --no-pager log --graph --decorate --abbrev-commit --all --date=local -25 --pretty=short
  sw = !git checkout $(git branch -a --format '%(refname:short)' | sed 's~origin/~~' | sort | uniq | fzf)
  lc = !git rev-parse HEAD
  rb = !git for-each-ref --sort=-committerdate refs/heads/ --format='%(refname:short) %(objectname:short) %(committerdate:format:%F)' | column -t
  fza = "!git ls-files -m -o --exclude-standard | fzf -m --print0 | xargs -0 git add"
  gone = "!f() { git fetch --all --prune; git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D; }; f"
  root = rev-parse --show-toplevel
  oldest-ancestor = !zsh -c 'diff -u <(git rev-list --first-parent "${1:-main}") <(git rev-list --first-parent "${2:-HEAD}") | sed -ne \"s/^ //p\" | head -1' -
  diverges = !sh -c 'git rev-list --boundary $1...$2 | grep "^-" | cut -c2-'
  dlog = "!f() { GIT_EXTERNAL_DIFF=difft git log -p --ext-diff $@; }; f"
Some of these I don't use much, but others I use every day:

"git fza" (aliased to "ga") shows all unstaged files in fzf and you can use space to toggle them, then hitting enter finishes adding/staging them. This is great for selecting some files to stage. I use this one every day, it makes my workflow just a little better :)

"git gone" deletes local branches that don't exist in the remote. I just saw in this thread that git remote prune origin might do the same thing, I need to test that.

"git lol" is a log alias.

"git oldest-ancestor brancha branchb" does what it says.

"git root" is part of an alias "gr" which runs "cd $(git root)". That takes you to the project root, and "cd -" will take you back to your previous location.

"git dlog" shows a detailed commit log.

"git lc" just shows the last commit.

"git rb" shows recent branches. Piping it to "| sort -k3" will sort by date. (I really need to update that!)

"git sw" shows branches in fzf, hit enter on one and you checkout that branch.

4 comments

If you use oh-my-zsh's git plugin, it defines a huge number of aliases that minimize the characters you have to type in. I use these mostly every day:

  gl = git pull
  gp = git push
  ga = aliased to git fza from my above comment
  gc = "git commit -m " so I type 'gc "chore: commit message"'
  gd = git diff
  gs = git status
  gr = cd's to the top level of the repo, cd - returns to your previous position.
I had to override some of the oh-my-zsh defaults in my .zshrc:

  unalias gcd
  unalias ga
  unalias grep
  unalias gr
  unalias gc

  alias m='mise'
  alias fd='fd -HI'
  alias gfv='git fetch --verbose'
  alias gs='git status'
  alias gr='cd $(git rev-parse --show-toplevel)'
  alias ga='git fza' # depends on gitconfig containing the alias "fza"
  alias gc='git commit -m' # depends on "unalias gc"
  alias commit-types='cat $HOME/.local/dotfiles/commit-types.txt'
  alias ct='cat $HOME/.local/dotfiles/commit-types.txt'
  alias mcdd='mcd $(date "+%Y-%m-%d")'
  alias sp='showpath' # showpath = echo $PATH | sed -e $'s/:/\\\n/g'
  alias uuidgen='uuidgen | tr "[:upper:]" "[:lower:]"'
I hope these are useful to someone at some point!
If you really want to minimize typing, try Emacs + Magit.
I should, I use Emacs a bunch, but I guess it's just muscle memory now to flip to the terminal.
I see that you're using --print0 and -0 for your "git fza" alias, it might be useful to add -z to the git-ls-files invocation and --read0 to fzf.
Ah, good point, I do need to revisit these and tidy them up. The perils of cargo-culting!
> "git oldest-ancestor brancha branchb" does what it says.

The oldest (common) ancestor of two revisions would typically be the initial commit. I assume your alias really finds the last (most recent) common ancestor. But are you aware of the `git merge-base` builtin? Your alias looks an awful lot like it.

Oh, yes that's exactly what I really meant. Whoops.

I'll check out merge-base tomorrow, thanks for mentioning it!

Fairly sure git remote prune will do just the remote-tracking refs/remote branches, same as fetch --prune that you're already using inside your alias. It won't remove your checked-out local copies of those.