git
my aliases and the workflows behind them.
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
- everyday commits
- starting a branch
- cleaning up before a pr
- undoing a mistake
on this page
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
index
staged
head
commits
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.
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
| alias | expands to | what it does |
|---|---|---|
gst | git status | what changed, what's staged, and which branch you're on |
gss | git status --short | the same, one line per file |
gaa | git add --all | stage every change, new and deleted files too |
ga <file> | git add | stage one file |
gcam "msg" | git commit --all --message | stage changes to tracked files and commit them. new files still need gaa first |
gcmsg "msg" | git commit --message | commit only what's staged |
gd | git diff | changes you haven't staged yet |
gds | git diff --staged | staged changes: what the next commit will hold |
glog | git log --oneline --decorate --graph | one 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.
| alias | expands to | what it does |
|---|---|---|
gb | git branch | list local branches |
gco <branch> | git checkout | switch to a branch |
gcb <name> | git checkout -b | create a branch where you are and switch to it |
gnb <name>custom | git fetch origin main && git checkout -b <name> origin/main | fetch main, then branch off origin/main. starts from the remote, not a stale local main |
gcm | git checkout $(git_main_branch) | switch to main, or master if that's what the repo uses |
gcd | git checkout $(git_develop_branch) | switch to the develop branch (dev, devel, develop, or development) |
gbd <branch> | git branch --delete | delete a branch. refuses if it isn't merged |
gbD <branch> | git branch --delete --force | delete 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
| alias | expands to | what it does |
|---|---|---|
gp | git push | push this branch. on a branch with no upstream yet, it sets one |
gl | git pull | fetch, then rebase your local commits on top. no merge commit |
gfa | git fetch --all --tags --prune --jobs=10 | fetch 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
| alias | expands to | what it does |
|---|---|---|
gpleasecustomsafe | git push --force-with-lease | force push, but only if the remote is still where your last fetch saw it |
gpf!destructive | git push --force | overwrite 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
| alias | expands to | what it does |
|---|---|---|
gsave "msg"custom | git stash push -u -m "<msg>" | stash everything, untracked files too, under a name. the name defaults to wip |
gpopcustom | git stash pop | put the newest stash back and drop it from the list |
gstashescustom | git stash list --pretty='%C(yellow)%gd%Creset %s %C(green)(%cr)%Creset' | list stashes, with how long ago each one was made |
gstl | git stash list | list stashes |
gsts | git stash show --patch | show the newest stash as a diff |
gstd | git stash drop | delete one stash, the newest unless you name another |
gstcdestructive | git stash clear | delete 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.
| alias | expands to | what it does |
|---|---|---|
gwip | git 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 |
gunwip | git rev-list --max-count=1 --format="%s" HEAD | grep -q "\--wip--" && git reset HEAD~1 | if the last commit is a wip commit, undo it. the changes stay, unstaged |
gwipedestructive | git reset --hard && git clean --force -df | throw 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.
| alias | expands to | what it does |
|---|---|---|
grb <branch> | git rebase | replay your commits on top of another branch |
grbi HEAD~3 | git rebase --interactive | reorder, squash, reword, or drop recent commits |
grbc | git rebase --continue | keep going after you fix a conflict and stage it |
grba | git rebase --abort | stop, and put the branch back the way it was |
grbm | git rebase $(git_main_branch) | rebase onto your local main. update main first |
gm <branch> | git merge | merge 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
| alias | expands to | what it does |
|---|---|---|
guncustom | git reset --soft HEAD~1 | undo the last commit. its changes stay staged |
gfixcustom | git commit --amend --no-edit | fold what's staged into the last commit, same message |
gcan! | git commit --verbose --all --no-edit --amend | gfix, but it stages every tracked change first |
grh <file> | git reset | unstage a file. your edits stay |
grhhdestructive | git reset --hard | throw away every uncommitted change to tracked files |
grohdestructive | git reset origin/$(git_current_branch) --hard | make this branch match the remote. local commits and changes are dropped |
log · search
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.
| alias | expands to | what it does |
|---|---|---|
glastcustom | git show --stat HEAD | the last commit, with the files it changed |
gfind "text"custom | git log --all --oneline --grep="<text>" | search commit messages on every branch |
glo | git log --oneline --decorate | one 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.
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
- on a branch with no upstream yet, the first
gpsets one. no-u origin <branch>.3 - my
.gitconfigpagesgdandgdsthrough delta:nandNjump between files,qquits.9 gwip, then switch branches: the work stays behind as a commit on the old branch.gsavekeeps it off every branch until yougpopit.ghelp <word>filters the sheet in a terminal. tryghelp force,ghelp stash, orghelp undo.
sources
-
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
-
git commit (opens in a new tab) in the git docs:
--all,--amend, and--no-edit. ↩ ↩2 -
push.autoSetupRemote (opens in a new tab) and push.default (opens in a new tab) in the git config docs. ↩ ↩2
-
pull.rebase (opens in a new tab) in the git config docs. ↩
-
—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
-
git stash (opens in a new tab) in the git docs:
push -u, whatpopdoes on a conflict, andclear. ↩ -
git merge (opens in a new tab) in the git docs:
--ffis the default, so a branch that is simply behind is fast-forwarded without a merge commit, and--no-ffmakes one anyway. ↩ -
git reset (opens in a new tab) in the git docs: the
--soft,--mixed, and--hardmodes, andORIG_HEAD. ↩ -
navigation keybindings (opens in a new tab) in the delta docs. ↩