Git Basics
Version control is one of the fundamental tools every software engineer needs to know.
Luckily for us, it’s freaking awesome.
In this lesson, we’ll learn the basics of Git and understand how it helps manage and track changes in your codebase.
This Lesson Will Cover
- 📝 What Git is and why it’s important
- 🧠 Core concepts of Git
- 🖥️ Installing and setting up Git
- 📜 Basic Git commands (add, commit, status, log)
- 🚀 Pushing code to GitHub
What is Git?
Git was created by Linus Torvalds (the same person who created Linux) in 2005. He named it “git” as a self-deprecating joke—in British slang, git means “unpleasant person.” Linus once said
“I’m an egotistical bastard, and I name all my projects after myself. First Linux, now git.”
It’s an open-source version control system that lets us track changes in our code and collaborate with others.
We could think of it as a time machine for our projects, that allows us to go back and forth between different points in time and even work on multiple versions concurrently like parallel code universes!.
Core Concepts in Git
Before jumping into commands, let’s break down a few essential concepts:
- Repository (Repo): A Git repository is just a project folder, but with Git’s version control system layered on top. It stores the code and all the version history.
- Commit: A commit is a record of changes made to a codebase. We use commits to save our progress. Effectively this represents a snapshot of our code at a specific point in time.
- Staging Area: A middle ground where changes are prepared before being committed.
- Branch: A parallel version of the code with its own history and changes that can be developed independently of other branches.
Getting Started with Git
1. Installing Git
First, we’ll need to install Git.
For Linux (Debian/Ubuntu):
sudo apt-get update
sudo apt-get install git
For macOS (using Homebrew):
brew install git
For Windows, our best bet might be to download the installer from git-scm.com.
Once installed on our operating system, we can verify it with:
git --version
2. Initializing a Repository
Start version-controlling a new project by initializing a Git repository:
# Setup a project
mkdir my-project
cd my-project
echo 'Hello world!' >> README.md
git init
This creates a hidden .git folder where Git stores all its data.
3. Tracking Changes
Add files to the project and start tracking them:
git add README.md
We can add all files in the current folder with:
git add .
4. Saving Changes
Save our changes with a commit:
git commit -m "Initial commit"
The -m flag allows us to add a descriptive message about what this commit does.
5. Checking Status
At any point, we can check what’s happening in the repository:
git status
This command shows which files are being tracked, which have changes, and which are staged for commit.
6. Viewing History
To view the commit history:
git log
Pushing Code to GitHub
1. Create a GitHub Repository
First, create a new repository on GitHub:
- Go to github.com and sign in.
- Click the ‘+’ button and select ‘New repository’.
- Name the repository (e.g., “my-project”).
- Leave it empty (don’t initialize with README).
2. Git Configuration
Configure your Git username and email to match your GitHub account:
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
3. Authentication
Before pushing to GitHub, we’ll need to authenticate. The best way to do this is to set up SSH keys.
Here are the first-time setup instructions:
# Generate SSH key
ssh-keygen -t ed25519
# Add key to ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Add the public key to GitHub
# Copy the contents of ~/.ssh/id_ed25519.pub to GitHub Settings > SSH keys
4. Link Local Repository to GitHub
Connect the local repository to GitHub:
git remote add origin https://github.com/username/my-project.git
5. Push The Code
There’s been a trend towards using main instead of master. This is just a naming convention thing. And—for what it’s worth—I’m in favor of using main.
# Rename from master to main (optional)
git branch -m master main
Push local commits to GitHub:
# First time push with -u flag to set upstream branch
git push -u origin main
# Subsequent pushes can just use
git push
Now the code is on GitHub! We can view it, share it, and collaborate with others.
Conclusion
Git can do amazing things.[1] Watching a git wizard in action is a remarkable thing.
Key Takeaways
🖥️ Git is an open-source version control system that tracks changes in code.
📂 Repositories store the project’s version history and enable collaboration.
📸 Commits are snapshots of a project at specific points in time.
✅ The staging area allows us to review changes before committing.
📝 Basic Git commands include
git add,git commit,git status, andgit log.🌍 GitHub is a remote hosting service that lets us store and share repositories.
🚀 To push code to GitHub, set up authentication and link your local repository.
Next: Collaboration with Git
Up next, we’ll learn about branching, merging, and collaborative workflows.
Footnotes
Scott Chacon’s FOSDEM 2024 talk on Git Tips and Tricks (45 minute presentation). Scott cofounded GitHub. ↩︎
Comments (0)
No comments yet.