# git

> my aliases and the workflows behind them.

source: https://learning.anthnydas.com/tools/git/

## tl;dr

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

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

> **interactive figure:** 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.[^commit]

| alias         | expands to                             | what it does                                                                   | mark |
| ------------- | -------------------------------------- | ------------------------------------------------------------------------------ | ---- |
| `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                                                                            | mark   |
| -------------- | ------------------------------------------------------------- | --------------------------------------------------------------------------------------- | ------ |
| `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>`   | `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 | custom |
| `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.[^push] `pull.rebase` makes `gl` rebase your local commits on top of what it fetched, instead of making a merge commit.[^pullrebase]

```ini
[push]
	default = current
	autoSetupRemote = true
[pull]
	rebase = true
```

| alias | expands to                                 | what it does                                                     | mark |
| ----- | ------------------------------------------ | ---------------------------------------------------------------- | ---- |
| `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.[^lease]

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.[^lease]

| alias     | expands to                    | what it does                                                             | mark         |
| --------- | ----------------------------- | ------------------------------------------------------------------------ | ------------ |
| `gplease` | `git push --force-with-lease` | force push, but only if the remote is still where your last fetch saw it | custom, safe |
| `gpf!`    | `git push --force`            | overwrite the remote branch, whatever is on it                           | destructive  |

## 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.[^stash]

| alias         | expands to                                                                | what it does                                                                  | mark        |
| ------------- | ------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ----------- |
| `gsave "msg"` | `git stash push -u -m "<msg>"`                                            | stash everything, untracked files too, under a name. the name defaults to wip | custom      |
| `gpop`        | `git stash pop`                                                           | put the newest stash back and drop it from the list                           | custom      |
| `gstashes`    | `git stash list --pretty='%C(yellow)%gd%Creset %s %C(green)(%cr)%Creset'` | list stashes, with how long ago each one was made                             | custom      |
| `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                          |             |
| `gstc`        | `git stash clear`                                                         | delete every stash. they may be impossible to get back                        | destructive |

## 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.[^omz] 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                                                                            | mark        |
| -------- | ------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- | ----------- |
| `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                 |             |
| `gwipe`  | `git reset --hard && git clean --force -df`                                                                                     | throw away every uncommitted change and delete untracked files. nothing to do with gwip | destructive |

## rebase · merge

> **interactive figure:** 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.

`gnb feat/x`: fetch main, then branch off `origin/main`. a pointer, no commit yet.
two commits on the branch, a and b. `gp` the first time sets the upstream on its own.
meanwhile main moved on: a teammate's commit 3. `gcm`, `gl` brings it down, `gco feat/x` goes back.
`grbm` replays a and b on top of 3. same changes, new commits, history stays one line.
`gplease` pushes the rewritten branch. it refuses if `origin/feat/x` moved since your last fetch. `gpf!` would not.
`gm --no-ff feat/x` on main: a merge commit m joins the two lines. plain `gm` would fast-forward here: main just moves to b, no m.
`gp`. `origin/main` catches up, and the branch can go.

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`.[^merge] `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                                     | mark |
| -------------- | ------------------------------- | ------------------------------------------------ | ---- |
| `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.[^commit] 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.[^reset]

| alias        | expands to                                      | what it does                                                             | mark        |
| ------------ | ----------------------------------------------- | ------------------------------------------------------------------------ | ----------- |
| `gun`        | `git reset --soft HEAD~1`                       | undo the last commit. its changes stay staged                            | custom      |
| `gfix`       | `git commit --amend --no-edit`                  | fold what's staged into the last commit, same message                    | custom      |
| `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                                          |             |
| `grhh`       | `git reset --hard`                              | throw away every uncommitted change to tracked files                     | destructive |
| `groh`       | `git reset origin/$(git_current_branch) --hard` | make this branch match the remote. local commits and changes are dropped | destructive |

## 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                                   | mark   |
| -------------- | ----------------------------------------- | ---------------------------------------------- | ------ |
| `glast`        | `git show --stat HEAD`                    | the last commit, with the files it changed     | custom |
| `gfind "text"` | `git log --all --oneline --grep="<text>"` | search commit messages on every branch         | custom |
| `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.

> **interactive figure:** 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 `gp` sets one. no `-u origin <branch>`.[^push]
- my `.gitconfig` pages `gd` and `gds` through delta: `n` and `N` jump between files, `q` quits.[^delta]
- `gwip`, then switch branches: the work stays behind as a commit on the old branch. `gsave` keeps it off every branch until you `gpop` it.
- `ghelp <word>` filters the sheet in a terminal. try `ghelp force`, `ghelp stash`, or `ghelp undo`.

[^omz]: the [oh-my-zsh git plugin](https://github.com/ohmyzsh/ohmyzsh/blob/964ddf789b022b6958b27d02d7fa54f552de21e9/plugins/git/README.md), where every alias here that isn't one of mine comes from. the expansions are copied from its [source](https://github.com/ohmyzsh/ohmyzsh/blob/964ddf789b022b6958b27d02d7fa54f552de21e9/plugins/git/git.plugin.zsh).

[^commit]: [git commit](https://git-scm.com/docs/git-commit) in the git docs: `--all`, `--amend`, and `--no-edit`.

[^push]: [push.autoSetupRemote](https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushautoSetupRemote) and [push.default](https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault) in the git config docs.

[^pullrebase]: [pull.rebase](https://git-scm.com/docs/git-config#Documentation/git-config.txt-pullrebase) in the git config docs.

[^lease]: [--force-with-lease](https://git-scm.com/docs/git-push#Documentation/git-push.txt---force-with-lease) and [--force-if-includes](https://git-scm.com/docs/git-push#Documentation/git-push.txt---force-if-includes) in the git push docs, including the note on fetches that run in the background.

[^merge]: [git merge](https://git-scm.com/docs/git-merge#Documentation/git-merge.txt---no-ff) 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.

[^stash]: [git stash](https://git-scm.com/docs/git-stash) in the git docs: `push -u`, what `pop` does on a conflict, and `clear`.

[^reset]: [git reset](https://git-scm.com/docs/git-reset) in the git docs: the `--soft`, `--mixed`, and `--hard` modes, and `ORIG_HEAD`.

[^delta]: [navigation keybindings](https://dandavison.github.io/delta/navigation-keybindings-for-large-diffs.html) in the delta docs.
