DEV Community

Cover image for Mastering Git and GitHub: A Comprehensive Guide - Cheatsheet
Yogesh Chavan
Yogesh Chavan

Posted on Edited on Originally published at Medium

Mastering Git and GitHub: A Comprehensive Guide - Cheatsheet

Version control is a crucial part of modern software development, and Git is the most widely used version control system. GitHub, on the other hand, provides a cloud-based platform for hosting Git repositories.

This article covers essential Git and GitHub commands to help you manage your projects efficiently.

Want to learn Git + GitHub from Scratch? Check out my 2+ hours crash course.


1. Getting Started with Git

Before using Git, you need to install and configure it on your machine.

Commands:

  • Install Git: Download and install Git from git-scm.com.

  • Check Git version:

git --version
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Set up username and email (required for commits):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • View configuration settings:
git config --list
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

2. Git Basics: Local Repository Operations

These commands allow you to initialize a repository, add files, commit changes, and check history.

Commands:

  • Initialize a new repository:
git init
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Check repository status:
git status
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Add files to the staging area:
git add <filename> 
git add .  # Add all files
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Commit staged files with a message:
git commit -m "Initial commit"
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • View commit history:
git log
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Show details of a specific commit:
git show <commit-hash>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

3. Working with Git Branches

Branches allow you to work on new features without affecting the main codebase.

Commands:

  • Create a new branch:
git branch <branch-name>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Switch to a branch:
git checkout <branch-name>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Create and switch to a new branch:
git checkout -b <branch-name>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • List all branches:
git branch
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Merge another branch into the current branch:
git merge <branch-name>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Delete a branch:
git branch -D <branch-name>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

4. Introduction to GitHub

GitHub hosts Git repositories online, making collaboration easier.

Commands:

  • Generate an SSH key for authentication:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • View and copy your SSH public key:
cat ~/.ssh/id_rsa.pub
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Connect Git to GitHub via HTTPS:
git remote add origin https://github.com/<your-username>/<repo-name>.git
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Connect Git to GitHub via SSH:
git remote add origin git@github.com:<your-username>/<repo-name>.git
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Check remote repository connections:
git remote -v
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

5. Interacting with GitHub

Collaboration involves pushing, pulling, and cloning repositories.

Commands:

  • Pull the latest changes before pushing:
git pull origin <branch-name>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Push local commits to GitHub:
git push origin <branch-name>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Clone a remote repository:
git clone https://github.com/<user>/<repo>.git
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Clone a remote repository with a new name:
git clone https://github.com/<user>/<repo>.git newname
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

6. Undoing Changes and Managing History

These commands help revert changes and modify commit history.

Commands:

  • Unstage a file:
git reset HEAD <file>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Discard changes in a file:
git checkout -- <file>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Reset to a previous commit:
git reset --hard <commit-hash>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Revert a commit (without modifying history):
git revert <commit-hash>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Modify the most recent commit message:
git commit --amend -m "Updated commit message"
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Stash uncommitted changes:
git stash
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Apply latest stashed changes:
git stash apply
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Apply specific stashed changes:
git stash list # See all stashed list

git stash apply stash@{1} # Apply a specific stash
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

7. Managing Contributions: Forks, Pull Requests, and Code Review

Commands:

  • Clone a forked repository:
git clone https://github.com/<your-username>/<forked-repo>.git
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Push changes to your forked repository:
git push origin <branch-name>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

8. Viewing File Changes

Commands:

  • View unstaged changes:
git diff
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • View staged changes:
git diff --staged
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

9. Ignoring Files using .gitignore

Commands:

  • Create a .gitignore file:
touch .gitignore
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

10. Inspecting the Log of Commits

Commands:

  • View commit history:
git log
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Compact commit history:
git log --oneline
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

11. Renaming Files in Git

Commands:

  • Rename a file and track changes:
git mv old-file.txt new-file.txt
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

12. Advanced Git Features

Commands:

  • Cherry-pick a specific commit:
git cherry-pick <commit-hash>
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Rebase a branch:
git rebase master
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

To understand the difference between merge and rebase with live demo check out the course.

  • Create a new tag:
git tag v1.0
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025
  • Push tags to GitHub:
git push origin v1.0
✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025 ✨ อัปเดตล่าสุด: myogeshchavan/mastering git and github a comprehensive guide cheatsheet gm - Kapook Update 2025

With these Git and GitHub commands, you can manage version control efficiently and collaborate seamlessly with others. Mastering these tools will enhance your productivity as a developer.

Connect With Me

Top comments (0)