Skip to content
· 8 min read · 0 views

Collaborate Smarter: Version Control with Git & GitHub

A practical guide to Git version control and GitHub collaboration, covering repositories, commits, branches, merging, pull requests, and real-world team workflows.

// table of contents (37 sections)

If you’ve ever had files like final_v2_REAL_FINAL.docx, you need Git.

Version control is not optional in professional development. Every company, every team, every serious project uses it. This guide covers the essentials you need to collaborate effectively.


What is Version Control?

Version control is a system that records changes to files over time so you can recall specific versions later.

Without Git

project/
├── main.dart
├── main_backup.dart
├── main_old.dart
├── main_final.dart
├── main_REAL_final.dart
└── main_please_work.dart

With Git

project/
├── .git/          ← Everything tracked here
└── main.dart      ← Just one file, full history saved

Git lets you:

  • Track every change with a message explaining why
  • Go back to any previous version
  • Work on features in parallel with branches
  • Collaborate without overwriting each other’s work

Git vs GitHub

These are not the same thing:

GitGitHub
WhatVersion control toolCode hosting platform
WhereRuns on your machineRuns in the cloud
DoesTracks changes, branching, mergingStores repos, PRs, issues, CI/CD
Needed?Yes, alwaysOptional (but highly recommended)

Other Git hosting platforms: GitLab, Bitbucket, Gitea.


Setup

Install Git

# macOS
brew install git

# Ubuntu/Debian
sudo apt install git

# Verify
git --version

Configure

git config --global user.name "Muhammad Abdu Ar Rahman"
git config --global user.email "your@email.com"

# Optional: set default branch name
git config --global init.defaultBranch main

SSH Key (for GitHub)

# Generate key
ssh-keygen -t ed25519 -C "your@email.com"

# Copy public key
cat ~/.ssh/id_ed25519.pub
# Paste in GitHub → Settings → SSH and GPG Keys → New SSH Key

Repository Basics

A repository (repo) is a project folder tracked by Git.

Create a New Repo

# Initialize in existing folder
mkdir my-project && cd my-project
git init

# Or clone an existing repo
git clone git@github.com:username/repo.git

The Three Areas

Understanding these three areas is the key to understanding Git:

Working Directory    →    Staging Area    →    Repository
(where you edit)        (git add)          (git commit)
AreaCommandWhat It Holds
Working directoryYour filesCurrent state of files
Staging areagit addChanges ready to commit
Repositorygit commitPermanent history

Core Commands

The Daily Workflow

# 1. Check what changed
git status

# 2. See the actual changes
git diff

# 3. Stage specific files
git add main.dart
git add .              # Stage everything

# 4. Commit with a message
git commit -m "Add login validation logic"

# 5. Push to remote
git push origin main

Viewing History

# Full log
git log

# Compact log
git log --oneline

# Visual branch graph
git log --oneline --graph --all

# See who changed what
git blame main.dart

Undoing Things

# Unstage a file (keeps your changes)
git reset HEAD main.dart

# Discard changes in a file (WARNING: permanent)
git checkout -- main.dart

# Undo last commit (keeps changes staged)
git reset --soft HEAD~1

# Undo last commit (discards changes)
git reset --hard HEAD~1

Be careful with --hard. It permanently deletes uncommitted changes. Only use it when you’re certain.


Branching

Branches let you work on different features in parallel without affecting the main code.

Why Branch?

main:       A — B — C ———————————————— M (merge)
                 \                     /
feature:          D — E — F — G ———/
  • main stays stable while you experiment
  • Each feature gets its own branch
  • Merge only when the feature is complete and tested

Branch Commands

# Create and switch to a new branch
git checkout -b feature/login

# Or with newer syntax
git switch -c feature/login

# Switch between branches
git switch main
git switch feature/login

# List all branches
git branch -a

# Delete a merged branch
git branch -d feature/login

Branch Naming Conventions

feature/add-user-profile
bugfix/fix-login-crash
hotfix/patch-security-issue
refactor/simplify-auth-logic

Merging & Resolving Conflicts

Merge a Branch

# Switch to target branch
git switch main

# Merge feature branch into it
git merge feature/login

# Push the result
git push origin main

Merge Conflicts

When two branches modify the same line, Git can’t auto-merge. You get a conflict:

<<<<<<< HEAD (main)
print('Welcome back, $name!');
=======
print('Hello, $name!');
>>>>>>> feature/login

Resolution steps:

  1. Open the conflicted file
  2. Choose which version to keep (or combine both)
  3. Remove the conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Stage and commit
# After resolving conflicts
git add .
git commit -m "Merge feature/login, resolve greeting conflict"

Rebase (Alternative to Merge)

Rebase replays your commits on top of another branch, creating a cleaner history:

git switch feature/login
git rebase main
MergeRebase
Preserves complete historyCreates linear history
Creates a merge commitNo merge commit
Safe for shared branchesCan cause issues on shared branches
Use for: merging featuresUse for: cleaning up local work

Rule of thumb: Never rebase commits that have been pushed to a shared branch.


Remote Repositories

Connecting to GitHub

# Add a remote
git remote add origin git@github.com:username/repo.git

# View remotes
git remote -v

# Push and set upstream
git push -u origin main

# After first push, just use
git push

Syncing with Remote

# Download new changes (don't merge)
git fetch origin

# Download and merge
git pull origin main

# Pull with rebase (cleaner history)
git pull --rebase origin main

Forking Workflow (Open Source)

1. Fork the repo on GitHub (creates your copy)
2. Clone your fork
3. Create a feature branch
4. Make changes and commit
5. Push to your fork
6. Open a Pull Request to the original repo

Pull Requests

A Pull Request (PR) is a request to merge your changes into another branch. It’s where code review happens.

Creating a PR

# 1. Push your feature branch
git push origin feature/login

# 2. Go to GitHub → your repo → "Compare & pull request"
# 3. Write a clear title and description
# 4. Request reviewers
# 5. Wait for approval
# 6. Merge when approved

Good PR Description

## What
Add email/password login validation

## Why
Users could submit empty forms, causing server errors

## Changes
- Add email format validation with regex
- Add password minimum length check (8 chars)
- Show error messages for invalid input

## Testing
- [ ] Empty email shows error
- [ ] Invalid email format shows error
- [ ] Short password shows error
- [ ] Valid credentials pass through

Code Review Etiquette

As reviewer:

  • Be constructive, not critical
  • Explain why something should change
  • Approve when you’re satisfied

As author:

  • Keep PRs small and focused
  • Respond to every comment
  • Push fixes as new commits (don’t force-push during review)

.gitignore

Not everything belongs in Git. Create a .gitignore file:

# Dependencies
node_modules/
vendor/

# Build output
dist/
build/
*.exe

# IDE
.vscode/
.idea/
*.swp

# OS files
.DS_Store
Thumbs.db

# Environment variables (IMPORTANT)
.env
.env.local

# Sensitive files
*.key
*.pem
credentials.json

Team Workflow

Feature Branch Workflow (Most Common)

1. git switch main
2. git pull origin main
3. git switch -c feature/new-feature
4. Code, commit, commit, commit...
5. git push origin feature/new-feature
6. Open Pull Request
7. Team reviews
8. Merge to main
9. Delete feature branch
10. git pull origin main (everyone syncs)

Commit Message Convention

type(scope): description

feat(auth): add login validation
fix(api): handle null response from server
docs(readme): update installation steps
refactor(utils): simplify date formatting
test(auth): add login unit tests
chore(deps): update dependencies
TypeWhen
featNew feature
fixBug fix
docsDocumentation
refactorCode cleanup (no behavior change)
testAdding tests
choreMaintenance tasks

Quick Reference

TaskCommand
Init repogit init
Clone repogit clone <url>
Check statusgit status
Stage changesgit add .
Commitgit commit -m "message"
Pushgit push origin main
Pullgit pull origin main
Create branchgit switch -c feature/name
Switch branchgit switch main
Merge branchgit merge feature/name
View loggit log --oneline

What’s Next?

With version control mastered, you’re ready to learn about databases and SQL so your apps can store and retrieve data properly.

Bismillah, happy committing! 🚀

You might also like

Enjoyed This Post?

Want to discuss the topic, have questions, or looking to collaborate on something similar? Drop a comment below or reach out directly.

Discussion