Site Tools


This is an old revision of the document!


Some handy git aliases I use all the time

1. choose branch to check out from fzf

'fzf' is such a great tool, it changes the way how I operate in vim and shell, git checkout autocomplate with branch is great, but it's better to list branches in a menu then choose it, therefore I add below to git config file:

cof = !git for-each-ref --format='%(refname:short)' refs/heads | fzf | xargs git checkout

so this one-liner list all local branches in fzf, then I could type to narrow down, then press enter to check out the selected branch.

Sometimes, I want to checkout remote branch, I use alias below, 'refname:lstrip=3' strips the remote name from branch name ref, so git checkout could figure out and create a local branch to match it.

cofr = !git for-each-ref --format='%(refname:lstrip=3)' refs/remotes | fzf | xargs git checkout

2. bulk delete branches

	delete-branches = "!git branch | grep -v '\\*' | fzf --multi | xargs git branch --delete --force"

The idea is list all local branches, but remove current branch (it's marked with *), pipe to fzf so user could select them by pressing 'tab', finally pipe to git to delete.

3. List local branches by commit date

More informative branch list with tip commit message and relative date.

	bl = branch --format='%(HEAD) %(color:yellow)%(committerdate:relative)\t%(color:red)%(refname:short)%(color:cyan)\n\t\t%(contents:subject) %(color:green) [%(authorname)]' --sort=-committerdate

4. Copy commit hash from git log in fzf

	pickhash = !git log --abbrev-commit --color=always | fzf --ansi  | awk '{print $1}' | pbcopy

5. Pick a commit, then pick changed file and open in vim

	edited-files = !git pickhash | xargs -o git show --format="format:" --name-only
	pickfile = !git edited-files | fzf --sync | sed -e "s#^#`git rev-parse --show-toplevel`/#" | xargs -o vim
blog/2023/10/12/git-alias.1697162430.txt.gz · Last modified: by dcai