Chapter 6: Git & GitHub
If you are saving your files as index.html, index_final.html, and index_final_for_real.html, you are begging for a disaster. What happens if your computer crashes? What happens if you need to collaborate with three other developers?
This is where Git comes in. Git is a Version Control System. It is basically a time machine for your code.
Git vs GitHub
People often confuse the two. Here is the difference:
- Git is the software installed on your local computer. It tracks every line of code you change and allows you to revert to previous versions.
- GitHub is a website (owned by Microsoft) that hosts your Git repositories in the cloud. It allows you to back up your code and collaborate with others over the internet.
The Core Workflow
The daily life of a developer involves running three commands constantly in the terminal. You make some code changes in VS Code, and then you want to save them permanently to your timeline.
# 1. Stage the files (Prepare them to be saved)
git add .
# 2. Commit the files (Save them to the timeline with a message)
git commit -m "Added responsive navbar"
# 3. Push the files (Send the timeline to GitHub)
git push origin main
Branching: The Killer Feature
Imagine you have a perfectly working website, but you want to try adding a crazy new dark mode. You don't want to break the working site. Git allows you to create a "Branch".
A branch is a parallel universe. You can switch to a new branch, break all the code, experiment, and if it fails, you just delete the branch and return to the main universe (the main branch) completely untouched. If the dark mode works perfectly, you can "merge" it into the main branch.
Merge Conflicts
If you and your coworker edit the exact same line of code in the same file, Git will throw a "Merge Conflict". It is basically saying, "I don't know which version is the correct one, please tell me." You will have to open the file and manually choose which code to keep.
Mini Task: Host for Free
- Create an account on GitHub.
- Push a simple HTML/CSS project to a public repository.
- Go to the repository Settings -> Pages, and select the Main branch. GitHub will instantly host your website for free and give you a live URL!