Git and GitHub: saving and sharing your work
Read TaskKit's history, take back a change before or after it is pushed, and send a new feature through a branch you check with TypeScript and a pull request your friend reviews before it reaches main.
Read TaskKit's history, take back a change before or after it is pushed, and send a new feature through a branch you check with TypeScript and a pull request your friend reviews before it reaches main.
Tested with TypeScript 7.x · Node.js LTS
A week after TaskKit went on GitHub, three things are waiting for you.
On Monday you made the holiday warnings red and pushed the change, and your
friend says red looks as if TaskKit has crashed. On Tuesday evening you
started tidying capitalize in tasks.ts, and now npm test (npm, the
Node package manager) fails and
you are not sure what you touched. And you want to add a warning for a
list that is almost full, but your friend runs git pull every
morning, so anything half done that you push reaches her the next day.
Lesson 15.3, Ship TaskKit: publish it on GitHub with a README and a
short demo recording, gave you three commands for every change: add,
commit, push. Git keeps far more than the newest snapshot, and GitHub
does far more than store it. By the end of this lesson you will read
TaskKit's history, take back both the Tuesday change and the Monday
commit, and send the new warning through a branch you check with
TypeScript, then a pull request your friend reads before it reaches
main.
Part 1 left work in the taskkit folder that Git has never saved.
git status lists the new folders and files under Untracked files:
server/, web/, shared/, scripts/, mobile/, taskkit-today/,
taskkit.cron and the rest. The files Part 1 changed are under
Changes not staged for commit: index.ts, package.json,
tsconfig.json, and store.ts if you made lesson A1.6's change. Two
must stay on your computer, for the reason tasks.json does:
backups/ holds copies of your list, and backup.log is what the
nightly run printed. Add them to .gitignore:
node_modules/
tasks.json
backups/
backup.logA project a tool made for you, such as taskkit-today, brings its own
.gitignore, and Git reads that one too. Then commit and push:
git add .
git commit -m "Add the work of Part 1"
git pushYour history then has this commit above TaskKit 1.0.
Then make the two changes of the story, so your folder matches this lesson:
Monday: red warnings, pushed
In index.ts, in warnIfHoliday, the first of its two
chalk.yellow(line) lines prints the holiday. Make it red:
console.log(chalk.yellow(line));
console.log(chalk.red(line));git add index.ts
git commit -m "Show holiday warnings in red"
git pushTuesday: a change you never commit
In tasks.ts, change the line inside capitalize, save the file, and
leave it uncommitted:
return task.slice(0, 1).toUpperCase() + task.slice(1);
return task[0].toUpperCase() + task.slice(1);Every commit you made, in lessons 15.2 and 15.3 and just now, is still
there, with its message. git log lists them, newest first, and
--oneline keeps each to one line. Run it in the taskkit folder:
git log --oneline1df6245 (HEAD -> main, origin/main) Show holiday warnings in red
ebc2120 TaskKit 1.0
e5a706f Update README.md
d887687 Say how to get TaskKit
25a9647 First version of TaskKitEach line starts with the first seven characters of a commit's id, then
its message. Your ids are different, since every commit gets its own,
and your list may hold a few more commits than this one. The words in
parentheses say where things stand. HEAD is Git's name for the commit
your folder holds right now, and HEAD -> main says you are on the
branch main. origin/main is where GitHub's main was the last time
Git talked to it. Both sit on the top line, so your copy and GitHub's
agree. Your line may also say origin/HEAD, GitHub's name for its
main branch: Git from version 2.48 prints it, so the line reads
(HEAD -> main, origin/main, origin/HEAD).
You never have to remember an id: when a command needs one, copy it from this list. Reading What Changed, in the Git Fundamentals course, shows more ways to read a history.
Which way back you take depends on one question: is the change in a commit yet?
The change is not committed
The file goes back to how your last commit has it. The history does not change.
The change is in a commit
A new commit undoes the old one. Both stay in the history, so a copy on GitHub still matches yours.
git diff and git restoreTuesday's change is in no commit, so git status lists tasks.ts as
modified, as in lesson 15.2. To see what you changed, ask for the
diff, the difference between your files and your last commit:
git diffdiff --git a/tasks.ts b/tasks.ts
index d391a9f..2346fb4 100644
--- a/tasks.ts
+++ b/tasks.ts
@@ -6,7 +6,7 @@ export interface Task {
status: Status;
}
export function capitalize(task: string): string {
- return task.slice(0, 1).toUpperCase() + task.slice(1);
+ return task[0].toUpperCase() + task.slice(1);
}
export function percentFull(added: number, limit: number): number {
// Rounded: without it, a limit of 3 prints 66.66666666666666The first four lines name the file: a/ is the file as your last
commit has it, b/ as your folder has it now. A line starting with
- is a line as your last commit has it; a line starting with + is
the same line as your file has it now. The lines with a space
in front did not change and are there so you can see where you are,
and the @@ line says which lines of the file are shown, from line 6.
So Tuesday's whole change is one line. task[0] is the string's first
character, as tasks[0] is an array's first item (lesson 4.1). An
empty string has no first character, so there task[0] is
undefined. Calling .toUpperCase() on it throws an error, and
npm test fails on capitalize leaves an empty task empty.
The old line was right. git restore puts a file back as your last
commit has it:
git restore tasks.ts
git statusOn branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree cleangit restore prints nothing itself. Working tree clean means your
files are exactly as your last commit has them, and npm test passes
again.
git revertMonday's red warnings are a different case: they are in the commit
1df6245, which is on GitHub, and your friend may have pulled it. To
revert a commit is to make a new commit that does the opposite of
it:
git revert --no-edit 1df6245[main 39f6ec8] Revert "Show holiday warnings in red"
Date: Thu Oct 29 09:30:00 2026 +0200
1 file changed, 1 insertion(+), 1 deletion(-)--no-edit keeps the message Git writes, Revert "Show holiday warnings in red". Without it, Git opens a text editor inside the
terminal for you to write one. The Date line is today's date. The new
commit is a commit like any other, so it goes to GitHub the usual way:
git pushTo https://github.com/ana/taskkit.git
1df6245..39f6ec8 main -> mainNow the history holds both: the red warnings, and the commit that took them back.
git log --oneline39f6ec8 (HEAD -> main, origin/main) Revert "Show holiday warnings in red"
1df6245 Show holiday warnings in red
ebc2120 TaskKit 1.0
e5a706f Update README.md
d887687 Say how to get TaskKit
25a9647 First version of TaskKitWhy not delete the Monday commit instead? Your friend's copy already
has it. A history that loses a commit no longer matches GitHub's, and
Git refuses your next push. A revert only adds, so every copy still
agrees, and the next git pull brings your friend yellow warnings
again. Undoing Things Safely covers the other ways back.
mainThe almost-full warning will take you a few commits, and until it
works, it should not reach main, the branch your friend pulls. Lesson
15.2 called a branch a line of snapshots. You can have more than one:
a new branch starts at the commit you are on, the commits you make on
it are added to that branch only, and main stays where it was until you
merge the branch, which brings its commits into main. It works
like a draft of a letter: you change the draft as much as you like, and
the clean copy changes only when you copy the draft over. So give each
change its own branch, made from main.
On GitHub, a branch is merged through a pull request: a request to
merge one branch into another, with a page of its own that shows the
branch's commits and every line they change, where others can read
and comment before anyone merges. The next section makes one.
Here is the whole trip of one change, from a new branch to main.
Switch between the two cases, and press Next step to walk through,
or let it play:
Your branch
GitHub
main
All 6 steps shown.
Your branch
GitHub
main
All 4 steps shown.
Make the branch and move onto it:
git switch -c almost-fullSwitched to a new branch 'almost-full'git switch moves you to a branch, and -c creates it first. Name a
branch after its change, in lower case, with hyphens between the
words. Now write the warning. In tasks.ts, under percentFull:
function isAlmostFull(added: number, limit: number): boolean {
return percentFull(added, limit) >= 80;
}and in print.ts, import it beside percentFull and print the warning
at the end of printSummary:
import { percentFull } from "./tasks.ts";
import { isAlmostFull, percentFull } from "./tasks.ts"; console.log(`List ${percent}% full`);
if (isAlmostFull(added, limit)) {
console.log(chalk.yellow("Almost full: finish a task first"));
}
}Before anyone sees the branch, let TypeScript read every file:
npm run check> check
> tsc
print.ts:7:10 - error TS2459: Module '"./tasks.ts"' declares
'isAlmostFull' locally, but it is not exported.
7 import { isAlmostFull, percentFull } from "./tasks.ts";
~~~~~~~~~~~~
tasks.ts:15:10 - 'isAlmostFull' is declared here.
15 function isAlmostFull(added: number, limit: number): boolean {
~~~~~~~~~~~~
Found 1 error in print.ts:7The first error line is one long line in your terminal, broken here to
fit. It is TS2459 (TS for TypeScript, then the number of the message)
from lessons 5.11 and 9.1: the function has no
export, so no other file may import it. Add the word, in tasks.ts:
export function isAlmostFull(added: number, limit: number): boolean {
return percentFull(added, limit) >= 80;
}and check again:
npm run check> check
> tscNo errors, and npm test still passes. See the warning at work.
TaskKit holds five tasks, and the warning starts at four. With an
empty list, add four; with tasks already in your list, add only as
many as it takes to reach four:
npm start -- add "buy milk" "call mom" "pay rent" "book the dentist"4 tasks added, room for 1 more
List 80% full
Almost full: finish a task firstThen commit the change on the branch:
git add tasks.ts print.ts
git commit -m "Warn when the list is almost full"[almost-full 8138ddb] Warn when the list is almost full
2 files changed, 7 insertions(+), 1 deletion(-)The first word in the square brackets is the branch the commit went
to. This is where TypeScript earns its place beside Git: you run the
check before you commit and push, so a mistake like the missing
export is caught before anyone else has the change. In CI: running
your checks on every pull request, later in this module, GitHub runs
the same check by itself on every pull request. CI, short for
continuous integration, is the name for checks like that.
Branching and Merging Basics shows more of what branches can do.
The branch is still only on your computer. Send it to GitHub, as a branch of the same name:
git push -u origin almost-fullremote:
remote: Create a pull request for 'almost-full' on GitHub by visiting:
remote: https://github.com/ana/taskkit/pull/new/almost-full
remote:
To https://github.com/ana/taskkit.git
* [new branch] almost-full -> almost-full
branch 'almost-full' set up to track 'origin/almost-full'.origin is GitHub's copy, as in lesson 15.3 and the origin/main
above. -u, short for
--set-upstream, links your branch to the one it just made on GitHub,
its upstream: from now on a plain git push on this branch knows
where to send. The remote: lines come from GitHub, which offers a
link for the next step.
Here the pull request asks to merge almost-full into main. Open
it from the terminal with gh, the GitHub CLI (command-line
interface: a program you drive by typing commands) you installed in
lesson 15.2;
pr in its commands stands for pull request:
gh pr create --fillCreating pull request for almost-full into main in ana/taskkit
https://github.com/ana/taskkit/pull/1--fill takes the pull request's title and description from your
commits, so gh asks you nothing. Open its page:
gh pr view --webOpening https://github.com/ana/taskkit/pull/1 in your browser.Send that link to your friend. To review it she needs a free GitHub
account of her own, signed in on github.com. On the page, the
Files changed tab
shows the change line by line, with - and + as in git diff. To
answer, she presses Review changes and picks Comment,
Approve or Request changes, then Submit review. (These are
the names GitHub used on 26 November 2026; GitHub changes its pages
from time to time, so a button may read a little differently on
yours.) You cannot approve your own pull request: a review is
someone else's judgement. If
she asks for a change, make it on almost-full, commit, and
git push: the new commit appears in the same pull request. With
nobody to review it, you can still read your own pull request and
merge it: the branch and the check are what keep main safe.
When she approves, merge it:
gh pr merge --merge --delete-branch✓ Merged pull request ana/taskkit#1 (Warn when the list is almost full)
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (1/1), 906 bytes | 453.00 KiB/s, done.
From https://github.com/ana/taskkit
* branch main -> FETCH_HEAD
39f6ec8..5c3baaa main -> origin/main
Updating 39f6ec8..5c3baaa
Fast-forward
print.ts | 5 ++++-
tasks.ts | 3 +++
2 files changed, 7 insertions(+), 1 deletion(-)
✓ Deleted local branch almost-full and switched to branch main
✓ Deleted remote branch almost-full--merge brings the branch's commits into main with one more commit
that records the merge. --delete-branch deletes almost-full on
GitHub and on your computer, since its work is in main now. Then
gh moves you back to main and pulls it: the lines from the first
remote: to 2 files changed are that pull, almost exactly what a
git pull prints. Your friend's next git pull brings her the
warning.
On the page, the same merge is three buttons: press Merge pull
request, then Confirm merge, then Delete branch near the
bottom of the page, which deletes the branch on GitHub. Then, in your
terminal, go back to main, pull it, and delete your own copy of the
branch:
git switch main
git pull
git branch -d almost-fullDeleted branch almost-full (was 8138ddb).-d deletes a branch only once its commits are merged, so nothing is
lost. When two branches change the same lines, GitHub cannot merge
them by itself and says the branch has conflicts. In the Git in
Practice course, Merging and Resolving Conflicts shows how
to settle them, and Pull Requests and Code Review how teams
review.
Picture the moment before the merge. You are on almost-full, just
after the push, and git log --oneline prints:
8138ddb (HEAD -> almost-full, origin/almost-full) Warn when the list is
almost full
39f6ec8 (origin/main, main) Revert "Show holiday warnings in red"
1df6245 Show holiday warnings in redand below them the commits of lessons 15.2 and 15.3 (and Part 1's, if
you made it). The first line is one long line in your terminal,
broken here to fit; a newer Git also prints origin/HEAD on the
second.
Predict
After these two commands, which commit message is on the first line?
git switch main
git log --onelineLesson 15.3 ended with a plain git push, which works on main
because gh repo create linked it to GitHub. A new branch has no such
link until its first push makes one.
Common mistake
Pushing a new branch with a plain git push
You commit on almost-full and send it the way you send everything:
git pushfatal: The current branch almost-full has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin almost-full
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.Nothing was sent. Git does not know which branch on GitHub this one belongs to.
fatal: The current branch almost-full has no upstream branch.
The fix
Push with -u, the short form of the --set-upstream the message
suggests:
git push -u origin almost-fullWhen you see no upstream branch, the commit is safe on your computer; only the push is missing.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
As in lesson 15.3, the page cannot run Git, so the commands are written
as console.log lines, and the questions ask about what Git does.
A change through a pull request, in order
This program prints the commands that send the almost-full warning from a new branch to a pull request, but in an order that would not work. Change the order of the lines, without changing any line itself, so that the commands work from top to bottom, with the check before the commit.
Loading the editor…
Which commit to revert
The commit that cut TaskKit's list to three tasks was a mistake, and
it is already on GitHub. git log --oneline prints:
4d2e9a1 (HEAD -> main, origin/main) Say how to get TaskKit on Windows
b7c03f5 Allow only three tasks
39f6ec8 Revert "Show holiday warnings in red"What do you type after git revert --no-edit to undo it?
Take back a change of this morning
This morning you changed today.ts, and you have not committed it.
git diff shows it was a bad idea. Which command puts today.ts back
the way your last commit has it?
A review that asks for a change
Your friend reviews the pull request for almost-full and asks for a
shorter warning. Where do you make the change?
Recap · 3 questions
Check yourself
A commit of yours is already on GitHub. Why undo it with git revert rather than delete it?
Check yourself
In git diff, what does a line starting with - show?
Check yourself
What is a pull request?
0 of 0 answered.
Cheat sheet
Git and GitHub: saving and sharing your work
What things are
| Word | What it means |
|---|---|
| HEAD | The commit your folder holds now |
| Diff | The difference between your files and your last commit |
| Revert | A new commit that undoes an earlier one |
| Branch | A line of commits, starting at the commit you are on |
| Merge | Bring a branch's commits into another branch |
| Upstream | The branch on GitHub your branch pushes to |
| Pull request | A request on GitHub to merge a branch, shown for review |
Git
| Write | To |
|---|---|
git log --oneline | List the commits, newest first |
git diff | See what changed since your last commit |
git restore tasks.ts | Throw away a change that is not committed |
git revert --no-edit 1df6245 | Undo a pushed commit with a new one |
git switch -c almost-full | Make a branch and move onto it |
git switch main | Move back to main |
git push -u origin almost-full | The first push of a new branch |
git branch -d almost-full | Delete a branch that is merged |
GitHub CLI
| Write | To |
|---|---|
gh pr create --fill | Open a pull request for this branch |
gh pr view --web | Open the pull request's page |
gh pr merge --merge --delete-branch | Merge it and delete the branch |
Rules
| Rule | What it means |
|---|---|
| Not committed | git restore throws it away |
| In a commit | git revert undoes it with a new commit |
| A new change | Its own branch, made from main |
| Check before the commit | npm run check on the branch |
You can now read TaskKit's history, take back a change before or after
it is pushed, and send a change through a branch you check with
TypeScript and a pull request, before it reaches main.
Next is Linters and formatters: Biome in TaskKit: a tool that reads your code for mistakes TypeScript does not catch, and lays it out the same way for everyone who works on it, so a pull request shows only the lines that really changed.
Before you move on, open a second pull request on your own TaskKit: on
a new branch, change the warning's words, check, commit, push with
-u, and open it with gh pr create --fill. Read it on the Files
changed tab, then merge it.