1. Introduction to Git & GitHub
What is Git?
Git is a distributed version control system that helps developers track changes in their code, collaborate with teams, and manage project versions efficiently.
What is GitHub?
GitHub is a cloud-based platform that provides hosting for Git repositories and enables collaboration, issue tracking, and project management.
2. Setting Up Git & GitHub
Step 1: Install Git
Windows:
Download Git from git-scm.com.
Run the installer and follow the default options.
Verify installation:
git --version
Mac:
Open Terminal and run:
brew install git
Verify installation:
git --version
Linux (Ubuntu/Debian):
sudo apt update
sudo apt install git
Verify installation:
git --version
Step 2: Configure Git
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
Check the configuration:
git config --list
Step 3: Create a GitHub Account
Sign up on GitHub.
Verify your email and set up your profile.
Step 4: Connect Git to GitHub (Using SSH)
Generate an SSH Key:
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
Copy the key:
cat ~/.ssh/id_rsa.pub
Add the key to GitHub:
Go to GitHub → Settings → SSH and GPG keys → New SSH Key
Paste the copied key and save.
Test the connection:
ssh -T git@github.com
3. Working with Git & GitHub
Step 1: Initialize a Git Repository
git init
Step 2: Clone a GitHub Repository
git clone <repository-url>
Example:
git clone git@github.com:username/repo-name.git
Step 3: Create a New Branch
git checkout -b feature-branch
Step 4: Add and Commit Changes
git add .
git commit -m "Initial commit"
Step 5: Push Changes to GitHub
git push origin feature-branch
Step 6: Create a Pull Request (PR)
Go to your GitHub repository.
Click Pull Requests → New Pull Request.
Compare changes and submit for review.
Step 7: Merge Branch
After approval, merge the branch into main
:
git checkout main
git merge feature-branch
git push origin main
Step 8: Delete the Branch
git branch -d feature-branch
git push origin --delete feature-branch
4. Git Command Cheat Sheet
Basic Git Commands
Command | Description |
git init | Initialize a new Git repository |
git clone <repo-url> | Clone an existing repository |
git status | Check the status of the working directory |
git add . | Stage all changes |
git commit -m "message" | Commit staged changes |
git log | View commit history |
Branching & Merging
Command | Description |
git branch | List all branches |
git checkout -b <branch-name> | Create and switch to a new branch |
git merge <branch-name> | Merge a branch into the current branch |
git branch -d <branch-name> | Delete a local branch |
git push origin --delete <branch-name> | Delete a remote branch |
Remote Repositories
Command | Description |
git remote add origin <url> | Add a remote repository |
git remote -v | View remote repositories |
git push -u origin <branch-name> | Push changes to a remote branch |
git pull origin <branch-name> | Pull latest changes |
Undo & Reset
Command | Description |
git reset --hard <commit-id> | Reset to a specific commit (removes changes) |
git revert <commit-id> | Create a new commit that undoes a previous commit |
git stash | Save changes without committing |
git stash pop | Restore stashed changes |
Here are some best practices for managing branches effectively in Git:
🔹 Branching Strategy
Use a Standard Naming Convention
feature/<feature-name>
– For new featuresbugfix/<bug-id>
– For bug fixeshotfix/<issue-id>
– For urgent fixesrelease/<version>
– For release branchesdevelop
– For ongoing developmentmain
(ormaster
) – Stable production-ready branch
Follow a Branching Model
Git Flow – Best for structured teams (Uses
develop
,main
,feature
,release
,hotfix
)GitHub Flow – Simple and effective (Uses
main
+ feature branches)Trunk-based Development – Good for CI/CD
🔹 Branch Management
Keep Branches Short-Lived
Merge and delete feature branches once the work is done.
Avoid long-lived branches that become outdated.
Rebase Regularly
Keep your branch updated with
main
ordevelop
usinggit rebase
.Avoid rebasing shared branches.
Use Pull Requests (PRs)
Always create a PR before merging.
Get code reviews to ensure quality and reduce bugs.
Automate Merging and Deletion
- Use GitHub Actions, GitLab CI/CD, or scripts to delete merged branches automatically.
Avoid Direct Commits to Main
- Work in a branch, then merge via PR.
🔥 Final Thoughts
By following these steps, you can set up Git and GitHub, manage repositories efficiently, and collaborate on projects seamlessly. Keep practicing with different Git commands and explore advanced features like Git hooks, rebasing, and automation!
😊 Thanks for reading, keep exploring and follow for more.