Lesson 1: Why version control
Git began in 2005
Git was written for Linux. The Pro Git book explains that from 2002 the Linux kernel used BitKeeper, a proprietary tool owned by a company, and that in 2005 its free-of-charge status was revoked. The Linux community, Linus Torvalds above all, then built a tool of its own. Its goals included speed, a simple design, thousands of parallel branches and being fully distributed.
Why it fits here: It answers this topic's question: the Linux community, led by Linus Torvalds, wrote git in 2005 after BitKeeper stopped being free for them.
Sources: Pro Git: A Short History of Git; Wikipedia: Git
Topic: Why keep versions at all · history, people
93.87% use Git
The claim that nearly every developer uses git has a number behind it. In Stack Overflow's survey of 2022, the first to ask about version control, 93.87% of the 71,379 people who answered named Git. Among professional developers the share was 96.65%. The next answer, SVN, was named by 5.18%, and 4.31% used no version control at all.
Why it fits here: It backs this topic's claim with a number: in 2022, 93.87% of Stack Overflow's respondents used Git.
Source: Stack Overflow Developer Survey 2022
Topic: Why keep versions at all · numbers, tools
The stupid content tracker
Git's first README did not take its name seriously. Saved with the first version, in April 2005, it calls git the stupid content tracker and says the name can mean anything, depending on your mood. It might be a random three-letter word that no common Unix command used, or, when it works for you, global information tracker.
Why it fits here: It answers this topic's question: git's first README called it the stupid content tracker, or global information tracker on a good day.
Sources: git: the first README (commit e83c516); Wikipedia: Git
Topic: A repository: git init · history, words
Everything in .git
Delete the .git folder, and that copy of the project forgets its whole history. The git init manual says it creates an empty repository, basically a .git directory with subfolders for objects, refs/heads and refs/tags. The layout manual says objects is the repository's object store, where its contents are kept, and that the file HEAD names the branch you are on.
Why it fits here: It shows what this topic's hidden .git folder holds: the objects, the branches and HEAD, which together are the repository.
Sources: git manual: git-init; git manual: gitrepository-layout
Topic: A repository: git init · tools, standards
The index was once the cache
The staging area has had three names. Git's glossary lists cache as an obsolete word for the index, and the old word lives on in commands. That is why git status suggests git rm --cached, which the manual says removes a path only from the index and leaves your file alone. The git diff manual adds that --staged is a synonym of --cached.
Why it fits here: It answers this topic's question: cache is git's old word for the index, which is why --cached still appears.
Sources: git manual: gitglossary; git manual: git-rm; git manual: git-diff
Topic: The three places a change passes through · words, history
Three versions on one tray
The staging area usually holds one version of each file, but not always. Git's glossary says the index can also hold a second and even a third version, used when merging. The merge manual explains that for a file in conflict it records up to three: the version from the common ancestor, the one from your branch, and the one being merged in.
Why it fits here: It adds depth to this topic's staging area: during a merge it can hold three versions of one file.
Sources: git manual: gitglossary; git manual: git-merge
Topic: The three places a change passes through · tools, standards
50 characters, then a blank line
Commit messages have a shape. The git commit manual says it is a good idea to begin with one short line of no more than 50 characters, then a blank line, then a fuller description. Git treats that first line as the commit's title. On 19 April 2008 Tim Pope wrote a note on commit messages, adding that the rest should be wrapped at about 72 characters.
Why it fits here: It answers this topic's question: a subject of at most 50 characters, a blank line, then a body wrapped at about 72.
Sources: git manual: git-commit; Tim Pope: A Note About Git Commit Messages
Topic: Commits: git add and git commit · standards, tools
Write the subject as an order
The best subjects read like orders. The Linux kernel's guide to sending patches asks people to describe changes in imperative mood, as in make xyzzy do frotz, rather than makes xyzzy do frotz or I changed xyzzy. It says to write as if giving orders to the codebase to change its behaviour. Subjects such as Close gate 2 for repairs follow that rule.
Why it fits here: It sharpens this topic's message that says why: kernel developers write each subject as an order to the code.
Source: The Linux kernel documentation: Submitting patches
Topic: Commits: git add and git commit · standards, words
SHA-1 was broken in 2017
The names git gives are hard to fake, but not impossible. On 23 February 2017 Google announced the first SHA-1 collision: two different files with the same SHA-1, the method behind git's names. Git's manual says this attack, called SHAttered, showed a practical collision, and that from version 2.13.0 git uses a hardened SHA-1 that the attack cannot fool. Git still plans to move to a newer hash.
Why it fits here: It answers this topic's question: in 2017 two different files were given one SHA-1, and git now uses a hardened version.
Sources: Google Security Blog: Announcing the first SHA1 collision; git manual: hash-function-transition
Topic: Reading the history: git log · history, numbers
Seven characters, or more
Seven characters is a starting point, not a rule. The git config manual says that, unless set, the length of short names is worked out from roughly how many objects the repository holds, so that they stay unique for some time. The minimum is 4. So a large project shows longer short names than a small one.
Why it fits here: It refines this topic's 7 characters: git lengthens short names as a repository grows, so that they stay unique.
Source: git manual: git-config (core.abbrev)
Topic: Reading the history: git log · numbers, tools
Settings in layers
Git reads its settings from several files, one after another. The git config manual lists them: the system-wide file, then your own global ones, then the repository's own. It says the last value found takes precedence over those read earlier. So git config user.email in one repository, without --global, gives that project its own address, while your other projects keep yours.
Why it fits here: It adds to this topic's --global: a setting made inside one repository wins over your global one there.
Source: git manual: git-config (FILES)
Topic: Telling git who you are · tools, tricks
main by choice since Git 2.28
Git did not always let you choose the first branch's name. GitHub's notes on Git 2.28, written on 27 July 2020, say git init had always created a first branch named master. Version 2.28 brought the setting init.defaultBranch, and git init uses its value when it is set. That is why git config --global init.defaultBranch main makes every new repository start on main.
Why it fits here: It answers this topic's question: since Git 2.28, init.defaultBranch decides the first branch's name, which was always master before.
Source: GitHub Blog: Highlights from Git 2.28
Topic: Telling git who you are · history, tools
Lesson 2: The basic workflow
Staging half a file
git add can take part of a file. The git add manual says its -p option lets you choose hunks, the pieces of a change, between the desk and the tray, and add only the ones you pick. It gives you a chance to review each difference before it is staged. So two unrelated edits in one file can still go into two separate commits.
Why it fits here: It answers this topic's question: git add -p stages a file one piece at a time, so a commit holds only what is finished.
Source: git manual: git-add
Topic: The everyday cycle · tools, tricks
One commit per issue
The Pro Git book gives a rule for commits: make each one a logically separate changeset. It warns against coding all weekend on five different issues and then filing them as one massive commit on Monday. Even then, it says, use the staging area on Monday to split the work into at least one commit per issue.
Why it fits here: It shows why this topic's cycle stages only what is finished: each commit should hold one separate change.
Source: Pro Git: Contributing to a Project
Topic: The everyday cycle · standards, tools
diff since 1976
The program diff is decades older than git. In July 1976, J. W. Hunt of Stanford University and M. D. McIlroy of Bell Laboratories described its method in a technical report, An Algorithm for Differential File Comparison. The report says diff gives the differences between two files as a minimal list of line changes, enough to bring either file into agreement with the other.
Why it fits here: It shows how old this topic's diff is: its method was published by Hunt and McIlroy in 1976.
Source: J. W. Hunt and M. D. McIlroy: An Algorithm for Differential File Comparison
Topic: Reading a diff · history, people
Three lines of context
The three unchanged lines around each change have a purpose. Git's manual says diffs carry 3 lines of context by default, unless diff.context or the -U option says otherwise. The GNU diff manual gives the reason: patch, the program that applies a diff to another copy of a file, typically needs at least three lines of context to work properly.
Why it fits here: It answers this topic's question: three lines of context let the patch program apply a diff properly.
Sources: git manual: git-config (diff.context); GNU Diffutils manual: Unified Format
Topic: Reading a diff · numbers, tools
git restore arrived in 2019
git restore is younger than many git guides. Git 2.23's release notes say two new commands, git switch and git restore, were introduced to split jobs out of the single git checkout command. GitHub's notes on that release, dated 16 August 2019, call them a new pair of experimental commands. Before then, git checkout did both jobs, which is why older guides use it here.
Why it fits here: It shows how new this topic's git restore is: it came with Git 2.23 in 2019, taking a job from git checkout.
Sources: git: release notes for 2.23.0; GitHub Blog: Highlights from Git 2.23
Topic: Undoing before you commit: git restore · history, tools
Put it aside with stash
A change need not be kept or thrown away; it can wait. The git stash manual says stash records the current state of the desk and the tray, then takes the desk back to the last commit. The saved changes can be listed, and later applied again, even on top of a different commit. So git stash is a safe pause where git restore would lose the work.
Why it fits here: It answers this topic's question: git stash puts changes aside, where git restore would throw them away.
Source: git manual: git-stash
Topic: Undoing before you commit: git restore · tools, tricks
Half the same is a rename
Git decides afterwards whether a change was a rename. The git diff manual says a removed file and an added file count as a rename when they are similar enough, and that the default similarity is 50%. With -M90%, more than 90% of the file must be unchanged, and -M100% finds only exact renames. So a renamed file that was also lightly edited still shows as a rename.
Why it fits here: It answers this topic's question: by default, two files at least half alike count as a rename.
Source: git manual: git-diff
Topic: Removing and renaming: git rm and git mv · numbers, tools
git mv is a convenience
git mv does nothing that three commands cannot. The Pro Git book explains that git does not track file movement, and stores no note saying that a file was renamed. Renaming with mv, then git rm on the old name and git add on the new, gives the same result, because git works out the change by itself. The book calls git mv a convenience: one command instead of three.
Why it fits here: It shows what this topic's git mv really does: the same as mv, git rm and git add, in one step.
Source: Pro Git: Recording Changes to the Repository
Topic: Removing and renaming: git rm and git mv · tools, standards
Ready-made lists from GitHub
Nobody has to write a .gitignore from nothing. GitHub keeps a public collection of .gitignore templates, and its README says GitHub.com uses that list for the template chooser shown when you create a new repository or file. Its root folder holds templates for popular programming languages and technologies, and a folder named Global holds ones for editors, tools and operating systems.
Why it fits here: It answers this topic's question: GitHub's public collection of templates offers a starting .gitignore for most kinds of project.
Source: GitHub: gitignore templates (README)
Topic: Keeping files out: .gitignore · tools, internet
A global ignore file of your own
Some patterns belong to you, not to a project. The gitignore manual says patterns for files your own tools make, such as an editor's backups, generally go in the file named by core.excludesFile. Unless set otherwise, that file is ~/.config/git/ignore. Patterns there apply in every repository, and nobody else has to share them.
Why it fits here: It adds to this topic's .gitignore: patterns for your own tools go in one global file that every repository reads.
Source: git manual: gitignore
Topic: Keeping files out: .gitignore · tools, tricks
A tilde and a caret
HEAD~2 has a look-alike. Git's manual on naming revisions says a suffix ~n means the nth ancestor, following only first parents, so HEAD~3 is the same as HEAD^^^. A single ^ means the first parent, but ^2 means the second parent, which a merge commit has. So HEAD~2 and HEAD^2 look alike but name different commits.
Why it fits here: It sharpens this topic's HEAD~1: ~ counts back through first parents, while ^2 picks a merge's second parent.
Source: git manual: gitrevisions
Topic: Looking back: git show · tools, mistakes
One file, as it was
git show can open a single file as it was. The git show manual gives the example git show next~10:Documentation/README, which shows that file as it stood ten commits back on the branch next. In the same way, git show HEAD~2:orders.txt prints orders.txt as it was two commits ago, and changes nothing on the desk.
Why it fits here: It answers this topic's question: git show commit:file prints a file as it was in that commit.
Source: git manual: git-show
Topic: Looking back: git show · tools, tricks
Lesson 3: Branches
Git's killer feature
Branching is what many people praise git for. The Pro Git book says some call git's branching model its killer feature, the thing that sets it apart from other version control systems. In many older tools a branch meant making a new copy of the source code folder, which could take a long time. In git it is nearly instantaneous, so teams branch and merge often, even several times a day.
Why it fits here: It shows why this topic's branches matter: in git a branch is only a name, so making one is nearly instant.
Source: Pro Git: Branches in a Nutshell
Topic: What a branch is · tools, history
Not always a file
A branch is not always its own file. The git pack-refs manual says branch and tag tips were traditionally stored one file per ref under .git/refs. With hundreds or thousands of tags, that wastes space and slows git down, so pack-refs stores them all in one file, .git/packed-refs. A ref missing from the folders is looked up in that file instead.
Why it fits here: It answers this topic's question: many branches and tags can live together in one file, .git/packed-refs.
Source: git manual: git-pack-refs
Topic: What a branch is · tools, numbers
One branch for each change
GitHub's own way of working starts with a branch. Its guide calls GitHub flow a lightweight, branch-based workflow, used at GitHub even for its site policy, documentation and roadmap. The first step is to create a branch with a short, descriptive name, such as increase-test-timeout, so that others can see ongoing work at a glance. The branch is a space to work without affecting the default branch.
Why it fits here: It shows this topic's git branch in daily use: GitHub's own workflow begins every change on a new, well-named branch.
Source: GitHub Docs: GitHub flow
Topic: Making a branch · tools, standards
Why ~, ^ and : are banned
The rules for branch names have reasons. The git check-ref-format manual says they make names easy for shell scripts to read, keep the shell from expanding a name typed without quotes, and avoid confusing expressions. A double dot already means a range, as in ref1..ref2. A tilde and a caret already mean parents, and a colon already pairs two refs in fetch and push.
Why it fits here: It answers this topic's question: ~, ^ and : already have meanings in git's names for commits, so a branch cannot use them.
Source: git manual: git-check-ref-format
Topic: Making a branch · standards, words
@{-2} and the dash
The dash in git switch - is a shortcut. The git switch manual says @{-N} names the Nth last branch you switched to, and that - means the same as @{-1}. So git switch @{-2} goes back two switches. The manual adds that the dash is often used to move quickly between two branches, or to undo a switch made by mistake.
Why it fits here: It answers this topic's question: @{-2} goes back two switches, and the dash is the same as @{-1}.
Source: git manual: git-switch
Topic: Switching branches · tools, tricks
Carry the changes across
A switch that git refuses can be made to carry the changes along. The git switch manual says that with --merge, changes on the desk that clash with the other branch are stashed before the switch and put back afterwards. If they do not overlap with the differences between the branches, the switch goes ahead with no stash at all. A clash on putting them back leaves them saved in the stash list.
Why it fits here: It adds to this topic's refused switch: git switch --merge stashes the clashing changes and puts them back on the new branch.
Source: git manual: git-switch
Topic: Switching branches · tools, tricks
Two dots and three dots
A range can have three dots as well as two. Git's manual on naming revisions says A...B, the symmetric difference, is the set of commits reachable from either A or B but not from both: what each line has that the other lacks. With two dots, A..B lists only B's side. An end left out means HEAD, so origin.. asks what you have done since you forked from origin.
Why it fits here: It answers this topic's question: A...B lists the commits on either line but not on both, where A..B lists only B's.
Source: git manual: gitrevisions
Topic: Two lines of work · tools, standards
Drawing the lines
git log can draw the lines it lists. The git log manual says --graph draws a text-based picture of the history on the left of the output, and may print extra lines between commits so that the graph is drawn properly. With --oneline and --all as well, every branch appears as a line of its own, and you can see where lines split and join.
Why it fits here: It adds a picture to this topic's git log --all: --graph draws how the lines of work split and join.
Source: git manual: git-log
Topic: Two lines of work · tools, tricks
GitHub's button does not fast-forward
Many teams keep a record of every merge, even when a fast-forward would do. The git merge manual says --no-ff creates a merge commit in all cases, even when the merge could be a fast-forward. GitHub's default Merge pull request button works that way: its documentation says the pull request is merged using the --no-ff option, so the branch's commits arrive in a merge commit.
Why it fits here: It answers this topic's question: GitHub's default merge button uses --no-ff, so it makes a merge commit instead of fast-forwarding.
Sources: git manual: git-merge; GitHub Docs: About merge methods on GitHub
Topic: Bringing a line back: fast-forward · tools, internet
git-flow, and second thoughts
On 5 January 2010 Vincent Driessen published A successful Git branching model, the scheme that became known as git-flow and grew hugely popular. On 5 March 2020 Driessen added a note of reflection to it. The note says that a team doing continuous delivery of software should adopt a much simpler workflow, like GitHub flow, instead of trying to shoehorn git-flow into the team.
Why it fits here: It puts this topic's branches in a team setting: the author of git-flow now advises simpler branching for teams that release continuously.
Source: Vincent Driessen: A successful Git branching model
Topic: Bringing a line back: fast-forward · history, people
Gone, but remembered for a while
A deleted branch's commits do not vanish at once. Git keeps reflogs, records of when the tips of branches and other refs changed, and the git config manual says reflog entries expire after 90 days by default. Entries for commits that are no longer reachable from the current tip expire sooner, after 30 days. Until then, git reflog can usually find a commit lost with -D.
Why it fits here: It answers this topic's question: for at least 30 days by default, the reflog can usually still find a deleted branch's commits.
Sources: git manual: git-reflog; git manual: git-config (gc.reflogExpire)
Topic: Deleting and renaming branches · tools, numbers
Which branches are safe to delete
git branch can list the branches whose work is already in. The git branch manual says --merged lists only branches whose tips are reachable from HEAD, or from a commit you name, and --no-merged lists the others. Every branch that git branch --merged prints, apart from the one you are on, can be deleted with -d without losing work.
Why it fits here: It adds a check to this topic's -d: git branch --merged lists the branches whose work is already safe.
Source: git manual: git-branch
Topic: Deleting and renaming branches · tools, tricks
Lesson 4: Merging and conflicts
ort, Ostensibly Recursive's Twin
The ort in git's merge message is a name with a joke inside. The git merge manual says ort stands for Ostensibly Recursive's Twin, because it was written to replace the previous default method, recursive. GitHub's notes on Git 2.34, from 15 November 2021, say ort became the default in that release. They add that in testing ort consistently outperformed recursive, so merges got faster with fewer bugs.
Why it fits here: It answers this topic's question: ort stands for Ostensibly Recursive's Twin, git's default way of merging since Git 2.34.
Sources: git manual: git-merge (merge strategies); GitHub Blog: Highlights from Git 2.34
Topic: When both lines have moved on · history, words
Two merge bases at once
Sometimes two lines share more than one last commit. The git merge manual says that when ort finds more than one common ancestor, it first merges those ancestors into one tree and uses that as the base of the three-way merge. It adds that tests on real merge commits from the history of the Linux 2.6 kernel showed this gives fewer conflicts without causing wrong merges.
Why it fits here: It extends this topic's merge base: when two lines share several last commits, ort first merges them into one base.
Source: git manual: git-merge (merge strategies)
Topic: When both lines have moved on · tools, history
The octopus merge
A merge commit can have more than two parents. The git merge manual describes a strategy called octopus, which resolves cases with more than two heads and is the default when merging more than one branch at once. It refuses any complex merge that needs a person to resolve it, and is meant mainly for bundling several topic branches together in one commit.
Why it fits here: It answers this topic's question: the octopus strategy joins three or more lines in one merge commit.
Source: git manual: git-merge (merge strategies)
Topic: A merge that git finishes alone · tools, words
A message that lists the work
The plain message Merge branch can say more. The git config manual says the setting merge.log adds, after the branch names, one-line descriptions of the commits being merged. Its value is the most it will list, and true means 20. So after git config merge.log true, a merge message also shows the subjects of the work that came in.
Why it fits here: It adds to this topic's default message: merge.log lists the subjects of the merged commits in the merge message.
Source: git manual: git-config (merge.log)
Topic: A merge that git finishes alone · tools, tricks
A third version between the markers
A conflict can show where both sides began. The git config manual says the default style, merge, shows your side and the other side. The style diff3 adds a marker of seven bars and the original text before the = signs. A newer style, zdiff3, works like diff3 but pulls out lines that match on both sides. So git config merge.conflictStyle zdiff3 shows the base in every conflict.
Why it fits here: It answers this topic's question: the diff3 and zdiff3 styles add the original lines to each conflict.
Source: git manual: git-config (merge.conflictStyle)
Topic: When both changed the same line · tools, tricks
Markers of 32 characters
Seven characters is only the usual length of a marker. The gitattributes manual describes conflict-marker-size, which sets how long the markers are for a file. Its own example gives git's manual page on merging markers of 32 characters, instead of the usual 7. That page shows conflict markers as examples, so longer real markers keep the two apart.
Why it fits here: It adds to this topic's seven-character markers: the length can be changed per file, as git does for its own merge manual.
Source: git manual: gitattributes (conflict-marker-size)
Topic: When both changed the same line · tools, numbers
rerere remembers your answer
Settling the same conflict twice is a waste, and git has a cure with an odd name: rerere, reuse recorded resolution of conflicted merges. Its manual says it records a conflict and the way you resolved it by hand, then applies that answer when the same conflict comes back. It helps with long-lived branches that meet the same conflict again and again. It works once rerere.enabled is set.
Why it fits here: It answers this topic's question: with rerere switched on, git reuses your earlier resolution of the same conflict.
Source: git manual: git-rerere
Topic: In the middle of a merge · tools, words
A tool for each conflict
You need not settle conflicts in a plain editor. The git mergetool manual says it runs one of several merge programs to resolve merge conflicts, and is typically run after git merge stops. Such programs show your side, the other side and often the base next to each other, so a decision can be made by clicking. git mergetool then opens each unmerged file in turn.
Why it fits here: It adds a helper to this topic's unmerged files: git mergetool opens each one in a merge program.
Source: git manual: git-mergetool
Topic: In the middle of a merge · tools, tricks
A check for leftover markers
git add accepts a file that still holds markers, so the slip is easy to make. The git diff manual says the option --check warns if changes introduce conflict markers or whitespace errors. Running git diff --check before git add points at any marker left behind, with its file and line, before it can be committed.
Why it fits here: It answers this topic's question: git diff --check warns about conflict markers left in a file.
Source: git manual: git-diff
Topic: Resolving the conflict · tools, mistakes
ours and theirs can swap
--ours and --theirs do not always mean what they seem. The git checkout manual says they take stage 2, ours, or stage 3, theirs, of an unmerged file. It warns that during git rebase and git pull --rebase the two may appear swapped: --ours gives the branch being rebased onto, and --theirs gives the branch holding your work. Read the conflict before choosing a side.
Why it fits here: It sharpens this topic's --ours and --theirs: in a rebase their meanings swap, so check which side is which.
Source: git manual: git-checkout
Topic: Resolving the conflict · tools, mistakes
Commit or stash before you merge
--abort is not a perfect undo. The git merge manual says that if there were uncommitted changes on the desk when the merge started, git merge --abort will in some cases be unable to put them back. It therefore recommends always committing or stashing your changes before running git merge. A clean desk makes every abort exact.
Why it fits here: It sharpens this topic's --abort: changes left uncommitted before a merge may not survive an abort, so commit or stash first.
Source: git manual: git-merge
Topic: Backing out: git merge --abort · tools, mistakes
ORIG_HEAD, the way back
A merge that has finished can still be undone. Git's manual on naming revisions says commands that move HEAD in a drastic way, git merge among them, record where HEAD was before in ORIG_HEAD. That lets you put the branch's tip back as it was before the command ran. So git reset --hard ORIG_HEAD, right after a merge, takes the branch back to its old tip.
Why it fits here: It answers this topic's question: after a finished merge, ORIG_HEAD still names the old tip, so the merge can be undone.
Source: git manual: gitrevisions
Topic: Backing out: git merge --abort · tools, tricks
Lesson 5: GitHub and remotes
origin is only a habit
origin is a default, not a rule. The git clone manual says its -o option makes clone use another name instead of origin to keep track of the repository it copied, and that this overrides the setting clone.defaultRemoteName. So git clone -o hill address names the remote hill, and every command that would say origin then says hill.
Why it fits here: It sharpens this topic's origin: it is only the default name for a remote, and clone can give another.
Source: git manual: git-clone
Topic: A copy somewhere else · tools, words
A remote on a USB stick
A remote need not be on a server. The git clone manual says git also supports local repositories natively, written as a plain path such as /path/to/repo.git/ or as file:///path/to/repo.git/. So a bare repository on a USB stick can be a remote: git push sends commits to it, and git clone, git fetch and git pull read from it, with no network at all.
Why it fits here: It answers this topic's question: a remote can be any folder git can reach, even one on a USB stick.
Source: git manual: git-clone (GIT URLS)
Topic: A copy somewhere else · tools, tricks
No -u needed with autoSetupRemote
The -u of a first push can be made automatic. The git config manual says that when push.autoSetupRemote is true, a plain git push assumes --set-upstream for a branch that has no upstream yet. So after git config --global push.autoSetupRemote true, git push alone sends a new branch to the default remote and sets its upstream in the same step.
Why it fits here: It answers this topic's question: push.autoSetupRemote makes git push set the upstream by itself.
Source: git manual: git-config (push.autoSetupRemote)
Topic: Sending your work: git push · tools, tricks
simple, the safe default
A plain git push sends only the current branch, and that was a deliberate choice. The git config manual says the setting push.default decides what a push sends when none is named. Its mode simple pushes only the current branch. When pushing back to the remote you pull from, it pushes only to an upstream branch of the same name. That mode has been the default since Git 2.0, as the safest option for beginners.
Why it fits here: It explains this topic's plain git push: since Git 2.0 the simple mode sends only the current branch, to its own upstream.
Source: git manual: git-config (push.default)
Topic: Sending your work: git push · tools, history
A clone of the last commit only
A clone can leave the old history behind. The git clone manual says --depth creates a shallow clone, with a history cut to the given number of commits. So git clone --depth 1 address copies only the latest commit, which saves time and space with a large project. The manual adds that --depth implies --single-branch, so only one branch comes along.
Why it fits here: It answers this topic's question: git clone --depth 1 copies only the latest commit, not the whole history.
Source: git manual: git-clone
Topic: Copying a repository: git clone · tools, tricks
Every branch arrives, as a record
A clone shows one branch, yet holds them all. The git clone manual says it creates a remote-tracking branch for each branch in the repository it copies, visible with git branch --remotes, and checks out one branch of your own. So git branch alone may list only main, while git branch --remotes lists origin/main and every other branch of the source.
Why it fits here: It adds to this topic's clone: every branch of the source arrives as a remote-tracking branch such as origin/main.
Source: git manual: git-clone
Topic: Copying a repository: git clone · tools, standards
Only what is missing
A fetch downloads far less than a clone. The git fetch manual says it fetches branches and tags from another repository, along with the objects needed to complete their histories. Objects you already hold are not sent again, since each one is known by its name. So a fetch right after another, with nothing new on the remote, moves almost nothing at all.
Why it fits here: It explains why this topic's git fetch is quick: only the objects missing from your history come down.
Source: git manual: git-fetch
Topic: Bringing work down: git fetch · tools, numbers
FETCH_HEAD remembers the last fetch
Every fetch leaves a note behind. Git's manual on naming revisions says FETCH_HEAD records the branch you fetched from a remote with your last git fetch. So right after a fetch, git log HEAD..FETCH_HEAD lists what came down, even when the fetch named a branch that has no remote-tracking branch of its own.
Why it fits here: It adds to this topic's look before you take: FETCH_HEAD names what the last fetch brought down.
Source: git manual: gitrevisions
Topic: Bringing work down: git fetch · tools, standards
A force with a safety catch
When a force push is truly needed, there is a careful kind. The git push manual says --force-with-lease protects each remote branch about to be updated by requiring that its current value still matches your remote-tracking branch for it. If someone else has pushed since your last fetch, the push is refused instead of erasing their work.
Why it fits here: It answers this topic's question: --force-with-lease refuses to overwrite commits you have not seen.
Source: git manual: git-push
Topic: git pull, and when a push is refused · tools, mistakes
GitHub blocks force pushes for you
Hosting services can enforce the rule against forcing. GitHub's documentation says branch protection rules decide whether collaborators can delete or force push to a branch. By default, each branch protection rule disables force pushes to the matching branches and prevents them from being deleted. So a protected main cannot lose its history to a forced push.
Why it fits here: It backs this topic's never force a shared branch: on GitHub, a protected branch refuses force pushes by default.
Source: GitHub Docs: About protected branches
Topic: git pull, and when a push is refused · tools, internet
No passwords since 13 August 2021
GitHub set a date for the end of passwords in git. Its blog announced that beginning 13 August 2021, it would no longer accept account passwords when authenticating git operations on GitHub.com. The change covered command-line git and apps that use git to reach GitHub. Since then, a personal access token, or another kind of key, has taken the password's place.
Why it fits here: It answers this topic's question: GitHub has refused account passwords for git operations since 13 August 2021.
Source: GitHub Blog: Token authentication requirements for Git operations
Topic: GitHub · history, internet
GitHub stops a pushed secret
GitHub can catch a secret before it lands. Its documentation describes push protection, a secret scanning feature designed to prevent hardcoded credentials, such as secrets or tokens, from ever being pushed to a repository. Rather than warning after the leak, it blocks the push that contains them. It is a second guard, not a reason to stop checking your own commits.
Why it fits here: It backs this topic's rule that a token never goes into a commit: GitHub's push protection blocks pushes holding secrets.
Source: GitHub Docs: About push protection
Topic: GitHub · tools, internet
Lesson 6: Pull requests
100 lines, not 1000
Google's published guide for code review has a rule of thumb for size. It says there are no hard and fast rules, but that 100 lines is usually a reasonable size for a change, and 1000 lines is usually too large, leaving the final call to the reviewer. It adds that a change spread across many files counts as larger than the same number of lines in one file.
Why it fits here: It answers this topic's question: Google's guide calls 100 lines a reasonable change and 1000 lines usually too large.
Source: Google Engineering Practices: Small CLs
Topic: One branch for one change · numbers, standards
The pull request before the button
Asking someone to pull your work is older than GitHub's buttons. Git itself has a command for it, git request-pull. Its manual says it generates a request asking an upstream project to pull changes into its tree, printed to the screen, with a summary of the changes and where they can be pulled from. Such requests were sent by email, as projects like the Linux kernel still do.
Why it fits here: It shows where this topic's pull request comes from: git's own request-pull writes such a request for sending by email.
Sources: git manual: git-request-pull; The Linux kernel documentation: Submitting patches
Topic: One branch for one change · history, tools
Fixes #12 closes issue 12
A pull request can tidy up the to-do list. GitHub's documentation says a pull request can be linked to an issue by a keyword in its description or in a commit message: close, closes, closed, fix, fixes, fixed, resolve, resolves or resolved, followed by the issue's number. When the pull request is merged into the default branch, the linked issue closes by itself.
Why it fits here: It answers this topic's question: a keyword such as Fixes #12 in the description closes the issue when the pull request is merged.
Source: GitHub Docs: Linking a pull request to an issue
Topic: Opening a pull request · tools, tricks
Reviewers chosen by a file
A pull request can ask the right people by itself. GitHub's documentation says a file named CODEOWNERS defines the people or teams responsible for code in a repository. Code owners are automatically requested for review when someone opens a pull request that changes code they own, though not for a draft. So a change to the payments folder can reach its owners without anyone naming them.
Why it fits here: It adds to this topic's opening: a CODEOWNERS file asks the owners of the changed code to review automatically.
Source: GitHub Docs: About code owners
Topic: Opening a pull request · tools, standards
One business day at most
Google's guide for reviewers puts a number on speed. It says that if you are not in the middle of a focused task, you should review a change shortly after it comes in. One business day is the maximum time to respond to a review request, which in practice means first thing the next morning. A slow review holds up the author's whole line of work.
Why it fits here: It answers this topic's question: Google's guide asks reviewers to respond within one business day at most.
Source: Google Engineering Practices: Speed of Code Reviews
Topic: Reviewing a pull request · numbers, standards
What a review is for
Google's guide names the purpose of reviewing plainly. The primary purpose of code review, it says, is to make sure that the overall code health of the code base is improving over time. It warns reviewers not to wait for a perfect change: they should approve one that definitely improves the code, even if it is not perfect.
Why it fits here: It frames this topic's Approve: a reviewer approves a change that definitely improves the code, not only a perfect one.
Source: Google Engineering Practices: The Standard of Code Review
Topic: Reviewing a pull request · standards, people
The Update branch button
The merge from main can be done on GitHub too. Its documentation says a pull request page can update the branch when it is behind the base branch and has no merge conflicts. The button Update branch performs a traditional merge, and a menu beside it offers to update with a rebase instead. Conflicts must still be resolved before updating.
Why it fits here: It answers this topic's question: GitHub's Update branch button merges main into a waiting branch with one click.
Source: GitHub Docs: Keeping your pull request in sync with the base branch
Topic: Keeping a pull request up to date · tools, internet
Up to date before merging
Some repositories insist on this step. GitHub's documentation describes a branch protection setting, Require branches to be up to date before merging. With it on, a pull request's branch must be up to date with the base branch before it can be merged, so its tests run against the latest main. The documentation notes that this can mean more builds.
Why it fits here: It shows this topic's step enforced: a protected branch can refuse pull requests that are behind main.
Source: GitHub Docs: About protected branches
Topic: Keeping a pull request up to date · tools, standards
A squash leaves no merge behind
A squash merge is a merge in effect only. The git merge manual says --squash produces the desk and the tray as if a real merge happened, but does not make a commit, move HEAD or record MERGE_HEAD. The commit you make next is an ordinary one, with a single parent, whose effect is the same as merging the other branch.
Why it fits here: It answers this topic's question: --squash makes no merge commit; the next commit has one parent but all the branch's changes.
Source: git manual: git-merge
Topic: Merging the pull request · tools, standards
Rebase and merge adds no merge commit
GitHub's third method keeps every commit but no merge commit. Its documentation says that with Rebase and merge, all commits from the topic branch are added onto the base branch one by one, without a merge commit. The history of main then reads as one straight line, with each of the branch's commits in it.
Why it fits here: It completes this topic's three methods: Rebase and merge puts the branch's commits onto main one by one, with no merge commit.
Source: GitHub Docs: About merge methods on GitHub
Topic: Merging the pull request · tools, internet
Branches that delete themselves
The first tidying step can be left to GitHub. Its documentation says a repository can have head branches automatically deleted after their pull requests are merged, a setting that anyone with admin permission can switch on or off. With it on, only the steps on your own machine are left: git fetch --prune and git branch -d.
Why it fits here: It adds to this topic's tidying: GitHub can delete a merged pull request's branch on the remote by itself.
Source: GitHub Docs: Managing the automatic deletion of branches
Topic: After the merge: tidying up · tools, internet
Restore branch
A branch deleted on GitHub can come back. Its documentation says you can restore the head branch of a closed pull request. Open the pull request from the list of closed ones, and near the bottom of its page, click Restore branch.
Why it fits here: It answers this topic's question: a closed pull request's page has a Restore branch button.
Source: GitHub Docs: Deleting and restoring branches in a pull request
Topic: After the merge: tidying up · tools, tricks
Lesson 7: Good habits
More than one author
A commit has one author, but its message can credit more. GitHub's documentation says to add one or more Co-authored-by trailers, lines at the end of the message, to attribute a commit to several authors. Each line names one co-author with a name and the email address that person uses. GitHub then attributes the commit to all of them.
Why it fits here: It answers this topic's question: Co-authored-by lines at the end of a message credit more than one author.
Source: GitHub Docs: Creating a commit with multiple authors
Topic: Commit messages that explain · tools, people
A type in front of the title
Some projects give every title a type. The Conventional Commits specification asks for messages shaped as a type, an optional scope in brackets, a colon and a description. A fix patches a bug, and a feat adds a new feature. The specification lists its reasons: tools can then generate change logs and work out the next version number from the types of the commits.
Why it fits here: It shows a stricter form of this topic's titles: Conventional Commits puts fix: or feat: in front, so tools can read the history.
Source: Conventional Commits 1.0.0
Topic: Commit messages that explain · standards, tools
bisect finds the guilty commit
Small commits pay off when a bug appears. The git bisect manual says it uses a binary search to find which commit in a project's history introduced a bug. You mark one commit bad and an older one good, and git checks out commits between them for you to test. Its own example has 675 commits left to test, roughly 10 steps. A small commit then points at the exact change.
Why it fits here: It answers this topic's question: git bisect finds the commit that brought in a bug, and a small commit pins it down.
Source: git manual: git-bisect
Topic: Small commits, one change each · tools, numbers
The diff below the message
Writing the message is easier with the change in view. The git commit manual says -v, for verbose, shows the diff between the last commit and what would be committed at the bottom of the message template, to remind you what the commit holds. The diff is not saved into the message. A diff too long to describe in one title is a sign the commit holds too much.
Why it fits here: It adds a check to this topic's one change: git commit -v shows the whole diff while you write the title.
Source: git manual: git-commit
Topic: Small commits, one change each · tools, tricks
Releases stand on tags
GitHub's Releases page is built on tags. Its documentation calls releases deployable software iterations that you can package and offer to a wider audience to download and use. It says releases are based on git tags, which mark a specific point in a repository's history. A tag's date may differ from the release's date, since the two can be made at different times.
Why it fits here: It answers this topic's question: every GitHub release stands on a git tag that marks the released commit.
Source: GitHub Docs: About releases
Topic: Naming the commits that matter: tags · tools, internet
A name for every commit after a tag
A tag also gives later commits a readable name. The git describe manual says it finds the most recent tag reachable from a commit, then adds the number of commits on top of it and a short name of the latest commit. Its example, typed at Linus Torvalds's own prompt, prints v1.0.4-14-g2414721: 14 commits after v1.0.4, at commit 2414721.
Why it fits here: It shows a use of this topic's tags: git describe names any commit by its distance from the last tag.
Source: git manual: git-describe
Topic: Naming the commits that matter: tags · tools, people
reset, restore and revert
Git's own manual admits the three names are easy to mix up, and sorts them out. It says git revert makes a new commit that reverts the changes of other commits. The command git restore restores files on the desk from the tray or from another commit, and does not change your branch. The command git reset moves your branch's tip to add or remove commits, which changes the history.
Why it fits here: It answers this topic's question: revert adds an undoing commit, restore fixes files, and reset moves the branch.
Source: git manual: git (Reset, restore and revert)
Topic: Undoing a change: revert and reset · tools, words
Reverting a merge needs a side
A merge commit is harder to revert than an ordinary one. The git revert manual says you usually cannot revert a merge, because git does not know which side of the merge should be treated as the main line. The option -m with a parent number, starting from 1, names that side, and the revert then undoes the change relative to that parent.
Why it fits here: It adds a case to this topic's git revert: undoing a merge commit needs -m to say which parent is the main line.
Source: git manual: git-revert
Topic: Undoing a change: revert and reset · tools, mistakes
stash@{2.hours.ago}
The names of stash entries come from the reflog. The git stash manual says the latest stash is stored in refs/stash, and older ones are found in the reflog of that ref. So stash@{0} is the newest entry and stash@{1} the one before. The usual reflog syntax works too, so stash@{2.hours.ago} names the stash as it was two hours ago.
Why it fits here: It answers this topic's question: stash entries use reflog names, so stash@{2.hours.ago} works.
Source: git manual: git-stash
Topic: Setting work aside: the stash · tools, tricks
A stash that no longer fits
A stash can wait so long that it no longer applies cleanly. The git stash manual offers git stash branch for that case. It creates and checks out a new branch starting from the commit where the stash was made, applies the stashed changes there, and drops the entry if that works. It is useful, the manual says, when the branch has changed so much that git stash apply fails with conflicts.
Why it fits here: It adds a rescue to this topic's git stash pop: git stash branch puts an old stash back on the commit it came from.
Source: git manual: git-stash
Topic: Setting work aside: the stash · tools, tricks
Tokens that say what they are
GitHub's tokens are built to be spotted. Its blog explained on 5 April 2021 that each token starts with a three-letter prefix, gh and a letter for the type, such as ghp for a personal access token, followed by an underscore. A 32-bit checksum in the last 6 characters lets a scanner reject fake matches. So a leaked token is easy for secret scanners to find.
Why it fits here: It answers this topic's question: the ghp_ prefix and a checksum let scanners find leaked GitHub tokens.
Source: GitHub Blog: Behind GitHub's new authentication token formats
Topic: Secrets never go in · tools, internet
Not filter-branch
Removing a secret from history needs care, and git's own manual names the wrong tool. The git filter-branch manual opens with a warning: the command has a plethora of pitfalls that can mangle the history it rewrites, and poor performance too. It asks people to use another history filtering tool, such as git filter-repo. Rotating the secret still comes first.
Why it fits here: It sharpens this topic's clean-up: git's own manual warns against filter-branch and points to git filter-repo.
Source: git manual: git-filter-branch
Topic: Secrets never go in · tools, mistakes
No fact matches. Try a shorter word, or take the tag off.