Git & Github Cheat Sheet by CM Mubeen
Git and Github Cover image

Git & Github Cheat Sheet by CM Mubeen

Hi folks,

Hope you're doing great! 😊 If you’ve landed on this post, chances are you’re a fellow web dev student—or just someone tired of losing code and shouting "Yeh code kahaan gaya?!" 😅

Well, good news: I’ve been diving into Git & GitHub over the past few weeks, and I’m here to break things down for you in the simplest way possible.

What is Git?

Think of Git as your code’s personal bodyguard. It’s a version control system that helps you track changes, undo mistakes, and even time travel through your code history.Imagine having Ctrl+Z on steroids—that’s Git!

What is GitHub?

GitHub is like Git’s cloud-based best friend. It’s an online platform where you can save your projects, collaborate with other developers, and even show off your code like a portfolio.

Why Should You Use Git & GitHub?

  • To save your progress: With Git, you don’t have to worry about losing your work. If something breaks, you can go back to an earlier version—just like undoing a mistake.
  • To work with others smoothly: When you’re building a project with teammates, Git helps you all work on different features without messing up each other’s code.
  • To track changes clearly: You can see what changes were made, when they were made, and by whom. It’s like having a timeline of your code’s full journey.

The following are the commands that are enough to work on projects

Local Repository Commands

git init        
Initializes a new Git repository in your current folder.
git status        
Shows the current status of files in the working directory and staging area.
ls        
Lists files and subfolders in the current directory.
ls -la        
Lists all files, including hidden ones like .git.
.git        
A hidden folder that Git uses to track all version history and settings.
git add .        
Stages all files for commit (sends to the staging area).
git commit -m "Your message"         
Records the staged changes to the repository with a message.
git log        
Shows commit history.
git log --oneline        
Shows commit history in one line.
git config --global user.name "Your Name"        
Sets the Git username globally for all projects.
git config --global user.email "your-email@example.com"        
Sets the Git email globally for all projects.
git config --global core.editor "code --wait"        
Sets VS Code as the default editor for Git.
.gitignore        
A file to tell Git what to ignore and not track.
cd foldername        
Navigates into a folder/directory.
mkdir foldername         
Creates a new directory.
touch filename         
Creates a new empty file.
git branch branchname        
Creates a new branch.
git branch        
Lists all branches in the local repo.
git switch branchname or git checkout branchname        
Switches to the specified branch.
git merge branchname        
Merges branchname branch into your current one.
git checkout --orphan new-branch-name        
Creates a new branch without history (useful for GitHub Pages).
git diff --staged         
Shows differences between staged changes and last commit.
git stash        
Temporarily saves your uncommitted changes.
git stash pop         
Applies and removes the last stashed changes.
git stash list         
Lists all saved stashes.
git stash apply         
Applies stashed changes but keeps them in the stash.
git restore filename         
Restores the file to its last committed state.
git checkout <commit-hash>         
Goes to a specific commit (detached HEAD).
git switch main        
Returns to main branch (reattaches HEAD).
git checkout HEAD^2        
Checks the second-to-last commit.
git rebase master        
Applies commits from one branch on top of another, rewriting commit history.

SSH Key Setup and GitHub Integration

ssh-keygen -t ed25519 -C "your-email@example.com"         
Generates a new SSH key with a label.
Press Enter 3 times        
Accept defaults and skip passphrase (optional).
eval "$(ssh-agent -s)"        
Starts the SSH agent in the background.
ssh-add ~/.ssh/id_ed25519        
Adds your SSH key to the SSH agent.
cat ~/.ssh/id_ed25519.pub        
Shows your public key (copy to GitHub).
GitHub > Settings > SSH & GPG Keys > New SSH Key        
Paste your public key here.
ssh -T git@github.com        
Tests your SSH connection with GitHub.
git remote -v        
Shows the URL of your connected remote GitHub repo.
git remote add origin git@github.com:username/repo.git        
Links your local project to the GitHub repository using SSH.
git branch -M main        
Renames current branch to main (recommended default).
git push -u origin main        
Pushes your code to GitHub and tracks the remote branch.
git remote remove origin        
Removes the connection to the remote GitHub repository.

Clone Repository and Open Source Contribution

  • Go to the repo on GitHub.
  • Click “Fork” (top-right corner).
  • This creates your own copy of the repo under your account.

git clone https://github.com/user/repo.git        
Clones a GitHub repo to your local machine.
git checkout -b new-branch         
Creates and switches to a new branch for changes.
git push -u origin new-branch        
Pushes the branch to GitHub and links with origin.
open your fork > Click "Pull Request" > Target original repo        
Create Pull Request On GitHub

Why Did I Write This?

I wrote this for all the amazing folks out there who already know what Git is, but every time they start a project, suddenly forget the commands like:

"Wait... was it git add . or git . add?"         

So, instead of Googling the same thing 100 times a week (we've all been there 🤐), just save this article and treat it like your personal Git GPS—guiding you through version control traffic!P.S. I’ll keep updating this article from time to time as I learn new things—so keep it bookmarked like your favorite meme folder! 😄

If it helped you even a little, drop a like, leave a kind word, or tag a coding buddy who forgets commands more than their own passwords. And yes, don’t forget to keep me in your precious duas (prayers) — they mean the world! 🌍

To view or add a comment, sign in

More articles by CM Mubeen

  • What is an HTML Attribute?

    Hey everyone! Let's dive into the world of HTML attributes – a key aspect of web development every developer should…

  • What is the purpose of the HTML doctype declaration?

    Hey LinkedIn fam! Ever stumbled upon the DOCTYPE declaration at the start of an HTML document? Let's decode it! 🎯…

Others also viewed

Explore content categories