How do you use Git and GitHub effectively?
Arpit Nuwal

 

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)

sh
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"

This ensures your commits have the correct identity.


3️⃣ Basic Git Commands You Should Know

βœ… Initialize a Repository

sh
git init

This creates a new Git repository in your project folder.

βœ… Clone an Existing Repository

sh
git clone https://github.com/username/repository.git

This copies a remote GitHub repository to your local machine.

βœ… Check Status

sh
git status

Shows the current state of your working directory.

βœ… Add Files to Staging Area

sh
git add filename # Add a specific file git add . # Add all modified and new files

This prepares files for committing.

βœ… Commit Changes

sh
git commit -m "Your commit message"

Records changes to the repository with a message describing what changed.

βœ… Push Changes to GitHub

sh
git push origin main # Push to the main branch

Sends your local commits to the remote repository on GitHub.


4️⃣ Working with Branches & Collaboration

βœ… Create a New Branch

sh
git branch feature-branch git checkout feature-branch # OR git checkout -b feature-branch # Creates and switches to new branch

Branches allow multiple people to work on different features without affecting the main code.

βœ… Merge a Branch into Main

sh
git checkout main git merge feature-branch

Brings changes from feature-branch into main.

βœ… Delete a Branch (After Merging)

sh
git branch -d feature-branch

Removes the branch to keep things clean.

βœ… Pull the Latest Changes (Stay Up to Date)

sh
git pull origin main

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:

sh
git status

Git will show the conflicting files.

βœ… Manually Fix the Conflict

Open the file and edit the conflicting sections (marked by <<<<<<<, =======, >>>>>>>).

βœ… Mark as Resolved & Commit

sh
git add conflict-file git commit -m "Resolved merge conflict"

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:

  1. Create a feature branch.
  2. Make changes and push them.
  3. Go to GitHub → Open a Pull Request (PR).
  4. 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).

sh
node_modules/ .env .DS_Store

βœ… 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:

sh
git remote add upstream https://github.com/original/repo.git git fetch upstream git merge upstream/main

πŸš€ 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