C - Creating a Repository
By the end of class you will have:
Checked for updates in Linux:
sudo apt update
If any updates exist: sudo apt -y upgrade
Set your name and email address in Git on your computer.
These only have to be done once before you start using remote repositoriesgit config --global user.name "Your Name" * replace "Your Name"
git config --global user.email "first.last@stu.ocsb.ca"
Set the default branch name to "main" (somewhat unspoken standard)
git config --global init.defaultBranch main
Initialized a git repo for your current folder:
Make sure you are in the folder for your review tasks (the lotto649, password check etc)
If you don't like the name of the folder, rename it first
Run: git init to initialize the local repository in this folder
You can check the status of your repo with git status
Tracking Files
Our file(s) are there but not being tracked by Git and the Git system knows it.
Let's add all the files in our folder to the Git tracking: git add *
Alternatively, you can add only the files you want: git add main.js
Now let's see the status: git status
Committing Files
We've told Git to track the files but we haven't made a snapshot yet. In fact, Git is telling us this with "No commits yet" and listing our file(s) as "new". Right now the file(s) have been staged - they're ready to be committed.
Creating a snapshot - or saving your results to the Git repository - is called committing. You create a commit for each new file or changed file in your folder. In this repo we probably only have one file - main.js (or whatever you named your script file). If you added other files to the folder, they will also be listed.
Let's commit these changes (the newly tracked files) to our repo: git commit -m "First Commit"
The command git commit tells Git to store all changes that are currently staged. Staging is important - we'll talk more about it later.
The -m "First Commit" part of the command is optional but it's important to give a brief description of each commit in order to find moments in time. Without this option, Git will ask us to put a message.
Staging
Let's change one of the files. Copy and paste the code to the right into your JavaScript file.
Now let's check the status: git status
Git will tell you that changes were found but have not been staged. The staging area allows us to prepare a commit. Maybe you only want to snapshot some changes. Or maybe you want to put a message for a commit of each file. If you accidentally setup a file for a commit in the staging area, you can unstage it before it gets committed!
Let's stage the file for a commit: git add main.js
Now our file is staged for a commit. Yup - it's that simple.
Let's commit it! git commit -m "Added the useless parrot"
Note - If you want to skip the staging (not recommended) you can go directly to the commit with the -a option: git commit -a -m "I skipped staging"
function useless_parrot(message) {
console.log("🦜 '", message, "'");
}
Git Log
All of the commits (and your description) can be seen by running git log without any extra options. Try it!
Congratulations - you have a local repository that tracks your changes whenever you commit those changes to memory!
"Sir - is there no graphical user interface for doing all this?"
"Yes... yes there is. But I think it's important to get good with the terminal commands."
Extra: Undoing things