2024-10-30
$ are commands to type# is a comment (so no need to type it in)ls and pwd and git status and md5sum commands than necessary, however this so that the student can check they have the same thingCongratulations, you found the vertical slide. Some exercises will have these vertical slides, and you can find more info, or deep-dive info in there.
There may be solutions or hints to the exercise in the vertical scroll, so only go there after you have solved the exercise.
At some point (but maybe better after you do at least some of the exercises):
Create a new directory in your home directory named minions and move to this new directory
Start a new git respository in this directory
$ pwd
/home/claude/minions
$ ls -la # see vertical slide for more info on the `-la` part
total 12
drwxr-xr-x 2 claude claude 4096 Mar 24 10:56 .
drwxr-x--- 1 claude claude 4096 Mar 24 10:56 ..
$ git init # starts new, empty, respository in current directory
Initialized empty Git repository in /home/claude/minions/.git/
$ git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
$ ls -la
total 16
drwxr-xr-x 3 claude claude 4096 Mar 24 11:01 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:01 ..
drwxr-xr-x 7 claude claude 4096 Mar 24 11:04 .gitls -la is a short form for ls -l -a (in lesson 2 we saw that often two short flags (-l and -a) can be pulled together into -la).
-a means that we want to see all files, including hidden files (the ones starting with . – see one more vertical slide for more info).
-l (lower-case L) means long format. It lists one file per line, and adds some info:
The exact format differs per system, but usually contains similar information
total 12
drwxr-xr-x 2 claude claude 4096 Mar 24 10:56 .
drwxr-x--- 1 claude claude 4096 Mar 24 10:56 ... is the current dir and .. the parent dir. These are always present (so this, in effect, is an empty directory).drwxr-xr-x explains that it is a directory (d), and next who has rights to read/write/access it (you can ignore this for now)claude claude describes the owner (and group) of the file4096 is the disk size of this file or directoryMar 24 10:56 is the modified time (last time something changed). It depends on the system which timezone this is in (in our case: UTC)1 and 2 are number of links, please ignore :)bananas.txt and make it a hidden file (and it won’t show in your explorer, unless you switch it to show hidden files).. (dot).. and .., the current and parent directory (which is why you normally don’t see them if you do ls without -a)..DS_Store, which you don’t see on mac. These are hidden files on mac, but not on windows..git is therefore also a hidden directory. Even though it’s a good idea to ignore .git directory, there are many hidden files that are useful to be aware of.Remove repository in minions directory
Literally the only thing that makes the directory a git repository, is the presence of the .git directory
$ pwd
/home/claude/minions
$ ls -la
total 16
drwxr-xr-x 3 claude claude 4096 Mar 24 11:01 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:01 ..
drwxr-xr-x 7 claude claude 4096 Mar 24 11:04 .git
$ rm -r .git # -r means "recursive", it will delete a whole directory
$ ls -la
total 12
drwxr-xr-x 2 claude claude 4096 Mar 24 11:06 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:01 ..
# as you can see, no more .git directory
$ git status
fatal: not a git repository (or any of the parent directories): .git
# also git knows it's not a git repository anymore
#(it looks in the current director, and ALL parents (until it gets to /)Deleting the repo means you lose all info (commits, log messages, history) in the repo (unless it’s backed up, e.g. in GitHub)
In “normal life” there is little reason to ever delete the repo; this exercise is mostly to show that the repository is no magic, just a directory and nothing more.
Recreate git repository and check the log (note, checking the log may fail at this time)
$ pwd
/home/claude/minions
$ ls -la
total 12
drwxr-xr-x 2 claude claude 4096 Mar 24 11:06 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:01 ..
$ git init
Initialized empty Git repository in /home/claude/minions/.git/
$ git init
Reinitialized existing Git repository in /home/claude/minions/.git/
# So if you type git init in an already existing repo, it does nothing much
$ git log
fatal: your current branch 'main' does not have any commits yet
# makes sense, the branch is empty, there is nothing to showCreate a simple file bananas.txt, write some text (minimal 5 lines) and save the file
$ pwd
/home/claude/minions
$ ls -la
total 16
drwxr-xr-x 3 claude claude 4096 Mar 24 11:17 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:01 ..
drwxr-xr-x 7 claude claude 4096 Mar 24 11:17 .git
$ nano bananas.txt # save with CTRL-O and exit with CTRL-X when done
# see slide "below" to see alternative way to create the file
$ ls -la
total 20
drwxr-xr-x 3 claude claude 4096 Mar 24 11:24 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:22 ..
drwxr-xr-x 7 claude claude 4096 Mar 24 11:17 .git
-rw-r--r-- 1 claude claude 145 Mar 24 11:23 bananas.txt# alternative way to create the file
$ echo "Ba Ba Ba, Ba Ba Na Na
Ba Ba Ba, Ba Ba Na Na
Bananahahaaa
Potatoo nahahaaa
Tokadido Pokatoli
Kalimanomani
Tanotika
Ba Ba Ba, Ba Ba Na Na" > bananas.txtBecause we used a quote (“), we can use multiple lines in our echo statement (the statement won’t end until the quote is closed). Watch out in this method, that if the thing you paste contains quotes by itself, it will be interpreted as endquote and it won’t always work.
E.g (don’t run this, it will give errors)
Run git status. What does it tell you?
$ pwd
/home/claude/minions
$ ls -la
total 20
drwxr-xr-x 3 claude claude 4096 Mar 24 11:24 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:22 ..
drwxr-xr-x 7 claude claude 4096 Mar 24 11:17 .git
-rw-r--r-- 1 claude claude 145 Mar 24 11:23 bananas.txt
$ git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
bananas.txt
nothing added to commit but untracked files present (use "git add" to track)
# It says that there is one untracked files. Untracked means "it is present in your working directory, but not in (the currently checked out) git revision.
# Think of "track changed" in Word; untracked means that changes are not being tracked (and remember, everything, including creation of a file, is a change).Move the file (or technically the changes) into the index
The add subcommand takes a filename as parameter
$ pwd
/home/claude/minions
$ ls -la
total 20
drwxr-xr-x 3 claude claude 4096 Mar 24 11:24 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:22 ..
drwxr-xr-x 7 claude claude 4096 Mar 24 11:17 .git
-rw-r--r-- 1 claude claude 145 Mar 24 11:23 bananas.txt
$ git add bananas.txt
# That's it :)) -- next exercise we confirm whether it worked
# alternatively you can do `git add *` to add all (non-hidden) files
# or `git add .` to add (everything in) the current directory.Run git status again. What does it tell you now?
$ pwd
/home/claude/minions
$ ls -la
total 20
drwxr-xr-x 3 claude claude 4096 Mar 24 11:24 .
drwxr-x--- 1 claude claude 4096 Mar 24 11:22 ..
drwxr-xr-x 7 claude claude 4096 Mar 24 11:17 .git
-rw-r--r-- 1 claude claude 145 Mar 24 11:23 bananas.txt
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: bananas.txt
# It shows that the bananas.txt file is not longer untracked, but now ready to be committedCommit index into repo. Create a commit message
# NB: THIS ANSWER BOX CAN BE SCROLLED DOWN FOR THE WHOLE ANSWER
$ pwd
/home/claude/minions
$ git status -s
A bananas.txt
# -s means "--short". See vertical slide for explanation of the format
# Feel free to use -s or not use it in the future.
$ git commit
Author identity unknown
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your accounts default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'claude@8a554909b6fd.(none)')
# Oops, git needs to know what name and email to put on our commits.
# Remember that (for now) we're using git 100% locally, so it really
# doesn't matter what you use here, but let's be honest.
# See below for more info
$ git config --global user.email "claude@claude.nl"
$ git config --global user.name "Claude"
# Let's try again
$ git commit
# This opened a nano editor. Type a commit message (e.g. "Bananas for minions") and
# press CTRL-O <enter> to save and CTRL-X to exit
# You can ignore the stuff beneath, as described it will all be ignored since it starts with `#`
[main (root-commit) 4075390] Bananas for minions
1 file changed, 9 insertions(+)
create mode 100644 bananas.txt
# Looked like that worked, a new commit with sha `4075390`
$ git status
On branch main
nothing to commit, working tree clean
# makes sense, after we committed, there are no further changes
$ ls -la
total 20
drwxr-xr-x 3 claude claude 4096 Mar 24 11:24 .
drwxr-x--- 1 claude claude 4096 Mar 24 17:50 ..
drwxr-xr-x 8 claude claude 4096 Mar 24 18:16 .git
-rw-r--r-- 1 claude claude 145 Mar 24 11:47 bananas.txt
# but the file is (obviously) still theregit status --short format.
git status green colours are about index stuff, red about local directory stuffA means the file is [A]dded (new)M means the file is [M]odifiedD means the file is [D]eleted?? means an untracked file.Example (note that colours are wrong)
means:
bananas.txt was “Added to index, no local changes” (A as first character, space as second)orange.txt was “Modified in local directory, but changes were not staged” (staged means: present in index)First time you use git on a new computer, git wants to know who you are (name and email). Git will never do anything with this data, except add it to every commit you make. If someone checks the log of commits later on, they can see this information (and will be able to talk to you, or email you in case they don’t know you).
What you fill in here is 100% your choice. It does mean that I can fill in Joe Biden and president@whitehouse.org if I want. Or I can fill in “Martyna Syposz” as name and make some commits to a project we both work on, and she’ll get blames for all mistakes I make.
Takeaway: this is for convenience only, never take a name or email address on a git commit as proof that it was that person!
Make code not crash with large input files is a good commit message. Changed "MAX_SIZE=500" into "MAX_SIZE=1000" in line 34 is a bad message.typo) commit messageGood (but maybe bit elaborate) commit message:
Bugfix: code crashed if input was empty
It turned out that if you provided an empty list to the `process()` function, the code would crash.
This is because `process()` would display a percentage completion, which was `nr_processed / input.length`, so en empty list resulted in division by zero error.
Added an if statement that makes progress = 100% if input.length == 0XKCD commit messages commic
The sad truth, especially late at night - Source: XKCD
git commit without a commit message on the command line, will open the nano editorgit commit will pick the message up from there (this is what we did in the “solution”)-m flag. If your commit message has spaces, you will need quotes (") around it!, because this has special meaning in bash)git commit -m "typo" # since there are no spaces in the message, the quotes around `typo` are not needed, but they don't hurt either
git commit -m "Bananas for minions"
git commit -m "I can write a very long commit message and this will not be a problem (except that convention said it should be short)"
git commit -m "ONE MORE TYPO!!!!" # WRONG: you cannot use ! on the terminal (in addition that, the message is not very informative, just `typo` would be better)
git commit -m "A fix for the "cannot open editor" bug" # WRONG: the whole message must be between quotes, but the " before `cannot` closes the start quoteCreate fluffy.txt, add it to index and commit
(nb: it’s hard to find good semantic commit messages when you’re just doing exercises. I think just using “exercise page 26” or something is fine)
$ pwd
/home/claude/minions
$ git status -s
# no reply means latest revision, index and working directory are all the same
$ echo "It's so fluffy" > fluffy.txt
$ git add fluffy.txt
$ git status -s
A fluffy.txt
$ git commit -m "exercise 26"
[main a587278] fluffy
1 file changed, 1 insertion(+)
create mode 100644 fluffy.txt🎉
You made it to the end
Please keep the homework, we will continue working with this repository next week.
The things in this lesson are essential, and exercise is required. Try to commit something at least once a day in your normal code.