3. π§ Git for Everyday Development#
Git is the most widely used version control system in software development.
It helps you track changes, collaborate safely, and recover mistakes.
This guide is written for beginners who want to understand why each Git step matters and when to use it in real projects.
3.1. βοΈ 1. Setting Up Git (Once per Machine)#
3.1.1. π§© Why this matters#
Before using Git on any project, you must tell Git who you are and how it should behave.
This information will appear in every commit and helps collaborators know who made which changes.
3.1.2. π When to do this#
Do it once after installing Git β it applies globally to all repositories on your computer.
3.1.3. π§ Commands#
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global fetch.prune true
git config --global core.autocrlf input # for mac/linux
# or on Windows:
# git config --global core.autocrlf true
3.1.4. π‘ Real-world tip#
If you work with GitHub, set up SSH keys to avoid typing passwords every time:
ssh-keygen -t ed25519 -C "you@example.com"
Add your public key to GitHub under Settings β SSH and GPG keys.
3.2. π 2. Initialize a Local Repository#
3.2.1. π§© Why this matters#
A repository (repo) is where Git tracks your projectβs files and their changes.
Initializing one tells Git, βstart version control here.β
3.2.2. π When to use it#
When you start a new project from scratch or want to version-control an existing folder.
3.2.3. π§ Commands#
mkdir my-project && cd my-project
git init
echo "# My Project" > README.md
git add .
git commit -m "chore: initial commit"
3.2.4. π‘ Real-world tip#
Use a .git folder (created automatically) to store all Git metadata.
Do not delete it β itβs your project history!
Check current status anytime:
git status
3.3. π 3. Connect to a Remote Repository (GitHub, GitLab, etc.)#
3.3.1. π§© Why this matters#
A remote repository lives on the internet and allows team collaboration.
Connecting your local repo to a remote lets you share your code or back it up.
3.3.2. π When to use it#
When you want to push your local work to GitHub (or clone from an existing repo).
3.3.3. π§ Commands#
Case 1 β Push local repo to remote
git remote add origin git@github.com:yourname/my-project.git
git branch -M main
git push -u origin main
Case 2 β Clone an existing remote
git clone git@github.com:org/project.git
cd project
3.3.4. π‘ Real-world tip#
Use git remote -v to check connected remotes.
You can rename or replace them later using:
git remote set-url origin <new-url>
3.4. πΏ 4. Branching β Working on Features Safely#
3.4.1. π§© Why this matters#
Branches let you work on new features without breaking the main code.
You can later merge tested changes back into the main branch.
3.4.2. π When to use it#
Before starting a new feature, bug fix, or experiment.
3.4.3. π§ Commands#
# Create and switch to a new branch
git switch -c feat/login-page
# View branches
git branch
# Push it to remote for collaboration
git push -u origin feat/login-page
3.4.4. π‘ Real-world tip#
Prefix branch names:
feat/,fix/,chore/,docs/, etc.Delete branches after merging to keep your repo clean:
git branch -d feat/login-page git push origin --delete feat/login-page
3.4.5. π§ͺ Hands-on task#
Create a branch
feat/homepageEdit files and commit changes
Push the branch to GitHub
Open a Pull Request from that branch
3.5. π 5. Merging and Keeping Up-to-Date#
3.5.1. π§© Why this matters#
Merging combines work from different branches.
Keeping your feature branch updated avoids big conflicts later.
3.5.2. π When to use it#
When your teammate updates
main, and you want those changesWhen youβve finished your feature and want to merge it back
3.5.3. π§ Commands#
Update your feature branch
git fetch origin
git switch main
git pull
git switch feat/homepage
git merge main
Merge feature into main
git switch main
git merge feat/homepage
git push
3.5.4. π‘ Real-world tip#
If your team prefers clean, linear history, use rebase instead of merge:
git switch feat/homepage
git fetch origin
git rebase origin/main
Rebase rewrites commit order but avoids messy merge commits.
3.6. βοΈ 6. Solving Merge Conflicts#
3.6.1. π§© Why this matters#
When two people edit the same file lines differently, Git canβt decide which one to keep.
Thatβs a merge conflict β you must resolve it manually.
3.6.2. π When to use it#
During git merge or git rebase if Git reports conflicts.
3.6.3. π§ How to fix#
Open conflicted file β youβll see:
<<<<<<< HEAD your code ======= teammate's code >>>>>>> main
Manually edit to the correct version.
Then mark resolved:
git add filename git merge --continue # or git rebase --continue
3.6.4. π‘ Real-world tip#
Use IDE merge tools (VS Code, PyCharm, etc.) to make it visual.
If it gets messy:
git merge --abort
and try again after pulling latest changes.
3.7. π« 7. Using .gitignore β Keep Unwanted Files Out of Git#
3.7.1. π§© Why this matters#
Some files (like build outputs, logs, or credentials) should never be tracked by Git.
.gitignore defines which files to exclude.
3.7.2. π When to use it#
At the start of your project, or when adding new tools that generate temporary files.
3.7.3. π§ Example#
# Dependencies
node_modules/
venv/
__pycache__/
# Builds
dist/
build/
# Logs
*.log
# Environment
.env
# System files
.DS_Store
Thumbs.db
3.7.4. π‘ Real-world tip#
If you accidentally tracked ignored files:
git rm -r --cached .
git add .
git commit -m "chore: apply .gitignore"
3.8. πΎ 8. Committing, Pushing, and Reviewing Changes#
3.8.1. π§© Why this matters#
Each commit represents a meaningful snapshot of your work.
Pushing uploads commits to the remote repo so teammates can review them.
3.8.2. π When to use it#
After completing a small, testable unit of work (not too big, not too small).
3.8.3. π§ Commands#
git status # check what changed
git add file.js # stage specific file
git add . # stage all
git commit -m "feat: add login validation"
git push # send to remote
3.8.4. π‘ Real-world tip#
Write clear commit messages:
β
fix: correct password validation regexβ
update codeorstuff fixed
To amend last commit (e.g., forgot a file):
git add missing.js
git commit --amend
3.9. π§° 9. Undo, Recover, and Experiment Safely#
3.9.1. π§© Why this matters#
Git makes mistakes reversible β you can go back in time.
Understanding these tools helps you experiment confidently.
3.9.2. π When to use it#
When you accidentally delete, break, or commit something wrong.
3.9.3. π§ Commands#
git restore file.txt # undo local changes
git reset HEAD~1 # undo last commit (keep files)
git reset --hard HEAD~1 # remove last commit completely
git reflog # view all actions (time travel log)
Recover old versions:
git checkout <commit-id>
3.9.4. π‘ Real-world tip#
Never use --hard on shared branches like main.
If you break something, look in git reflog β itβs your safety net.
3.11. π 11. Typical Daily Git Workflow#
3.11.1. π§© Why this matters#
A clear daily routine prevents conflicts and keeps your repository organized.
3.11.2. π When to use it#
Every workday β itβs your main habit loop.
3.11.3. π§ Example#
# 1. Update main
git switch main
git pull
# 2. Create a feature branch
git switch -c feat/dashboard
# 3. Work & commit
git add .
git commit -m "feat: add dashboard layout"
# 4. Sync often
git fetch origin
git merge origin/main
# 5. Push and create a Pull Request
git push -u origin feat/dashboard
3.11.4. π‘ Real-world tip#
Pull before starting work every day to reduce conflicts.
Always test before committing to keep history clean.
3.12. π§ 12. Summary and Next Steps#
Concept |
Why it matters |
When to use |
|---|---|---|
|
Start version control |
Beginning of any project |
|
Save progress with history |
After finishing a small unit of work |
|
Work safely on new ideas |
Before big feature or fix |
|
Combine work |
When integrating changes |
|
Share work |
With teammates / before PR |
|
Exclude unwanted files |
Always |
|
Pause work temporarily |
When switching tasks |
3.12.1. β You learned:#
How to set up and initialize Git
How to connect with remote repositories
How to branch, merge, and handle conflicts
How to construct a
.gitignoreHow to safely undo and recover work
Next steps: Learn about GitHub Pull Requests, rebasing strategies, and advanced history cleanup (rebase -i).
Git isnβt just about commands β itβs a safety net and collaboration tool.
The more you commit (both literally and figuratively), the easier it gets!