Orientation
A git repository is initialized in a directory (the repository root) and records snapshots of the files you choose to track under that directory.
Each commit stores a snapshot of the tracked files as staged, plus metadata including timestamp, message, and the hash(es) of its parent commit(s). Usually a commit has a single parent, but the first commit has none, and a merge commit has two or more. Commits linked by parents form a directed acyclic graph (DAG) of history.
A commit is identified by a hash of all of that content. Since the hash covers the parent hashes, it includes not just the commitβs data but also its position in the graph. Commits are therefore immutable: amending, rebasing, or cherry-picking creates new commits with new hashes.
It is generally safe to make history-changing graph modifications; the original commits remain in the reflog until garbage collected. However, since rewriting an old commit means rewriting all its descendants, pushing revised history to others requires them to reconcile diverged paths.
Referring to commits
HEAD is the current commit. git show HEAD
HEAD^ is the (first) parent commit of HEAD. The first parent of a merge commit is the branch you were on when you merged. main^2 is the second parent of a merge commit (what was brought into main).
main..newbranch: All commits reachable from newbranch, excluding those that are reachable from main. Same as newbranch ^main
main...newbranch: All commits that are in only one of the two branches, not both
More at https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection
Compare
List the commits that are in origin/release-candidate but not in origin/main:
git log --graph --pretty=oneline --abbrev-commit origin/release-candidate ^origin/main
List the filenames that have changed in the commits in myfeature branch excluding the commits in main:
git diff --name-status main..myfeature
Modify the local graph
Rebase is covered wonderfully by https://womanonrails.com/git-rebase-onto
git rebase --onto newParent commitToLeaveBehind
Sync
Update a local branch to match the origin version (like pull) but without having that branch currently checked out:
git fetch origin main:main
Push tag to the origin: git push origin release-1.2.3
Delete tag on the origin: git push --delete origin mytagname (Delete local tag: git tag -d mytagname)
Configure
Git configuration comes from:
- system-wide
/etc/gitconfig(Linux) set withgit config --system - per-user at
~/.gitconfigor~/.config/git/configset withgit config --global - repository-specific at
.git/config
List combined git configuration and its source: git config --list --show-origin --show-scope
Line endings, bane of repos that touch multiple platforms
core.autocrlf
input: convert CRLF to LF on commit but not the other way around (leave changed on check-out)true: for Windows git users, converts LF endings into CRLF on check-outfalse: for single platform Windows-only projects, make no adjustments to line-endings
.gitattributes checked into the root of the repo takes precedence over core.autocrlf. Contains space delimited:
- file name pattern, e.g.
*,*.sh,*.png - line ending configuration, e.g.
text eol=lf(all users checkout as LF),binary(do not touch)
Identities for syncing with remote
For the current repository, use the specified key to authenticate to the remote: git config core.sshCommand "ssh -i /path/to/specific_private_key -o IdentitiesOnly=yes"
Find which user on the remote is associated with the given key: ssh -T -ai ~/.ssh/id_rsa git@github.com