Git Commands that I Find Useful
Many of these are initially sourced from Stack Overflow or great articles that have titles like “5 Must-Know Git Commands” then modified to fit my workflow (here’s one great example for popular git configs). Some are actually put together by reading the docs.
Delete Branches that have been Remove from Remote
For this to delete branches, local must know that remote has removed branches (which git branch -v lists as [gone]). One way to accomplish this is to configure your fetch to always prune.
git branch -v | awk '/ \[gone\] /{ print $1 }' | xargs git branch -dCode language: JavaScript (javascript)
Always Prune on Fetch
From the docs, “…remove any remote-tracking references that no longer exist on the remote.” git pull and git fetch would both have the following setting applied, since git pull is (in my mind) like running git fetch and git merge.
git config --global fetch.prune trueCode language: PHP (php)
Viewing the global git config (cat ~/.gitconfig) should now show
[fetch]
prune = trueCode language: JavaScript (javascript)
Always Rebase on Pull
git config --global pull.rebase trueCode language: PHP (php)
Viewing the global git config (cat ~/.gitconfig) should now show
[pull]
rebase = trueCode language: JavaScript (javascript)
List Subtrees
git log | awk '/git-subtree-dir: / { print $2 }' | uniqCode language: JavaScript (javascript)
As an alias (global .gitconfig):
[alias]
subtrees = !git log | awk '/git-subtree-dir: / { print $2 }' | uniqCode language: JavaScript (javascript)
Delete Orphaned Local Branches
Prerequisite to this your local branches must have references removed to non-existent remote branches.
git branch -v | awk '/ \[gone\] /{ print $1 }' | xargs git branch -dCode language: JavaScript (javascript)
As an alias (global .gitconfig):
[alias]
prune-delete = !git branch -v | awk '/ \\[gone\\] /{ print }' | xargs git branch -dCode language: JavaScript (javascript)