Functionality in this post is maintained in this Github Repo. PRs welcome!
I’m an idiot. And git is hard. A lot of places use a rebase-based Git workflow, and I’ve made git less hard with a set of handy aliases. Put these in your ~/.gitconfig
and turn git into an actually less painful command line tool to use.
[alias]
# *********************
# Rebase workflow
mainbranch = "!git remote show origin | sed -n '/HEAD branch/s/.*: //p'"
synced = "!git pull origin $(git mainbranch) --rebase"
update = "!git pull origin $(git rev-parse --abbrev-ref HEAD) --rebase"
squash = "!git rebase -v -i $(git mainbranch)"
publish = push origin HEAD --force-with-lease
pub = publish
Here’s documentation for these idiot-proof git aliases.
Publish your branch’s changes to the world
The publish
(abbrev pub
) command publishes your changes to your branch Github. If other changes have been posted by another user to Github, then changes will be rejected. You should update
first to incoroporate their changes and resolve any conflicts.
git publish
git pub
Under the hood this is a force push with lease to your branch at origin.
Sync your branch with main / master from Github
The synced
command will get your local branch up to date with your main branch (main or master) at origin (ie Github).
git synced
Under the hood, this fetches main or master from origin and performs a rebase, replaying your commits on top of the tip of main or master.
Update your branch with Github’s copy of your branch
If your local branch gets out of date with origin’s version of your branch, then do an update. This would happen if you’re collaborating on the branch with a colleague and need to get up to date.
git update
Under the hood, this fetches origin/your-branch and performs a rebase.
Squash commits
Prior to merging, you likely want to shrink your branch’s many noisy commits to one or two meaningful ones. That’s what squash
command does, giving you a chance to rewrite the branch’s history.
git squash
Under the hood, this is an interactive rebase.
Other useful commands
git pr
-> Open this PR at github.comgit hub
-> Open this repo at github.comgit amend
-> Alias forgit commit --amend
git ammend
-> How I always misspell ‘amend’, also alias forgit commit --amend
# ********************
# My dumbass typos
ammend = commit --amend
amend = commit --amend
# *********************
# Github
# From - https://salferrarello.com/git-alias-open-pull-request-github/
pr = "!f() { \
open \"$(git ls-remote --get-url $(git config --get branch.$(git rev-parse --abbrev-ref HEAD).remote) \
| sed 's|git@github.com:\\(.*\\)$|https://github.com/\\1|' \
| sed 's|\\.git$||'; \
)/compare/$(\
git config --get branch.$(git rev-parse --abbrev-ref HEAD).merge | cut -d '/' -f 3- \
)?expand=1\"; \
}; f"
hub = "!f() { \
open \"$(git ls-remote --get-url \
| sed 's|git@github.com:\\(.*\\)$|https://github.com/\\1|' \
| sed 's|\\.git$||'; \
)\"; \
}; f"
For simplicity, I use “Github” here to refer to the origin
remote, but replace whatever your Github with whatever your origin
is. The exception being git pr
and git hub
commands. Similarly I use main
as the trunk/main/master. But both main
and master
are supported.