Pro Git

by
Last Modified: 2025-12-19

A revisitation of a good book.

I made a couple naïve attempts at trying to master git circa 2024.

After obtaining the UGrad, I have had a couple months of unemployment to improve my Software Engineering skills.

As such I read more than a small selection of this book and produced the following flashcards1:

"Deep Work: Professional activity performed in a state of distraction-free concentration that pushes your cognitive capabilities to their limit." —Cal Newport

Notes

Flashcards

Is it true that git add is a multi-purpose command? if so, what are some things it can do?   progit

True. it can track new files, stage files, and mark merge-conflicted files as resolved.

it may also be able to do more things.

What is the checksumming algorithm used by Git? What is the length of the values? What are the value strings composed of?   progit

SHA1 hash. 40 length. Hex; i.e. 0-9 + a-f

What are the three main states files can reside in?   progit

  1. Modified (changed in your working directory)
  2. Staged (in the index, ready for commit)
  3. Committed (saved in the repository / HEAD history)

What does the staging area look like?   progit

it is a file called index.

What are the three scopes of git config and where are each of the configuration files stored?   progit

  • --system | all users: /etc/gitconfig
  • --global | current user; all repos: ~/.gitconfig or ~/.config/git/config
  • --local= | .git/config per repo control

Does /etc/gitconfig trump .git/config?   progit

No. local trumps global which trumps system. This is also the case for configuration files more generally.

What command can you use to check the git configuration settings? What about a specific key?   progit

git config --list

git config <key> ; where key can be something such as user.name or user.email, core.editor, etc.

What are the two types of files from gits' perspective? What kind of files belong in each type?   progit

  1. tracked: things previously staged; newly staged files.

    • can be modified, unmodified or staged
  2. untracked: everything else. the stuff git doesn't know about.

How to git add recursively?   progit

trick question; git add automatically adds directories recursively.

What does git add do?   progit

it begins tracking the file and stages it for committing. it is multi-purpose and can do even more. think of it as "add precisely this content to the next commit"

How to get a short git status? what do the LHS columns mean here?   progit

$ git status -s
M README
MM Rakefile
A lib/git.rb
M lib/simplegit.rb
?? LICENSE.txt
Back

git status --short or -s.

  • m : modified
  • mm : modified, staged, modified
  • a : added to staging
  • ?? : untracked

What files do *.[oa] and *.~ ignore in the .gitignore?   progit

files ending with .o OR .a and files ending with .~.

How do you write comments in .gitignore file? how do you specify a directory to ignore?   progit

#

end the directory name with /

note, unless you add the prefix slash, it will ignore that named directory at all levels! the prefix slash means 'not recursively'

What are glob patterns?   progit

Front

What do these ones do?

  • *
  • [abc]
  • ?
  • [0-9]
  • a/**/z
Back

they are simplified regular expressions.

  • * : matches zero or more characters.
  • [abc] : matches any character inside the brackets
  • ? : a single character
  • [0-9] : matches any character between the range
  • a/**/z : nested directories; matches a/b/c/z, a/z, a/b/z, etc.

What do the following instructions do in a .gitignore file?   progit

  • *.a
  • !lib.a
  • /TODO
  • build/
  • doc/*.txt
  • doc/**/*.pdf
Back
  • ignore all *.a files
  • but track lib.a
  • only ignore TODO file in current directory. not subdir/TODO. recall that prefix / avoids recursivity
  • ignore all files in any directory named build
  • ignore doc/notes.txt, but not doc/server/notes.txt
  • ignore all .pdf files in doc/ and subdirs

Can a repo only have 1 .gitignore file?   progit

no, it can have one in each subdirectory if required.

What does git diff do?   progit

it compares what is in your working directory with what is in your staging area.

How can you use git diff to compare your staged changes to the last commit?   progit

git diff --staged

What is the difference between git diff --staged and git diff --cached? What do they do?   progit

they're synonyms!

shows the diff between last commit and what is staged

What does -v do in git commit?   progit

adds a git diff to the commit popup so you can see exactly what you are committing.

How can you skip the staging area? What is there to be careful of?   progit

git commit -a -m "<message>"

this will only commit the files that were already being tracked. no new files.

How do you remove a file from git? precisely what does the command for this do?   progit

you must remove the file from the tracked files (staging area) and then commit.

git rm does this and also deletes the file from the working directory (so it's not "unstaged" later)

When should you do the -f option for git rm?   progit

if you modified the file or added it to the staging area.

it's a safety feature to prevent removal of files that aren't in a snapshot.

How to remove a file from staging area, but keep it in working directory?   progit

git rm --cached <file>

What order does git log show commits in?   progit

reverse chronological order – most recent commits first.

What does git log -p -2 do?   progit

shows only last 2 commits with diff (or patch) details

What do git log --stat and git log --pretty do? what values can pretty take?   progit

--stat prints a list of modified files below each entry. how many such files, and how many lines were changed.

--pretty has options: oneline, short, full, fuller, format: _, etc.

What does git log -n do where n is integral? How can you get commits made in the last 2 weeks?   progit

shows last 2 commits.

git log --since=2.weeks

also, --until is useful too.

dates can be "2008-01-15" or even relative: "2 years 1 day 3 minutes ago"

What git log flag is commonly referred to as the pick axe? What does this pickaxe do?   progit

git log -S<needle> (the “pickaxe”) shows commits where the number of occurrences of <needle> changes (added/removed).

(Note: git log -s just suppresses diff output.)

How can you filter in git log based on path? where in the command should this filter go?   progit

at the very end, separated by -- to segregate from the other options: git log -- path/to/file

What flag allows you to declutter the log history from merge commits when using git log?   progit

--no-merges

How can you fix a commit message / add an extra file to your commit (that hasn't been pushed yet)?   progit

Use git commit --amend.

  • To fix the message only: git commit --amend -m "new message" (or run without -m to edit in your editor)
  • To add a forgotten file: git add <file> then git commit --amend --no-edit

Amending rewrites the last commit: it creates a new commit with a new hash (the old commit becomes unreachable unless referenced). Avoid amending if the commit has already been pushed/shared (or you’ll need a force push).

What does git checkout -- <file> do? Are there perils?   progit

It discards working-tree changes to <file> by replacing it with the version from the index (staging area). If the file is not staged, that effectively means “restore from =HEAD=​”.

Perils:

  • It can permanently lose local edits in your working directory (not just “unstage”).
  • It does not create a commit or safety net.

Safer / modern equivalents:

  • git restore <file> (restore working tree from index)
  • git restore --source=HEAD <file> (force restore from last commit)

Which 2 commands does git restore replace?   progit

git reset HEAD <file> and git checkout -- <file>

Which 2 commands does git reset replace?   progit

(For the “unstage” use-case) git reset replaces:

git reset HEAD <file>

Because git reset <file> defaults to HEAD, so:

git reset <file>

is the shorter equivalent.

Note: git checkout -- <file> is a different operation (discard working-tree changes) and is now commonly written as:

git restore <file>

What is the command to add a new remote with shortname and URL?   progit

git remote add <shortname> <url>

What does git fetch <remote> do?   progit

it just downloads the data from whichever remote is the argument, and places it in the .git folder. no merging occurs.

What does git reset HEAD <file> do?   progit

It resets the file in the index to match HEAD (unstages it) while leaving the working directory version unchanged.

How to see the URLs of all the remotes? What about a single remote?   progit

git remote -v

OR for more info on a single remote: git remote show origin

How can you list tags? How can you (optionally filter them)?   progit

git tag

with optional -l that respects globs -l ""

What are the 2 types of tags? What do they do?   progit

lightweight: pointer to a specific commit

annotated: full objects. checksummed; contain metadata: tagger name, email, date, message.

How do you push tags? how can you delete them?   progit

git push <remote> <tagname>

or for lots: git push <remote> --tags

delete: git tag -d <tagname> (which does so locally) followed by git push <remote> --delete <tagname>

How can you create an annotated tag with message? how can you tag an old commit? how do you see tag data?   progit

git tag -a <tagname> -m ""

git tag -a <tagname> <commit_hash>

git show <tagname>

When is rebasing a bad idea?   progit

when other people have based work on your commits.

do not rebase those.

How can you rebase server branch onto master without checking out server first?   progit

git rebase master server

"replay the commits onto master from server branch"

What does git rebase master do? Assuming you're on the experiment branch?   progit

/doc/org/flashcards/soft-eng/img/rebase1.png

Back

goes to common ancestor; gets diff of every commit – saves them into a temp file; resets experiment branch to same commit as master. applies each patch.

you will then need to fast-forward by checking out master and merging experiment into it.

/doc/org/flashcards/soft-eng/img/rebase2.png /doc/org/flashcards/soft-eng/img/rebase3.png

What are the commands for a basic rebase? How are they different to a merge?   progit

rebase: git checkout experiment git rebase master "go to experiment branch, apply patches to master"

merge: git checkout master git merge experiment "merge changes into current branch"

What does rebasing do?   progit

takes all the changes committed on one branch and replays them on another branch

/doc/org/flashcards/soft-eng/img/rebase2.png

What is a small gotcha vis-a-vis the git branch -vv command and the information it returns?   progit

it's telling you information about what we have cached from the last fetch. we are better off running git fetch --all; git branch -vv

What does the -vv flag do to git branch?   progit

shows what tracking branches have been set-up.

lists out local branches with more information; what each branch is tracking and if the local branch is ahead, behind or both.

What is @{u} short for? What is an alternative short-form for this? What does git merge @{u} do?   progit

short for the upstream url. hence @{upstream} is another short-form.

git merge @{u} merges the current branches' upstream into the current branch.

If you already have a local branch and want to set it to a remote branch you just pulled down, or want to change the upstream branch you're tracking, the command is {{c1:: git branch -u origin/<branch>}}   progit

What is git checkout serverfix an alias for, where you don't have the remote serverfix branch?   progit

git checkout --track origin/serverfix which itself is shorthand for git checkout -b serverfix origin/serverfix

When you git fetch from a remote (git fetch origin), does that give you editable copies of the new branches?   progit

no, only unmodifiable pointers.

you need to run git merge origin/<branch> which pulls in the changes into your current branch. to make a new branch, use git checkout -b serverfix origin/serverfix

How to push a branch up to a particular remote?   progit

git push -u <remote> <branch>

This pushes the local branch to the given remote and sets its upstream, so that later you can just run git push on that branch.

Creating a branch does not automatically push it; you must push it at least once as above.

How to pull changes down from a certain remote?   progit

git fetch <remote>

How can we get a full list of remote references?   progit

git ls-remote <remote>

OR

git remote show <remote>

How do you rename a branch? Are there perils?   progit

git branch -m <old> <new> (rename branch locally)

git push -u origin <new> (push new branch name + set upstream)

git push origin --delete <old> (delete old branch from the remote)

Perils: if others are using the old branch name (locally or tracking origin/<old>), renaming/deleting it will break their setup until they rename or re-point their branches.

What does git branch --no-merged master do?   progit

shows you which branches have not been merged into master. You can give the final argument to see info on a branch you don't have checked out.

What do the --merged and --no-merged options do on git branch?   progit

shows you which branches are already merged into the branch you're on.

--no-merged shows the branches that have work that you have not merged in.

What happens when you run git branch with no arguments? What about if you add -v?   progit

naked is a simple listing.

 git branch
  iss53
,* master
  testing

note that * tells you what is currently checked out! (i.e. where the HEAD points)

-v also shows the last commit on each branch:

$ git branch -v
  iss53 93b412c Fix javascript issue
,* master 7a98805 Merge branch 'iss53'
  testing 782fd34 Add scott to the author list in the readme

How can you solve merge-conflicts?   progit

edit the files and choose the correct code.

git add and git commit (staging the file marks it as resolved). you could also use git mergetool

What happens when you do a git merge and the commit of the branch you're on is not a direct ancestor of the branch you're merging in?   progit

simple three-way merge. uses the two snapshots pointed to by the branch tips and the common ancestor of the two. /doc/org/flashcards/soft-eng/img/three-way-merge.png

/doc/org/flashcards/soft-eng/img/merge-commit.png

How to merge hotfix branch into main?   progit

git checkout main

git merge hotfix

note if you can fast-forward then you should delete the hotfix branch after merging: git branch -d hotfix

What is "fast-forward"?   progit

When the merge has no divergent work to merge together it just moves the branch pointer forward

/doc/org/flashcards/soft-eng/img/rebase2.png /doc/org/flashcards/soft-eng/img/rebase3.png

How can you create a new branch and switch to it in the same command?   progit

git checkout -b <new_branch_name>

alternatively you can now use git switch with --create / -c for a new branch.

note git switch - changes to the last branch.

/doc/org/flashcards/soft-eng/img/change-head.png

Is it expensive to create and delete branches? Explain.   progit

No, the pointers are just simple text files with the 40 character SHA1-hash checksums of the commit it points to.

41 bytes cost (+ newline)

Which branches does git log show branches for? How can you modify this behaviour?   progit

git log <branch_name> shows logs for an arbitrary branch git log --all shows for all branches but by default, it's whatever HEAD is pointing to.

What is the command to switch an existing branch? How can you create a new branch?   progit

git checkout <branch_name>

git branch <branch_name>

/doc/org/flashcards/soft-eng/img/new-branch.png

What command can we use to see where the branch pointers are pointing? i.e. HEAD   progit

git log --oneline --decorate

/doc/org/flashcards/soft-eng/img/head-branch.png

How does Git know what branch you're on?   progit

Keeps a special pointer called HEAD that is a pointer to the local branch

/doc/org/flashcards/soft-eng/img/head-branch.png

How many pointers are in a commit object?   progit

  • 0 for the initial commit (no parents)
  • 1 for regular commits
  • multiple (2?) for merge commits

/doc/org/flashcards/soft-eng/img/commit-parents.png

What happens when you make a commit? What does Git store?   progit

stores a commit object with a pointer to the snapshot. also contains metadata + pointer/s to directly previous commits.

/doc/org/flashcards/soft-eng/img/commit-tree.png

A branch in Git is simply a lightweight, movable {{c1::pointer}} to a commit.   progit

When does a file get a checksum? What does this imply has happened?   progit

when the file is staged.

implies a version of the file has been stored. (called blobs)

Why should you write git commit messages in the imperative form? What does imperative form mean?   progit

Write "Fix bug", not "Fixes bug" or "Fixed bug".

The convention matches with commit messages generated by commands like git merge and git revert.

How can you condense git commit -a -m ""? Is this true for all flags?   progit

Yes: you can combine short flags, so:

git commit -am "msg"

is the same as:

git commit -a -m "msg"

But no, this is not true for all flags. Only single-letter short options can be grouped like this, and only when they don’t require their own attached value.

Examples:

  • OK: -am (equivalent to -a -m; -m takes its message as the next argument)
  • NOT like this: you generally can’t combine long options (e.g. --amend) or assume every short flag combination is valid.

What does the three-dots syntax do? git log --no-merges issue54...origin/master   progit

The three-dots range A...B is the symmetric difference between the two branches.

git log --no-merges issue54...origin/master

shows commits that are reachable from either issue54 or origin/master, but not from both — i.e. commits that are unique to one side or the other.

(You can add --left-right to see which side each commit comes from.)

What is the -u flag short for? git push -u origin featureB:featureBee   progit

-u is short for --set-upstream.

In:

git push -u origin featureB:featureBee

  • origin is the remote.
  • featureB:featureBee uses the <src>:<dst> syntax:

    • featureB is the local branch.
    • featureBee is the remote branch name on origin.

The -u / --set-upstream flag makes the local branch featureB track origin/featureBee, so later you can just run git push or git pull with no extra arguments.

What does git merge --squash featureB do?   progit

It takes all the changes introduced by branch featureB (since the merge-base with your current branch) and applies them to your working tree/index as one combined change, without creating a merge commit and without recording the branch history.

After running it:

  • the changes are staged (like a big “combined” diff)
  • you still need to run git commit to create the single squashed commit
  • it does not mark featureB as “merged” in Git history (a later normal merge may try to reapply commits).

What does git am do?   progit

"_a_​pplies a series of patches from a _m_​ailbox"

reads on mbox file, but can also accept a .patch file.

What does git log --pretty =fuller -1 do?   progit

shows last commit with fuller formatting.

What does the --not option do here:   progit

git log <branch> --not master

Back

excludes commits on the master branch.

synonymous to git log master...branch

What option can pass to git log to get the diff introduced by each commit?   progit

-p

How to do a diff between the last commit of the branch you're on and its common ancestor with another branch?   progit

On your feature branch, to see what this branch has introduced since it forked from master:

git diff master...

This uses the three-dots syntax:

  • In general, git diff A...B means: diff between merge-base(A, B) and B (changes on B since the common ancestor).
  • So git diff master... (or git diff master...HEAD) shows commits that are on your current branch but not on master, ignoring changes that only master has.

(You can also write git diff master...branch if you want to compare explicitly to branch.)

How to explicitly find the common ancestor of two branches contrib and master?   progit

git merge-base contrib master

What is cherry-picking? What does the usage look like?   progit

it is like a rebase, but for a single commit.

git cherry-pick <commit_hash>

(cherry picks that commit into current branch)

What does git describe <branch> do?   progit

Gives a human-readable string to describe a commit.

What does git shortlog --no-merges master --not v1.0.1 do?   progit

gives a short log of all the changes since the last release of v1.0.1

How to clone a repository? How to specify a path to clone into?   progit

git clone <remote> <optional_path>

What is a bare repository? How can you create one?   progit

a Git repository with no working directory.

git clone --bare <source>

git init --bare <directory>.git

the .git part is convention for bare repositories.

What does ssh-keygen do? What does adding the -o flag do?   progit

Generates a public and (corresponding) private key.

-o saves the private key in a format more resistant to brute-force attacks. (use this option if setting a password)

What are some pros and cons of setting up Git on your own webserver?   progit

pros:

  • lots of control
  • run everything from within your own firewall
  • some organisations might not allow code on another server

cons:

  • time to set-up
  • maintainence efforts

What are the 4 distinct protocols Git can use to transfer data?   progit

local, http, secure shell (ssh), git.

How do you check the open issues in a repository with gh?   progit

gh issue list --state open

How do you see the descriptions of a particular Github issue using gh?   progit

gh issue view <num>

Is Github a part of Git?   progit

No, it is a Git host. A place where some functionalities become simpler.

How does Github map commits to your user?   progit

by email address

Do forks exist as part of Git or Github? How about pull-requests?   progit

Forks and pull requests are not core Git features; they are hosting-platform features (e.g., GitHub/GitLab/Bitbucket).

In Git itself, you only have repositories and remotes; a “fork” is just another repo you can add as a remote, and a “pull request” is a workflow built on Git commits/branches plus platform review + merge tooling.

Is it true that often pull requests are created at the beginning of a project? If so, why?   progit

True. Because you can keep pushing to the topic branch even after the pull request is opened.

How can you update a pull request with new code?   progit

Commit to the topic branch and push. The pull request gets updated automatically.

How can you solve "Pull request does not merge cleanly"?   progit

/doc/org/flashcards/soft-eng/img/pull-request-not-merge-cleanly.png

Back

two main options:

  1. rebase your branch on top of whatever the target branch is
  2. you can merge the target branch into your branch (preferred)

In Github, all issues and pull requests are assigned {{c1::numbers}} and they are {{c1::unique}} within the project   progit

How can you reference issues/pull requests from comments? What are the three syntaxes?   progit

  • same scope (i.e. same fork): #<num>
  • another fork: username #<num>
  • different repo: username/repo#<num>

What happens at the bottom of an issue or pull request if someone links to it with the #<num> syntax?   progit

Github create a 'trackback' event

In addition to linking to issue numbers in Github, you can also reference a specific {{c1::commit}} by the {{c1::full 40 character SHA-1 hash}}.   progit

What is a really cool aspect of the checkbox syntax in Github flavoured markdown?   progit

- [X] write the code
- [ ] write all the tests
Back

simply clicking the checkbox updates the comment – there is no need to manually edit the file!

How can task lists be used in Issues and Pull requests on pages that list these out?   progit

A little icon with their progress appears beside the headings.

How to write emojis in Github?   progit

I :eyes: that :bug: and I :cold_sweat:.
:trophy: for :microscope: it.
:+1: and :sparkles: on this :ship:, it's :fire::poop:!
:clap::tada::panda_face:

Pull requests can either come from a {{c1::branch}} in a {{c1::fork}} of your repository or they can come from {{c1::another branch in the same repository}}.   progit

What is the command to merge in a remote branch without having to add a remote?   progit

git pull <URL> patch-1

here patch-1 is the branch.

Technically speaking, what does this do?   progit

$ curl https://github.com/tonychacon/fade/pull/1.patch | git am
Back

merges in the pull request.

Is ls-remote a plumbing command or porcelain command? What happens if you run it on a repository?   progit

plumbing.

gives a list of all the branches and tags and other references in the repo.

What does the CONTRIBUTING file do on Github?   progit

It shows contributors the rendered version of the file when they start opening a pull request.

What is this an example of?   progit

/doc/org/flashcards/soft-eng/img/audit-log.png

What kind of entities have this functionality in Github?

Back

Audit log.

Github organisations.

What are the 2 main systems for scripting Github?   progit

  1. Hooks and Services
  2. API

/doc/org/flashcards/soft-eng/img/service-hooks.png

What are the rate limits to Github when authenticated vs. when not?   progit

5,000 vs. 60.

Is Curl + HTTP requests the best way to interface with the Github API?   progit

There is never a "best way", but other languages like Go, .NET, Objective-C and Ruby do have more idomatic frameworks for the API.

What to do when you do a git init followed by a git add -A of many large files?   progit

git rm -r --cached (unstages everything)

git gc --prune=now (deletes blobs)

What does git blame do?

for each line in a file, it shows the last commit that modified it: who made the change, when, and which commit it was.

e.g. git blame path/to/file.py

What does the syntax git show commit:file do?

It shows the contents of file as it was in commit (or any commit-ish), without changing your working tree or HEAD.

e.g. git show HEAD~2:src/main.py prints how src/main.py looked two commits ago.

Footnotes


1

note that the formatting of these cards is not guaranteed; they are the source files for my anki-editor workflow.