Git and GitHub Cheat Sheet Every Developer Should Bookmark


What is Git?

Git is a distributed version control system (VCS) that helps developers track changes in source code, collaborate with teams, and maintain a history of project modifications.

Key Features

  • Tracks file changes
  • Supports team collaboration
  • Maintains project history
  • Allows rollback to previous versions
  • Fast and lightweight

Created By

Git was created by Linus Torvalds in 2005 for Linux kernel development.

What is GitHub?

GitHub is a cloud-based platform that hosts Git repositories online and provides collaboration features.

GitHub Features

  • Repository hosting
  • Pull Requests
  • Issue Tracking
  • Project Management
  • GitHub Actions (CI/CD)
  • Code Reviews
  • Team Collaboration

Why Use Git and GitHub?

Benefits of Git

  • Version control
  • Backup and recovery
  • Branching and merging
  • Distributed architecture

Benefits of GitHub

  • Remote repository storage
  • Team collaboration
  • Open-source contribution
  • Automated workflows

Installing Git

Windows

Download Git from:

https://git-scm.com

Verify installation:

git --version

Expected output:

git version 2.x.x

Linux

sudo apt update
sudo apt install git

Mac

brew install git

Git Configuration

Configure your identity:

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

Check configuration:

git config --list

Creating Your First Repository

Create project folder:

mkdir MyProject
cd MyProject

Initialize Git:

git init

Output:

Initialized empty Git repository

Understanding Git Workflow

Git operates in three areas:

Working Directory
       ↓
Staging Area
       ↓
Repository

Workflow

  • Create or modify files
  • Add files to staging area
  • Commit changes
  • Push to GitHub

Basic Git Commands

Check Status

git status

Add Single File

git add index.html

Add All Files

git add .

Commit Changes

git commit -m "Initial commit"

View History

git log

Compact view:

git log --oneline

Working with Branches

View Branches

git branch

Create Branch

git branch feature-login

Switch Branch

git checkout feature-login

Modern alternative:

git switch feature-login

Create and Switch

git checkout -b feature-login

Merging Branches

Switch to main branch:

git checkout main

Merge feature branch:

git merge feature-login

Resolving Merge Conflicts

Conflict example:

<<<<<<< HEAD
Current Code
=======
Incoming Code
>>>>>>> feature-login

Steps:

  • Open conflicting file
  • Remove conflict markers
  • Keep desired code
  • Save file

Commit changes

git add .
git commit -m "Resolved merge conflict"

Working with Remote Repositories

Add Remote

git remote add origin https://github.com/user/project.git

Verify Remote

git remote -v

Push Code

git push origin main

Pull Changes

git pull origin main

Fetch Changes

git fetch origin

GitHub Fundamentals

Create GitHub Repository

  • Login to GitHub
  • Click New Repository
  • Enter repository name
  • Click Create Repository

Connect local repository:

git remote add origin https://github.com/username/repo.git
git push -u origin main

Cloning Repositories

Copy repository to local machine:

git clone https://github.com/user/project.git

Clone specific branch:

git clone -b develop https://github.com/user/project.git

Pull Requests (PR)

A Pull Request allows developers to propose code changes.

PR Workflow

Create Branch
      ↓
Commit Changes
      ↓
Push Branch
      ↓
Create Pull Request
      ↓
Code Review
      ↓
Merge

Git Stash

Temporarily save uncommitted changes.

Save Changes

git stash

View Stashes

git stash list

Apply Stash

git stash apply

Remove Stash

git stash drop

Git Tags

Tags mark important releases.

Create tag:

git tag v1.0

Push tag:

git push origin v1.0

List tags:

git tag

Git Rebase

Rebase creates a cleaner commit history.

git checkout feature
git rebase main

Benefits:

  • Cleaner history
  • Easier review
  • Linear commits

Git Cherry-Pick

Apply specific commit to another branch.

git cherry-pick COMMIT_ID

Example:

git cherry-pick 6f3d2a4

Undoing Changes

Restore File

git restore file.txt

Unstage File

git restore --staged file.txt

Undo Last Commit

Keep changes:

git reset --soft HEAD~1

Remove changes:

git reset --hard HEAD~1

Git Hooks

Git Hooks automate tasks before or after Git events.

Examples:

  • Code formatting
  • Running tests
  • Security checks
  • Deployment automation

Common hooks:

pre-commit
commit-msg
post-commit
pre-push
post-merge

Git Best Practices

Write Meaningful Commit Messages

Bad:

git commit -m "fix"

Good:

git commit -m "Fix login validation issue"

Commit Frequently

Small commits are easier to review.

Use Branches

Keep main branch stable.

Pull Before Push

git pull origin main

Ignore Unnecessary Files

Create .gitignore:

bin/
obj/
node_modules/
.env

Complete Git & GitHub Cheat Sheet

Repository

git init
git clone URL
git status
git log

Staging

git add .
git add filename
git restore --staged filename

Commit

git commit -m "message"
git commit --amend

Branching

git branch
git branch new-branch
git checkout new-branch
git checkout -b new-branch
git switch new-branch

Merge

git merge branch-name

Remote

git remote add origin URL
git remote -v
git fetch
git pull
git push

Stash

git stash
git stash list
git stash apply
git stash drop

Tags

git tag
git tag v1.0
git push origin v1.0

Undo

git restore file
git reset --soft HEAD~1
git reset --hard HEAD~1

Advanced

git rebase main
git cherry-pick COMMIT_ID
git reflog

Git Command Flow Diagram

Create File
    ↓
git add .
    ↓
git commit -m "message"
    ↓
git push origin main
    ↓
GitHub Repository

Conclusion

Git and GitHub are essential tools for modern software development. Git helps developers manage source code efficiently, while GitHub enables collaboration, code reviews, project management, and continuous integration.

By mastering the commands covered in this guide—from basic operations like add, commit, and push to advanced concepts such as rebase, cherry-pick, hooks, and pull requests—you can confidently manage projects of any size and collaborate effectively with development teams.

0 Comments Report