DEV Community

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

Posted 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
Enter fullscreen mode Exit fullscreen mode
  • Set up username and email (required for commits):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Enter fullscreen mode Exit fullscreen mode
  • View configuration settings:
git config --list
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Check repository status:
git status
Enter fullscreen mode Exit fullscreen mode
  • Add files to the staging area:
git add <filename> 
git add .  # Add all files
Enter fullscreen mode Exit fullscreen mode
  • Commit staged files with a message:
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode
  • View commit history:
git log
Enter fullscreen mode Exit fullscreen mode
  • Show details of a specific commit:
git show <commit-hash>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode
  • Switch to a branch:
git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Create and switch to a new branch:
git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • List all branches:
git branch
Enter fullscreen mode Exit fullscreen mode
  • Merge another branch into the current branch:
git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Delete a branch:
git branch -D <branch-name>
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode
  • View and copy your SSH public key:
cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode
  • Connect Git to GitHub via HTTPS:
git remote add origin https://github.com/<your-username>/<repo-name>.git
Enter fullscreen mode Exit fullscreen mode
  • Connect Git to GitHub via SSH:
git remote add origin git@github.com:<your-username>/<repo-name>.git
Enter fullscreen mode Exit fullscreen mode
  • Check remote repository connections:
git remote -v
Enter fullscreen mode Exit fullscreen mode

5. Interacting with GitHub

Collaboration involves pushing, pulling, and cloning repositories.

Commands:

  • Pull the latest changes before pushing:
git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Push local commits to GitHub:
git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode
  • Clone a remote repository:
git clone https://github.com/<user>/<repo>.git
Enter fullscreen mode Exit fullscreen mode
  • Clone a remote repository with a new name:
git clone https://github.com/<user>/<repo>.git newname
Enter fullscreen mode Exit fullscreen mode

6. Undoing Changes and Managing History

These commands help revert changes and modify commit history.

Commands:

  • Unstage a file:
git reset HEAD <file>
Enter fullscreen mode Exit fullscreen mode
  • Discard changes in a file:
git checkout -- <file>
Enter fullscreen mode Exit fullscreen mode
  • Reset to a previous commit:
git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode
  • Revert a commit (without modifying history):
git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode
  • Modify the most recent commit message:
git commit --amend -m "Updated commit message"
Enter fullscreen mode Exit fullscreen mode
  • Stash uncommitted changes:
git stash
Enter fullscreen mode Exit fullscreen mode
  • Apply latest stashed changes:
git stash apply
Enter fullscreen mode Exit fullscreen mode
  • Apply specific stashed changes:
git stash list # See all stashed list

git stash apply stash@{1} # Apply a specific stash
Enter fullscreen mode Exit fullscreen mode

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

Commands:

  • Clone a forked repository:
git clone https://github.com/<your-username>/<forked-repo>.git
Enter fullscreen mode Exit fullscreen mode
  • Push changes to your forked repository:
git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

8. Viewing File Changes

Commands:

  • View unstaged changes:
git diff
Enter fullscreen mode Exit fullscreen mode
  • View staged changes:
git diff --staged
Enter fullscreen mode Exit fullscreen mode

9. Ignoring Files using .gitignore

Commands:

  • Create a .gitignore file:
touch .gitignore
Enter fullscreen mode Exit fullscreen mode

10. Inspecting the Log of Commits

Commands:

  • View commit history:
git log
Enter fullscreen mode Exit fullscreen mode
  • Compact commit history:
git log --oneline
Enter fullscreen mode Exit fullscreen mode

11. Renaming Files in Git

Commands:

  • Rename a file and track changes:
git mv old-file.txt new-file.txt
Enter fullscreen mode Exit fullscreen mode

12. Advanced Git Features

Commands:

  • Cherry-pick a specific commit:
git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode
  • Rebase a branch:
git rebase master
Enter fullscreen mode Exit fullscreen mode

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

  • Create a new tag:
git tag v1.0
Enter fullscreen mode Exit fullscreen mode
  • Push tags to GitHub:
git push origin v1.0
Enter fullscreen mode Exit fullscreen mode

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)