Introduction to Git
Git is a distributed version control system that helps developers track changes in their code and collaborate with others.
Key Concept: Git takes snapshots of your project called "commits" that you can revert to at any time.
Why use Git? It allows you to track changes, experiment with new features safely, and collaborate efficiently with teams.
Git vs GitHub
Git:
Version control system that runs locally on your machine
GitHub:
Cloud-based hosting service that uses Git for version control
This tutorial focuses on Git commands you can use locally before pushing to services like GitHub.
Essential Git Commands
Initialize a Repository
git init
Creates a new Git repository in your current directory.
Stage Changes
git add <file>git add .
Prepares changes for commit (staging area).
Commit Changes
git commit -m "Your message"
Takes a snapshot of your staged changes with a descriptive message.
Check Status
git status
Shows which files are modified, staged, or untracked.
Git Workflow
Basic Workflow
-
1
Initialize or clone a repository
Start tracking your project or get a copy of an existing one
-
2
Create a branch for your changes
Keep your work isolated from the main codebase
-
3
Make changes and commit them
Stage and save your work with descriptive messages
-
4
Push your branch to the remote
Share your work with others
-
5
Create a pull/merge request
Propose your changes to be merged into the main branch
Branch Visualization
Best Practices
- Commit often with meaningful messages
- Create branches for new features or bug fixes
- Pull changes frequently to stay up-to-date
- Review changes before merging
Interactive Terminal
Try Git Commands
Type basic Git commands below to see simulated output:
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Try these commands: git add ., git commit -m "message", git branch new-feature
Common Scenarios
Undo the last commit
Keep changes but undo the commit
Discard local changes
Revert files to last committed state
Resolve merge conflicts
What to do when Git can't auto-merge
Temporarily save changes
When you need to switch branches but aren't ready to commit
Git Cheat Sheet
Setup & Config
- git config --global user.name "Your Name" Set your username
- git config --global user.email "email@example.com" Set your email
- git config --list View configuration
Inspection
- git log Show commit history
- git log --oneline Compact commit history
- git diff Show unstaged changes
Undoing
- git reset <file> Unstage a file
- git checkout -- <file> Discard changes
- git revert <commit> Create new commit that undoes a previous commit