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
Begin tracking a project in your current directory. This sets up a hidden .git folder.
π
Clone an Existing Repo
git clone <repo-url>
Download a copy of a remote repository to your local machine.
π Tracking and Saving Changes
β Stage Files
git add <file>
git add .
Mark files to be included in the next commit.
β
Commit Changes
git commit -m "your message"
Save a snapshot of staged changes with a message.
π View Status
git status
Check whatβs staged, modified, or untracked.
π View Differences
git diff # unstaged vs working directory
git diff --cached # staged vs last commit
πΏ Branching & Merging
π± Create a Branch
git branch <branch-name>
π Switch Branches
git checkout <branch-name>
git checkout -b <new-branch>
π Merge Branches
git merge <branch-name>
βοΈ Working with Remotes
π Push Changes
git push origin <branch>
π
Pull Updates
git pull origin <branch>
π View History
git log
π Advanced Commands
βͺ Revert a Commit
git revert <commit-hash>
Safely undo a commit without rewriting history.
π Reset Commits
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
Control whether to keep, unstage, or discard changes when going back.
π¦ Stash Your Work
git stash
git stash apply
Temporarily save your changes and come back to them later.
π Cherry Pick Commits
git cherry-pick <commit-hash>
Apply a specific commit from one branch onto another.
πͺ Rebase Branches
git rebase <branch>
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
π Popular Git Workflows
-
Centralized Workflow: Direct commits to
main
- Feature Branch Workflow: Isolated branches per feature
-
Gitflow: Structured with
main
,develop
,release
, andfeature
branches
Top comments (0)