DEV Community

Cover image for 🧠 Master Git: A Practical Cheatsheet for Developers
Jitendra Rawat
Jitendra Rawat

Posted on • Edited on

🧠 Master Git: A Practical Cheatsheet for Developers

Introduction: Whether you're a solo developer or collaborating with a team, version control is non-negotiableβ€”and Git is the standard. It helps manage code changes, track history, experiment safely, and work across branches. But let’s be honest: Git's power can be overwhelming at first. That’s why I’ve compiled this concise, no-fluff Git cheatsheet to help you get up to speed quickly and stay productive.

πŸš€ Getting Started

πŸ”§ Initialize a Repository

git init
Enter fullscreen mode Exit fullscreen mode

Begin tracking a project in your current directory. This sets up a hidden .git folder.

πŸ“… Clone an Existing Repo

git clone <repo-url>
Enter fullscreen mode Exit fullscreen mode

Download a copy of a remote repository to your local machine.

πŸ“ Tracking and Saving Changes

βž• Stage Files

git add <file>
git add .
Enter fullscreen mode Exit fullscreen mode

Mark files to be included in the next commit.

βœ… Commit Changes

git commit -m "your message"
Enter fullscreen mode Exit fullscreen mode

Save a snapshot of staged changes with a message.

πŸ” View Status

git status
Enter fullscreen mode Exit fullscreen mode

Check what’s staged, modified, or untracked.

πŸ“Š View Differences

git diff        # unstaged vs working directory  
git diff --cached  # staged vs last commit
Enter fullscreen mode Exit fullscreen mode

🌿 Branching & Merging

🌱 Create a Branch

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

πŸ“ Switch Branches

git checkout <branch-name>
git checkout -b <new-branch>
Enter fullscreen mode Exit fullscreen mode

πŸ”€ Merge Branches

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

☁️ Working with Remotes

πŸ“„ Push Changes

git push origin <branch>
Enter fullscreen mode Exit fullscreen mode

πŸ“… Pull Updates

git pull origin <branch>
Enter fullscreen mode Exit fullscreen mode

πŸ“œ View History

git log
Enter fullscreen mode Exit fullscreen mode

πŸ’  Advanced Commands

βͺ Revert a Commit

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Safely undo a commit without rewriting history.

πŸ”€ Reset Commits

git reset --soft HEAD~1  
git reset --mixed HEAD~1  
git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

Control whether to keep, unstage, or discard changes when going back.

πŸ“¦ Stash Your Work

git stash
git stash apply
Enter fullscreen mode Exit fullscreen mode

Temporarily save your changes and come back to them later.

πŸ’ Cherry Pick Commits

git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Apply a specific commit from one branch onto another.

πŸ’ͺ Rebase Branches

git rebase <branch>
Enter fullscreen mode Exit fullscreen mode

Reapply commits from one branch on top of another for a cleaner history.

βš™οΈ Productivity Boosters

βš“ Git Hooks

Automate tasks before/after Git events by placing scripts in .git/hooks.

⚑ Create Aliases

git config --global alias.co checkout
git config --global alias.br branch
Enter fullscreen mode Exit fullscreen mode

πŸ“š Popular Git Workflows

  • Centralized Workflow: Direct commits to main
  • Feature Branch Workflow: Isolated branches per feature
  • Gitflow: Structured with main, develop, release, and feature branches

πŸ”— Connect with me:

πŸ’Ό LinkedIn

πŸ’» GitHub

Top comments (0)