Git & GitHub: Complete Guide with Setup and Cheat Sheet

Git & GitHub: Complete Guide with Setup and Cheat Sheet

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:

  1. Download Git from git-scm.com.

  2. Run the installer and follow the default options.

  3. Verify installation:

     git --version
    

Mac:

  1. Open Terminal and run:

     brew install git
    
  2. 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

  1. Sign up on GitHub.

  2. Verify your email and set up your profile.

Step 4: Connect Git to GitHub (Using SSH)

  1. Generate an SSH Key:

     ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
    
  2. Copy the key:

     cat ~/.ssh/id_rsa.pub
    
  3. Add the key to GitHub:

    • Go to GitHub → Settings → SSH and GPG keys → New SSH Key

    • Paste the copied key and save.

  4. 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)

  1. Go to your GitHub repository.

  2. Click Pull RequestsNew Pull Request.

  3. 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

CommandDescription
git initInitialize a new Git repository
git clone <repo-url>Clone an existing repository
git statusCheck the status of the working directory
git add .Stage all changes
git commit -m "message"Commit staged changes
git logView commit history

Branching & Merging

CommandDescription
git branchList 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

CommandDescription
git remote add origin <url>Add a remote repository
git remote -vView remote repositories
git push -u origin <branch-name>Push changes to a remote branch
git pull origin <branch-name>Pull latest changes

Undo & Reset

CommandDescription
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 stashSave changes without committing
git stash popRestore stashed changes

Here are some best practices for managing branches effectively in Git:

🔹 Branching Strategy

  1. Use a Standard Naming Convention

    • feature/<feature-name> – For new features

    • bugfix/<bug-id> – For bug fixes

    • hotfix/<issue-id> – For urgent fixes

    • release/<version> – For release branches

    • develop – For ongoing development

    • main (or master) – Stable production-ready branch

  2. 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

  1. Keep Branches Short-Lived

    • Merge and delete feature branches once the work is done.

    • Avoid long-lived branches that become outdated.

  2. Rebase Regularly

    • Keep your branch updated with main or develop using git rebase.

    • Avoid rebasing shared branches.

  3. Use Pull Requests (PRs)

    • Always create a PR before merging.

    • Get code reviews to ensure quality and reduce bugs.

  4. Automate Merging and Deletion

    • Use GitHub Actions, GitLab CI/CD, or scripts to delete merged branches automatically.
  5. 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.