1οΈβ£ Git vs. GitHub – What's the Difference?
πΉ Git – A version control system that tracks changes in your code locally.
πΉ GitHub – A cloud-based repository hosting service that helps you collaborate with others and store your Git projects.
π‘ Think of Git as the engine and GitHub as the garage where you store your projects.
2οΈβ£ Setting Up Git & GitHub
β
Install Git (if you haven’t already)
πΉ Windows: Download from git-scm.com
πΉ macOS: Use brew install git
πΉ Linux: Use sudo apt install git
β
Configure Git (First-Time Setup)
This ensures your commits have the correct identity.
3οΈβ£ Basic Git Commands You Should Know
β
Initialize a Repository
This creates a new Git repository in your project folder.
β
Clone an Existing Repository
This copies a remote GitHub repository to your local machine.
β
Check Status
Shows the current state of your working directory.
β
Add Files to Staging Area
This prepares files for committing.
β
Commit Changes
Records changes to the repository with a message describing what changed.
β
Push Changes to GitHub
Sends your local commits to the remote repository on GitHub.
4οΈβ£ Working with Branches & Collaboration
β
Create a New Branch
Branches allow multiple people to work on different features without affecting the main code.
β
Merge a Branch into Main
Brings changes from feature-branch into main.
β
Delete a Branch (After Merging)
Removes the branch to keep things clean.
β
Pull the Latest Changes (Stay Up to Date)
Fetches the latest code from GitHub to prevent conflicts.
5οΈβ£ Handling Merge Conflicts
π‘ When two people edit the same part of a file, Git gets confused. Here's how to fix it:
β
Identify the Conflict
Run:
Git will show the conflicting files.
β
Manually Fix the Conflict
Open the file and edit the conflicting sections (marked by <<<<<<<
, =======
, >>>>>>>
).
β
Mark as Resolved & Commit
6οΈβ£ Using GitHub Effectively
β
Forking a Repository
If you want to contribute to a project you don’t own, click “Fork” on GitHub to create a copy in your own account.
β
Pull Requests (PRs)
To contribute to a project:
- Create a feature branch.
- Make changes and push them.
- Go to GitHub → Open a Pull Request (PR).
- Describe your changes and submit.
π‘ The project maintainer can now review and merge your changes.
β
Using Issues for Bug Tracking
If you find a bug or need a feature, create an Issue in GitHub and discuss with collaborators.
7οΈβ£ Git Best Practices (Pro Tips) π―
β
Write Meaningful Commit Messages
Bad: git commit -m "Fixed stuff"
Good: git commit -m "Fixed login authentication bug #45"
β
Commit Often, but Keep Commits Small
Frequent commits make it easier to track changes and debug.
β
Use .gitignore
to Exclude Unnecessary Files
Create a .gitignore
file and add files to ignore (e.g., logs, dependencies, secrets).
β
Use Feature Branches
Don’t work directly on main
. Create a separate branch for each new feature.
β
Keep Your Fork Updated
If you fork a repository, update it regularly:
π TL;DR – Git & GitHub Essentials
Command |
Purpose |
git init |
Initialize a new repository |
git clone <repo-url> |
Copy a repository from GitHub |
git add . |
Add all files to staging |
git commit -m "Message" |
Save changes locally |
git push origin main |
Upload changes to GitHub |
git pull origin main |
Get latest changes from GitHub |
git branch new-feature |
Create a new branch |
git checkout new-feature |
Switch to a branch |
git merge new-feature |
Merge a branch into main |
git status |
Check changes |
git log |
View commit history |