diff --git a/assets/sass/application.scss b/assets/sass/application.scss index 176ac7f90d..6d28510499 100644 --- a/assets/sass/application.scss +++ b/assets/sass/application.scss @@ -27,6 +27,7 @@ $baseurl: "{{ .Site.BaseURL }}{{ if (and (ne .Site.BaseURL "/") (ne .Site.BaseUR @import 'book2'; @import 'lists'; @import 'about'; +@import 'cheat-sheet'; @import 'dark-mode'; @import 'git-turns-20'; diff --git a/assets/sass/cheat-sheet.scss b/assets/sass/cheat-sheet.scss new file mode 100644 index 0000000000..018309a5cf --- /dev/null +++ b/assets/sass/cheat-sheet.scss @@ -0,0 +1,112 @@ +@import "variables"; + +.cheat-sheet { + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; + + .item { + background: var(--bg-color); + padding: 10px; + border-radius: 10px; + border: 1px solid var(--callout-color); + display: flex; + font-size: 1rem; + flex-direction: column; + gap: 5px; + + p { + font-size: 1rem; + margin: 0 8px; + } + + h3 { + color: var(--font-color); + line-height: 1em; + font-size: 1.1rem; + margin-bottom: 8px; + font-weight: 500; + } + + label { + display: block; + margin-top: 10px; + } + + code { + background: var(--callout-color); + color: var(--font-color); + border-radius: 3px; + padding: 4px 8px; + font-family: Courier, monospace; + margin-bottom: 0; + } + + h3 code { + padding: 0; + background: transparent; + } + + .or { + font-weight: 700; + font-size: 0.8em; + display: block; + margin-left: 30px; + font-family: sans-serif; + } + } + + section { + margin-bottom: 20px; + display: grid; + gap: 10px; + grid-template-columns: 1fr 1fr; + + @media (max-width: $mobile-m) { + & { + display: flex; + flex-direction: column; + } + } + + h2 { + grid-column: 1/3; + + margin-top: 0; + font-size: 1.2rem; + font-weight: 700; + line-height: 1.2; + color: var(--orange); + } + } + + .commit-reference { + background: var(--callout-color); + border-left: 4px solid var(--orange); + display: block; + padding: 10px; + border-radius: 10px; + + .intro { + margin: 16px 0; + } + + dl { + display: grid; + grid-template-columns: auto 1fr; + gap: 8px 16px; + margin: 0; + + dt { + &::before { + content: "★ "; + font-size: 18px; + color: #666; + } + } + + dd { + font-family: monospace; + padding: 4px 8px; + } + } + } +} diff --git a/assets/sass/dark-mode.css b/assets/sass/dark-mode.css index 1e771e4f62..94a91ec5a8 100644 --- a/assets/sass/dark-mode.css +++ b/assets/sass/dark-mode.css @@ -152,6 +152,10 @@ #l10n-versions-dropdown footer a { color: #6969dd; } + + .cheat-sheet img { + filter: invert(60%); + } } } diff --git a/content/cheat-sheet/_index.html b/content/cheat-sheet/_index.html new file mode 100644 index 0000000000..c55f941d80 --- /dev/null +++ b/content/cheat-sheet/_index.html @@ -0,0 +1,775 @@ +--- +section: "documentation" +subsection: "cheat-sheet" +title: "Git Cheat Sheet" +url: /cheat-sheet.html +headings: + - text: "Getting Started" + id: "getting-started" + - text: "Prepare to Commit" + id: "prepare-to-commit" + - text: "Make Commits" + id: "make-commits" + - text: "Move Between Branches" + id: "move-between-branches" + - text: "Diff Staged/Unstaged Changes" + id: "diff-staged-unstaged-changes" + - text: "Diff Commits" + id: "diff-commits" + - text: "Ways to refer to a commit" + id: "ways-to-refer-to-a-commit" + - text: "Discard Your Changes" + id: "discard-your-changes" + - text: "Edit History" + id: "edit-history" + - text: "Code Archaeology" + id: "code-archaeology" + - text: "Combine Diverged Branches" + id: "combine-diverged-branches" + - text: "Restore an Old File" + id: "restore-an-old-file" + - text: "Add a Remote" + id: "add-a-remote" + - text: "Push Your Changes" + id: "push-your-changes" + - text: "Pull Changes" + id: "pull-changes" + - text: "Configure Git" + id: "configure-git" + - text: "Important Files" + id: "important-files" +--- + +
+

Git Cheat Sheet

+ +
+

Getting Started

+
+

Start a new repo:

+ git init +
+
+

Clone an existing repo:

+ git clone <url> +
+
+ +
+

Prepare to Commit

+
+

Add untracked file or unstaged changes:

+ git add <file> +
+
+

Add all untracked files and unstaged changes:

+ git add . +
+
+

Choose which parts of a file to stage:

+ git add -p +
+ +
+

Move file:

+ git mv <old> <new> +
+
+

Delete file:

+ git rm <file> +
+ +
+

Tell Git to forget about a file without deleting it:

+ git rm --cached <file> +
+
+

Unstage one file:

+ git reset <file> +
+
+

Unstage everything:

+ git reset +
+
+

Check what you added:

+ git status +
+
+ +
+

Make Commits

+
+

Make a commit (and open text editor to write message):

+ git commit +
+
+

Make a commit:

+ git commit -m 'message' +
+
+

Commit all unstaged changes:

+ git commit -am 'message' +
+
+ +
+

Move Between Branches

+
+

Switch branches:

+ git switch <name> + OR + git checkout <name> +
+
+

Create a branch:

+ git switch -c <name> + OR + git checkout -b <name> +
+
+

List branches:

+ git branch +
+
+

List branches by most recently committed to:

+ git branch --sort=-committerdate +
+
+

Delete a branch:

+ git branch -d <name> +
+
+

Force delete a branch:

+ git branch -D <name> +
+
+ +
+

Diff Staged/Unstaged Changes

+
+

Diff all staged and unstaged changes:

+ git diff HEAD +
+
+

Diff just staged changes:

+ git diff --staged +
+
+

Diff just unstaged changes:

+ git diff +
+
+ +
+

Diff Commits

+
+

Show diff between a commit and its parent:

+ git show <commit> +
+
+

Diff two commits:

+ git diff <commit> <commit> +
+
+

Diff one file since a commit:

+ git diff <commit> <file> +
+
+

Show a summary of a diff:

+ git diff <commit> --stat + git show <commit> --stat +
+
+
+

Ways to refer to a commit

+

+ Every time we say <commit>, you can use any of these: +

+ +
+
a branch
+
main
+ +
a tag
+
v0.1
+ +
a commit ID
+
3e887ab
+ +
a remote branch
+
origin/main
+ +
current commit
+
HEAD
+ +
3 commits ago
+
HEAD^^^ or HEAD~3
+
+
+ +
+

Discard Your Changes

+
+

Delete unstaged changes to one file:

+ git checkout <file> +
+
+

Delete all staged and unstaged changes to one file:

+ git checkout HEAD <file> +
+
+

Delete all staged and unstaged changes:

+ git reset --hard +
+
+

Delete untracked files:

+ git clean +
+
+

'Stash' all staged and unstaged changes:

+ git stash +
+
+ +
+

Edit History

+
+

"Undo" the most recent commit (keep your working directory the same):

+ git reset HEAD^ +
+
+

Squash the last 5 commits into one:

+ git rebase -i HEAD~6 +

Then change "pick" to "fixup" for any commit you want to combine with the previous one

+
+
+

Undo a failed rebase:

+ git reflog BRANCHNAME +

Then manually find the right commit ID in the reflog, then run:

+ git reset --hard <commit> +
+
+

Change a commit message (or add a file you forgot):

+ git commit --amend +
+
+ +
+

Code Archaeology

+
+

Look at a branch's history:

+ git log main + git log --graph main + git log --oneline +
+
+

Show every commit that modified a file:

+ git log <file> +
+
+

Show every commit that modified a file, including before it was renamed:

+ git log --follow <file> +
+
+

Find every commit that added or removed some text:

+ git log -G banana +
+
+

Show who last changed each line of a file:

+ git blame <file> +
+
+ +
+

Combine Diverged Branches

+
+

Combine with rebase:

+ git switch banana + git rebase main + + + {{< graphviz engine="neato">}} + digraph G { + + bgcolor="transparent"; + rankdir=TB; + node [ + shape=box, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,1!"]; + B [label="B", pos="1,1!"]; + C [label="C", pos="2,1!"]; + D [label="D", color="#f14e32", pos="2,0!"]; + E [label="E", color="#f14e32", pos="3,0!"]; + + main_label [label="main", shape=plaintext, pos="5,1!", fixedsize=false]; + main_label -> C [dir=forward]; + + banana_label [label="banana", shape=plaintext, pos="5.2,0!", fixedsize=false]; + banana_label -> E [dir=forward]; + + // Edges + A -> B; + B -> C; + B -> D; + D -> E; + } + + {{< /graphviz>}} + + {{< graphviz engine="neato">}} + digraph G { + + bgcolor="transparent"; + rankdir=TB; + node [ + shape=box, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,1!"]; + B [label="B", pos="1,1!"]; + C [label="C", pos="2,1!"]; + D [label="D", style="dashed", color="#f14e32", pos="2,0!"]; + E [label="E", style="dashed", color="#f14e32", pos="3,0!"]; + + D2 [label="D", color="#f14e32", pos="3,1!"]; + E2 [label="E", color="#f14e32", pos="4,1!"]; + + + main_label [label="main", shape=plaintext, pos="2,2!", fixedsize=false]; + main_label -> C [dir=forward]; + + banana_label [label="banana", shape=plaintext, pos="4,2!", fixedsize=false]; + banana_label -> E2 [dir=forward]; + + + lost_label [label="\"lost\"", shape=plaintext, pos="5.2,0!", fixedsize=false]; + lost_label -> E [dir=forward]; + + // Edges + A -> B; + B -> C; + C -> D2; + D2 -> E2; + + B -> D; + D -> E; + } + + {{< /graphviz>}} +
+
+

Combine with merge:

+ git switch main + git merge banana + + + {{< graphviz engine="neato">}} + digraph G { + + bgcolor="transparent"; + rankdir=TB; + node [ + shape=square, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,1!"]; + B [label="B", pos="1,1!"]; + C [label="C", pos="2,1!"]; + D [label="D", color="#f14e32", pos="2,0!"]; + E [label="E", color="#f14e32", pos="3,0!"]; + + main_label [label="main", shape=plaintext, pos="5,1!", fixedsize=false]; + main_label -> C [dir=forward]; + + banana_label [label="banana", shape=plaintext, pos="5.2,0!", fixedsize=false]; + banana_label -> E [dir=forward]; + + // Edges + A -> B; + B -> C; + B -> D; + D -> E; + } + + {{< /graphviz>}} + + {{< graphviz engine="neato">}} + digraph G { + + bgcolor="transparent"; + rankdir=TB; + node [ + shape=square, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,1!"]; + B [label="B", pos="1,1!"]; + C [label="C", pos="2,1!"]; + D [label="D", color="#f14e32", pos="2,0!"]; + E [label="E", color="#f14e32", pos="3,0!"]; + M [label="◇", pos="4,1!"]; + + main_label [label="main", shape=plaintext, pos="5,1!", fixedsize=false]; + main_label -> M [dir=forward]; + + banana_label [label="banana", shape=plaintext, pos="5.2,0!", fixedsize=false]; + banana_label -> E [dir=forward]; + + // Edges + A -> B; + B -> C; + B -> D; + D -> E; + C -> M; + E -> M; + } + + {{< /graphviz>}} +
+
+

Combine with squash merge:

+ git switch main + git merge --squash banana + git commit + + + {{< graphviz engine="neato">}} + digraph G { + + bgcolor="transparent"; + rankdir=TB; + node [ + shape=square, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,1!"]; + B [label="B", pos="1,1!"]; + C [label="C", pos="2,1!"]; + D [label="D", color="#f14e32", pos="2,0!"]; + E [label="E", color="#f14e32", pos="3,0!"]; + + main_label [label="main", shape=plaintext, pos="5,1!", fixedsize=false]; + main_label -> C [dir=forward]; + + banana_label [label="banana", shape=plaintext, pos="5.2,0!", fixedsize=false]; + banana_label -> E [dir=forward]; + + // Edges + A -> B; + B -> C; + B -> D; + D -> E; + } + + {{< /graphviz>}} + + {{< graphviz engine="neato">}} + digraph G { + + bgcolor="transparent"; + rankdir=TB; + node [ + shape=rect, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,1!"]; + B [label="B", pos="1,1!"]; + C [label="C", pos="2,1!"]; + D [label="D", color="#f14e32", pos="2,0!"]; + E [label="E", color="#f14e32", pos="3,0!"]; + S [label="D\nE", color="#f14e32", pos="3,1!", width=0.6, height=0.8]; + + main_label [label="main", shape=plaintext, pos="5,1!", fixedsize=false]; + main_label -> S [dir=forward]; + + banana_label [label="banana", shape=plaintext, pos="5.2,0!", fixedsize=false]; + banana_label -> E [dir=forward]; + + // Edges + A -> B; + B -> C; + B -> D; + D -> E; + C -> S; + } + + {{< /graphviz>}} +
+
+

Bring a branch up to date with another branch (aka "fast-forward merge"):

+ git switch main + git merge banana + + + {{< graphviz engine="neato">}} + digraph G { + + bgcolor="transparent"; + rankdir=LR; + node [ + shape=rect, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,0!"]; + B [label="B", pos="1,0!"]; + C [label="C", pos="2,0!"]; + D [label="D", pos="3,0!"]; + E [label="E", pos="4,0!"]; + invisible [label="", shape=plaintext,pos="6,0!"]; + + main_label [label="main", fontcolor="#f14e32", shape=plaintext, pos="2,-1!", fixedsize=false]; + main_label -> C [dir=forward]; + + banana_label [label="banana", shape=plaintext, pos="4,-1!", fixedsize=false]; + banana_label -> E [dir=forward]; + + // Edges + A -> B; + B -> C; + C -> D; + D -> E; + } + {{< /graphviz>}} + + {{< graphviz engine="neato">}} + digraph G { + + bgcolor="transparent"; + rankdir=LR; + node [ + shape=rect, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,0!"]; + B [label="B", pos="1,0!"]; + C [label="C", pos="2,0!"]; + D [label="D", pos="3,0!"]; + E [label="E", pos="4,0!"]; + + main_label [label="main", fontcolor="#f14e32", shape=plaintext, pos="6, 0!", fixedsize=false]; + main_label -> E [dir=forward]; + + banana_label [label="banana", shape=plaintext, pos="4,-1!", fixedsize=false]; + banana_label -> E [dir=forward]; + + // Edges + A -> B; + B -> C; + C -> D; + D -> E; + } + {{< /graphviz>}} +
+
+

Copy one commit onto the current branch:

+ git cherry-pick <commit> + + + {{< graphviz engine="neato">}} + digraph G { + bgcolor="transparent"; + rankdir=TB; + node [ + shape=box, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,1!"]; + B [label="B", pos="1,1!"]; + C [label="C", pos="2,1!"]; + D [label="D", color="#f14e32", pos="2,0!"]; + E [label="E", pos="3,0!"]; + + main_label [label="main", shape=plaintext, pos="5,1!", fixedsize=false]; + main_label -> C [dir=forward]; + + // Edges + A -> B; + B -> C; + B -> D; + D -> E; + } + {{< /graphviz>}} + + + {{< graphviz engine="neato">}} + digraph G { + bgcolor="transparent"; + rankdir=TB; + node [ + shape=box, width=0.6, height=0.6, penwidth=3, + fixedsize=true, fontname="monospace", + fontsize=24 + ]; + edge [arrowsize=1.0, dir=none, penwidth=3]; + + + // Nodes + A [label="A", pos="0,1!"]; + B [label="B", pos="1,1!"]; + C [label="C", pos="2,1!"]; + D2 [label="D", color="#f14e32", pos="3,1!"]; + + D [label="D", color="#f14e32", pos="2,0!"]; + E [label="E", pos="3,0!"]; + + main_label [label="main", shape=plaintext, pos="5,1!", fixedsize=false]; + main_label -> D2 [dir=forward]; + + // Edges + A -> B; + B -> C; + C -> D2; + B -> D; + D -> E; + } + {{< /graphviz>}} +
+
+ +
+

Restore an Old File

+
+

Get the version of a file from another commit:

+ git checkout <commit> <file> + OR + git restore <file> --source <commit> +
+
+ +
+

Add a Remote

+
+ git remote add <name> <url> +
+
+ +
+

Push Your Changes

+
+

Push the main branch to the remote origin:

+ git push origin main +
+
+

Push the current branch to its remote "tracking branch":

+ git push +
+
+

Push a branch that you've never pushed before:

+ git push -u origin <name> +
+
+

Force push:

+ git push --force-with-lease +
+
+

Push tags:

+ git push --tags +
+
+ +
+

Pull Changes

+
+

Fetch changes (but don't change any of your local branches):

+ git fetch origin main +
+
+

Fetch changes and then rebase your current branch:

+ git pull --rebase +
+
+

Fetch changes and then merge them into your current branch:

+ git pull origin main + OR + git pull +
+
+

Fetch all branches:

+ git fetch --all +
+
+ +
+

Configure Git

+
+

Set a config option:

+ git config user.name 'Your Name' +
+
+

Set option globally:

+ git config --global ... +
+
+

Add an alias:

+ git config alias.st status +
+
+

See all possible config options:

+ man git-config +
+
+ +
+

Important Files

+
+

Local git config:

+ .git/config +
+
+

Global git config:

+ ~/.gitconfig +
+
+

List of files to ignore:

+ .gitignore +
+
+
diff --git a/layouts/partials/sidebar.html b/layouts/partials/sidebar.html index aadd84098a..34c083780d 100644 --- a/layouts/partials/sidebar.html +++ b/layouts/partials/sidebar.html @@ -23,6 +23,9 @@
  • Reference
  • +
  • + Cheat Sheet +
  • Book