Skip to content
POLARISAI ACADEMY
Going deeper (optional)

This lesson is an optional deeper look that goes with course F05. It has worked examples with numbers and technical words. Start with F05 if you are new. Go to course F05 →

FD05 · First Light

Computer Craft: Files, Terminal, Git and Editor

You can start a Git repository, load JSON in a short script and print a field, undo a bad commit with git revert, and keep a .env file out of Git with .gitignore. You can also find your own line in a stack trace and tell exit code 0 from any other code.

Level
First Light
Time
10 h (estimate)
Prerequisites
F05
Course 5 of 7
Jump to

After this course you can

The student can initialise a Git repository, fetch and parse JSON via script, revert a defective commit cleanly, and block .env secrets via .gitignore.

Paths and the folder you are in

A path is the address of a file. An absolute path starts at the root and names every folder along the way, such as /home/sam/notes/todo.md on Linux or macOS. A relative path starts from the folder you are in now. From notes, that same file is todo.md. Windows writes a drive path as C:\Users\sam\notes\todo.md, and many terminals there accept forward slashes. If a name contains a space, wrap the path in quotes. Otherwise the shell treats it as two arguments and looks for a file you do not have.

ls prints the names in the current folder. ls -l adds size, owner, and permission bits. cd notes moves into notes, cd .. moves up one level, and cd alone returns to your home folder. pwd prints the absolute path of the folder the terminal is using. Use it when a script cannot find a file you can see in the editor. The editor and the terminal often start in different folders. Save the file, run pwd, and check that the path matches the tab you just edited.

Search, pipes, exit codes, permissions

grep price notes.txt prints each line in that file that contains price. grep -n adds the line number, which is the number you look up when a crash report names a file and a line. A pipe sends one command's output into the next command. ls | grep env shows only visible names that contain env. Add -a, as in ls -a | grep env, to include hidden names such as .env. curl downloads the body of a URL and prints it. Add -f so an HTTP error counts as a failed command.

Every program returns an exit code when it stops. Code 0 means the command finished the job you asked for. Any other code means something went wrong, and you still need the message printed above the prompt to see what. grep uses 1 when it searches cleanly and finds no line. That is a real result, and it still fails a script that stops on the first non-zero code. Read both the text and the code. A POSIX pipeline reports the last command's code.

ls -l starts each line with a file-type character and nine permission bits, such as -rw-r--r--. The dash means an ordinary file and d means a folder. Of the nine bits, the first three apply to the owner, the next three to the group, and the last three to everyone else. r means read, w means write, and x means execute. A script without x for you will not start as ./script.sh until chmod u+x script.sh adds execute for the owner. bash script.sh still runs it. Environment variables are names the shell stores for the session. HOME holds your home directory. export API_TOKEN=example-value sets a name a program can read while it runs.

.env and .gitignore

A .env file is a list of lines in the form NAME=value. Programs that use a loader such as dotenv read it when they start, so passwords, tokens, and host names stay out of the source you show people. Git remembers a file from the commit where you added it. Deleting the file in a later commit leaves the old commit readable. Someone who clones the repo can check out that commit and read the secret. If a real secret has already been pushed, treat it as copied and replace it at the service that issued it.

.gitignore sits in the project root and lists patterns Git should skip. A line with .env skips a file of that name in any folder. A line with *.log skips names that end in .log. Ignoring a file does nothing if Git is already tracking it. When git status still shows .env as modified, run git rm --cached .env. That drops the file from the index and leaves your copy on disk. Commit that change. Before each commit, read git status. An ignored .env is absent from that list.

Commits, branches, and revert

git init makes a repository here and stores history in .git. git status lists what changed. git add notes.md stages that file. git commit -m "Add price notes" records the staged snapshot with that message. git diff prints unstaged edits as removed and added lines. git diff --staged prints what the next commit will contain. A branch is a name for a commit. git switch -c wip creates wip and checks it out. git merge wip brings that work onto the branch you have open.

git revert undoes a chosen commit by adding a new commit with the opposite diff. HEAD means the newest commit on the branch you have open. If the bad commit put 99 where the file had 12, the revert commit puts 12 back. The bad commit stays in git log, and the revert sits on top of it. Prefer this after a push. A merge commit needs -m and a parent number. If the patch hits a later edit, Git stops with a conflict you fix and commit.

A stack trace is the list printed when a program crashes. Each frame names a function, a file, and a line. Open the frame that names your file and read that line in the editor. Library frames show which call failed. Objects in JSON sit in braces, lists sit in brackets, and a field looks like "price": 12. YAML stores data with indentation instead of braces. Markdown is plain text for a README. A line that begins with # is a heading. A fenced block holds commands.

An example

Revert a bad price in a new repo

Make a folder called shop and run git init inside it. The terminal prints that it initialized an empty Git repository, and the path ends in shop/.git. Add .gitignore with the single line .env. Add .env with TOKEN=example-secret. Call that an example token. git status lists .gitignore as untracked. .env stays off that list because the ignore pattern matches it. Add item.json with the text {"qty": 4, "price": 12}. Commit .gitignore and item.json with the message "Add item".

A script that loads item.json and prints qty times price prints 48. You can check that product by hand, since 4 times 12 is 48. It exits 0. One command is python -c "import json; d = json.load(open('item.json')); print(d['qty'] * d['price'])" and it prints 48. The same steps work if the first line fetches JSON with curl -fsS and writes the body to item.json before the parser runs.

Change the price from 12 to 99 and commit with the message "Set price to 99". By hand, 4 times 99 is 396, the wrong total for this example. git show --stat on that commit names item.json and reports one insertion and one deletion. git revert HEAD --no-edit then adds a new commit. The printed subject starts with Revert "Set price to 99". git log --oneline lists three commits, newest first. The 99 commit remains in that list. Open item.json and you are back to "price": 12.

Exercise

Do it on your own machine

Create a folder. Add .gitignore containing only .env. Add .env containing TOKEN=example-secret, and treat that token as an example. Add item.json containing {"qty": 4, "price": 12}. On paper, multiply 4 by 12, then 4 by 99. If you have a terminal, git init, check git status, and run a short script that loads the JSON and prints qty times price. Commit .gitignore and item.json. Set the price to 99, commit, then git revert HEAD --no-edit. With no terminal, paste the steps to a free chatbot and ask what they print.

Your paper shows 48 and 396. git status before the first commit lists .gitignore and item.json, and it leaves .env out. The script prints 48 and exits 0. After the revert, git log --oneline shows three commits. The newest subject starts with Revert. item.json holds 12 again, so a second run of the script prints 48. The middle commit still mentions the price 99. If .env appears in git status, the ignore line is missing or the file was already tracked.

Check questions

Answer on your own first. Then open a question to see the answer.

A command finishes with exit code 0. What does that code mean?

The command finished the job it was asked to do. A different code means the command reported failure. grep uses 1 when the search runs and no line matches.

What does git revert HEAD --no-edit add to the repository?

A new commit whose diff is the opposite of the newest commit. The old commit stays in the log. In the shop example the log has three commits and the price returns from 99 to 12.

.env is already tracked, and .gitignore now contains the line .env. How do you stop Git tracking it while keeping the file on disk?

Run git rm --cached .env and commit that change. The file stays on disk, and later commits no longer record it.

item.json holds qty 4 and price 12. The script prints qty times price. What does it print, and what is 4 times 99?

It prints 48, because 4 times 12 is 48. Four times 99 is 396, the wrong total in the bad commit.

Take with you

Ignore .env before the first commit, check a JSON product by hand, and let git revert add a new commit when a shared change was wrong.

Final task

Submit a Git repository bundle inspected by automated tests that verify: clean commit history, a reverted defective commit, .gitignore blocking .env, zero leaked keys in git log -p, and a shell/Python data parser passing 10 fixed input/output tests.

The final task is marked against fixed answer keys and automated tests once the tests open.

Tests and diplomas coming

This lesson is open to read. Tests, diplomas and accounts come later; the waitlist is for those. Join the waitlist