tools

git

my aliases and the workflows behind them.

report a mistake
.md

one letter per verb: g + the git word. the custom ones fix the sharp edges.

aliases
47 38 from oh-my-zsh, 9 mine
custom
9 in my .gitconfig
destructive
5 can throw work away
in a terminal
ghelp ghelp <word> filters it
reach for it
  • everyday commits
  • starting a branch
  • cleaning up before a pr
  • undoing a mistake
on this page
  1. everyday
  2. branch
  3. push · pull · sync
  4. force push
  5. stash
  6. wip
  7. rebase · merge
  8. undo · amend
  9. log · search
  10. try it
  11. tips
  12. sources

this is my ghelp sheet with pictures: every git alias i use, most from the oh-my-zsh git plugin1 and nine of my own from my .gitconfig. in a terminal, ghelp <word> filters the same sheet, like ghelp stash or ghelp undo.

working tree

your edits

api.tsnav.cssreadme.md

index

staged

head

commits

base
stash
gone

a change lives in one of three places. press a command and watch where it goes.

an interactive figure with three columns, working tree, index and head, plus a stash shelf. buttons run ga, gaa, gcmsg, grh, gun, gfix, gsave, gpop and grhh, and three file chips move between the columns to show where each command sends a change.

every row below moves a change between these places. ga and gaa go right, grh and gun go left, gsave parks it off the branch, grhh throws it away.

everyday

the loop: look at what changed, stage it, commit it. gd shows the edits you haven’t staged, gds shows what the next commit will hold. gcam only picks up files git already tracks, so a new file still needs gaa first.2

everyday
aliasexpands towhat it does
gstgit statuswhat changed, what's staged, and which branch you're on
gssgit status --shortthe same, one line per file
gaagit add --allstage every change, new and deleted files too
ga <file>git addstage one file
gcam "msg"git commit --all --messagestage changes to tracked files and commit them. new files still need gaa first
gcmsg "msg"git commit --messagecommit only what's staged
gdgit diffchanges you haven't staged yet
gdsgit diff --stagedstaged changes: what the next commit will hold
gloggit log --oneline --decorate --graphone line per commit, with the branch graph

branch

gcb branches from wherever you are. gnb is mine: it fetches first and branches from origin/main, so the new branch starts from what’s on the remote, even if your local main is days old.

branch
aliasexpands towhat it does
gbgit branchlist local branches
gco <branch>git checkoutswitch to a branch
gcb <name>git checkout -bcreate a branch where you are and switch to it
gnb <name>customgit fetch origin main && git checkout -b <name> origin/mainfetch main, then branch off origin/main. starts from the remote, not a stale local main
gcmgit checkout $(git_main_branch)switch to main, or master if that's what the repo uses
gcdgit checkout $(git_develop_branch)switch to the develop branch (dev, devel, develop, or development)
gbd <branch>git branch --deletedelete a branch. refuses if it isn't merged
gbD <branch>git branch --delete --forcedelete a branch even if it isn't merged

push · pull · sync

two settings in my .gitconfig make plain gp and gl do more. push.autoSetupRemote makes the first push of a branch with no upstream set one, so there’s no git push -u origin <branch>, and push.default = current sends it to a branch with the same name.3 pull.rebase makes gl rebase your local commits on top of what it fetched, instead of making a merge commit.4

[push]
	default = current
	autoSetupRemote = true
[pull]
	rebase = true
push · pull · sync
aliasexpands towhat it does
gpgit pushpush this branch. on a branch with no upstream yet, it sets one
glgit pullfetch, then rebase your local commits on top. no merge commit
gfagit fetch --all --tags --prune --jobs=10fetch every remote, and forget remote branches that were deleted

force push

after a rebase or an amend, your branch and the remote one have different histories, so a plain push is refused. gplease is mine too: it only overwrites the remote if it still points where your last fetch saw it, so a teammate’s new commit makes the push fail instead of disappearing.5

the check is only as fresh as that fetch. after a gfa, it compares against what you just pulled down, so look at what came in before you push again. the plugin’s gpf, without the bang, adds --force-if-includes, which also refuses when fetched commits never made it into your branch.5

force push
aliasexpands towhat it does
gpleasecustomsafegit push --force-with-leaseforce push, but only if the remote is still where your last fetch saw it
gpf!destructivegit push --forceoverwrite the remote branch, whatever is on it

stash

a stash puts your changes on a shelf that belongs to no branch: switch, do something else, come back, gpop. gsave names the stash and takes untracked files too, which a plain git stash leaves behind. if gpop hits a conflict, the stash stays in the list, so fix the conflict and gstd it yourself.6

stashhides from branch
aliasexpands towhat it does
gsave "msg"customgit stash push -u -m "<msg>"stash everything, untracked files too, under a name. the name defaults to wip
gpopcustomgit stash popput the newest stash back and drop it from the list
gstashescustomgit stash list --pretty='%C(yellow)%gd%Creset %s %C(green)(%cr)%Creset'list stashes, with how long ago each one was made
gstlgit stash listlist stashes
gstsgit stash show --patchshow the newest stash as a diff
gstdgit stash dropdelete one stash, the newest unless you name another
gstcdestructivegit stash cleardelete every stash. they may be impossible to get back

wip

the other way to park work. gwip commits everything as a throwaway --wip-- commit, so the work stays on that branch, and goes to the remote when you push it. gunwip takes it back off when you return, and only if the last commit really is a wip commit.1 careful with gwipe: it sits here because of its name, and it deletes uncommitted work instead of saving it.

wipcommits, travels with branch
aliasexpands towhat it does
gwipgit add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign --message "--wip-- [skip ci]"commit everything, untracked files too, as one --wip-- commit. skips hooks and signing
gunwipgit rev-list --max-count=1 --format="%s" HEAD | grep -q "\--wip--" && git reset HEAD~1if the last commit is a wip commit, undo it. the changes stay, unstaged
gwipedestructivegit reset --hard && git clean --force -dfthrow away every uncommitted change and delete untracked files. nothing to do with gwip

rebase · merge

gnb feat/x: fetch main, then branch off origin/main. a pointer, no commit yet.

an animated commit graph with a main lane and a feat/x lane. gnb branches off a fresh origin/main, two commits land on the branch and the first gp puts them on origin/feat/x, main moves on, grbm replays the two commits after main's new tip, gplease moves origin/feat/x to the rewritten branch, gm --no-ff merges it into main with a merge commit, and gp pushes main.

both bring another branch’s commits into yours. gm joins the two histories with a merge commit. when your branch has nothing of its own, as main right after a rebase, it fast-forwards instead: the pointer moves, no commit, unless you say gm --no-ff.7 grb replays your commits on top of the other branch, so history stays one straight line, but the replayed commits are new commits, and a branch you already pushed needs gplease after.

grbm rebases onto your local main, so update main first, or gfa and grb origin/main. if a rebase stops on a conflict, fix the files, stage them, and grbc. grba puts everything back the way it was.

rebase · merge
aliasexpands towhat it does
grb <branch>git rebasereplay your commits on top of another branch
grbi HEAD~3git rebase --interactivereorder, squash, reword, or drop recent commits
grbcgit rebase --continuekeep going after you fix a conflict and stage it
grbagit rebase --abortstop, and put the branch back the way it was
grbmgit rebase $(git_main_branch)rebase onto your local main. update main first
gm <branch>git mergemerge a branch into this one

undo · amend

gun takes the last commit back and leaves its changes staged, so you can fix them and commit again. gfix folds whatever is staged into the last commit and keeps its message.2 both replace a commit, so if it was already pushed, follow up with gplease.

the resets differ in what they keep. --soft keeps the changes staged, the default --mixed keeps them unstaged (grh), and --hard throws them away (grhh, groh). uncommitted work lost to a hard reset is gone, but commits aren’t: git saves the old tip as ORIG_HEAD, so git reset --hard ORIG_HEAD right after a bad groh brings them back.8

undo · amend
aliasexpands towhat it does
guncustomgit reset --soft HEAD~1undo the last commit. its changes stay staged
gfixcustomgit commit --amend --no-editfold what's staged into the last commit, same message
gcan!git commit --verbose --all --no-edit --amendgfix, but it stages every tracked change first
grh <file>git resetunstage a file. your edits stay
grhhdestructivegit reset --hardthrow away every uncommitted change to tracked files
grohdestructivegit reset origin/$(git_current_branch) --hardmake this branch match the remote. local commits and changes are dropped

glo is one line per commit, and glog adds the branch graph. glast answers “what did i just commit”, with the files it touched. gfind searches commit messages on every branch, not just this one.

log · search
aliasexpands towhat it does
glastcustomgit show --stat HEADthe last commit, with the files it changed
gfind "text"customgit log --all --oneline --grep="<text>"search commit messages on every branch
glogit log --oneline --decorateone line per commit, with branch and tag names

try it

six situations, three aliases each. pick the one you would reach for.

1 / 6

you committed, then noticed a typo in a file that should have been in that commit.

pick the alias you would reach for.

0 / 0 right first try

a quiz: a situation like 'you committed on main by mistake', three aliases to pick from, and the reason the right one is right.

tips

sources

  1. the oh-my-zsh git plugin (opens in a new tab), where every alias here that isn’t one of mine comes from. the expansions are copied from its source (opens in a new tab). ↩ ↩2

  2. git commit (opens in a new tab) in the git docs: --all, --amend, and --no-edit. ↩ ↩2

  3. push.autoSetupRemote (opens in a new tab) and push.default (opens in a new tab) in the git config docs. ↩ ↩2

  4. pull.rebase (opens in a new tab) in the git config docs. ↩

  5. —force-with-lease (opens in a new tab) and —force-if-includes (opens in a new tab) in the git push docs, including the note on fetches that run in the background. ↩ ↩2

  6. git stash (opens in a new tab) in the git docs: push -u, what pop does on a conflict, and clear. ↩

  7. git merge (opens in a new tab) in the git docs: --ff is the default, so a branch that is simply behind is fast-forwarded without a merge commit, and --no-ff makes one anyway. ↩

  8. git reset (opens in a new tab) in the git docs: the --soft, --mixed, and --hard modes, and ORIG_HEAD. ↩

  9. navigation keybindings (opens in a new tab) in the delta docs. ↩