You are on page 1of 32

Git Practices

Where to Begin?
Plumbing or Porcelain?
Neither!
For that, go to https://git‑scm.com.
Practices.
Not "Best"; not even "Good," just Practices.
Opening Questions
How do you enter a "flow" state?
Avoid interrupting yourself!
How do you use git effectively?
Learn your shell
Read some documentation
Incantation #1: Get most used git‑
related commands from history
# WARNING: works as-is in zsh,
# must be run on a single line to work in bash.

history | # get history


grep git | # filter history for only commands that co
awk '{CMD[$3]++;count++;}END { for (a in CMD)print CMD
column -t | # make columns, output as a table
sort -nr | # reverse-sort by number
nl | # print line numbers (gives us "ranks"); f
head -n10
Detour: Writing and Describing
Code
How We Write Code
Iterative/Procedural
"Stream of consciousness"
How We Describe Code
Feature‑level.
Decidedly not stream of consciousness
Mercurial and Git
Mercurial (hg)
Distributed version control, 2005, SHA‑1 hashes for
revisions
Git (git)
Distributed version control, 2005, SHA‑1 hashes for
revisions
Mercurial Philosophy: "History is permanent and
sacred."
Git Philosophy: ^^ definitely not that
command: git commit
git commit -m "commit message"
git commit --amend
command: git add
git add .
git add --all
git add -p
git checkout and time travel
git checkout -- {filename}

"restore {filename} to version in index"


git checkout -b {branchname}

"start a new branch based on current ref"


git checkout -

"point HEAD to previous HEAD ref"


git checkout {ref}; [...] git checkout -b
{newbranchname}

"point HEAD to {ref}." unbounded time travel and history


generation!
commit messages
good commits describe state transitions within the
code and communicate context around the change.
good commits answer the question why did the thing
change?
commit subject should be written in imperative tense:
"this commit will ______"
commit messages
separate subject and body w/ blank line
message explains what (subject) and why (body),
ideally not how
why should you care?
commit messages: why you should
care
: way less useful if commit messages are
choppy/unintentional
git blame

git revert: if you don't have well‑formed, intentional


commit messages, good luck using this.
git log: what if you've been on vacation and want to
know what happened in your project over the last two
weeks?
: potentially impenetrable in the absense
of meaningful commits with well‑crafted messages.
git bisect
git reflog
allows you to observe all changes (e.g.
commits, branches switching, merges, rebase
git reflog

minutia) from outside of context, in true chronological


order
entries older than 3 months (default, configurable) are
removed in git's periodic garbage collection process
(git gc)
did you make a horrible mistake? so long as it's
local/non public, reflog is a safety net
if you just need to see the public history for a branch,
don't use git reflog, just use some flavor ofgit log
Mutating History
Simple rule: don't mutate history on shared branches.
Above all else, if your repo is actively being
used by others, don't mutate master.
Your own branch: mutate your own commits for your
own current feature at will
Tips
Read the man pages (e.g. man git-commit).
Read well‑maintained code on GitHub. Read library
commit histories.
There's {insert your favorite library here}, React, the
linux kernel, even git itself.
Want super powers? Mix with https://git‑
scm.com/book/.
Discussion
Postscript: Why do we merge
master into feature branches so
often?
Continual merge of master into feature branches
considered harmful?
If you're also trying to rebase on top of master, yeah, it
will conflict quite a bit.

You might also like