Git Introduction
Git keeps a history of your project, so every change is saved and can be brought back.
The Problem Git Solves
Without version control, many people save copies like this:
Example
website-final.zip
website-final-2.zip
website-final-REAL.zip
website-final-REAL-ali-changes.zipIt is confusing, it wastes space, and when two people edit the same file, someone's work gets lost. Git replaces all of this with one folder and a clean history.
What is Version Control?
A version control system (VCS) records changes to files over time. With it you can:
- See what changed, when, and who changed it
- Go back to any earlier version
- Work on new features without breaking the main code
- Combine work from many people
Git is the most popular VCS. It was created in 2005 by Linus Torvalds, the creator of Linux. It is free and open source.
Git vs GitHub
| Git | GitHub | |
|---|---|---|
| What it is | A program on your computer | A website |
| Works offline | Yes | No |
| Job | Tracks versions of your files | Stores repositories online, adds teamwork tools |
| Made by | Linus Torvalds and the community | GitHub, owned by Microsoft |
| Alternatives | (Git is the standard) | GitLab, Bitbucket |
You can use Git without GitHub, but GitHub needs Git.
Installing Git
- Windows: download Git from the official git-scm website and install with the default options. It includes Git Bash, a terminal for Git commands.
- macOS: run
git --versionin Terminal; if Git is missing, macOS offers to install it. - Linux:
sudo apt install giton Ubuntu or Debian.
Check that it works:
Example
git --versiongit version 2.47.0
Your version number may be different.
First-time Setup
Tell Git who you are. This name and email are saved with every commit you make:
Example
git config --global user.name "Ayesha Khan"
git config --global user.email "ayesha@example.com"
git config --global init.defaultBranch mainThe last line makes new projects start with a branch called main, the modern standard.
Check your settings:
Example
git config --listuser.name=Ayesha Khan user.email=ayesha@example.com init.defaultbranch=main
Use the same email you will use for GitHub. Then GitHub can link your commits to your profile.
Key Words
| Word | Meaning |
|---|---|
| Repository (repo) | A project folder that Git tracks |
| Commit | A saved snapshot of the project |
| Branch | A separate line of work |
| Remote | A copy of the repository on another computer or on GitHub |
| Clone | Download a full copy of a repository |
Exercise
Exercise
Which command saves your name for all Git projects on your computer?
