Git Commands
The daily set: setup, workflow, branching, remotes — and how to undo things.
Setup
git config --global user.name "Name"git config --global user.email "you@example.com"git config --global init.defaultBranch main
Start & clone
git initstart a repo heregit clone <url>copy a remote repo locally
Daily workflow
git statuswhat changed?git diffunstaged changes in detailgit add <file>stage a file (use.for all)git commit -m "msg"commit staged changesgit commit -am "msg"stage tracked files + commit in one step
Branching
git branchlist branches (-aincludes remotes)git switch -c featcreate branchfeatand switch to itgit switch mainswitch back to maingit merge featmergefeatinto the current branchgit branch -d featdelete merged branch (-Dforces)
Remotes
git remote -vshow remotesgit fetchdownload without merginggit pullfetch + merge (or rebase, per config)git pushupload commitsgit push -u origin featpush new branch and set upstream
Undo (in order of danger)
git restore <file>discard unstaged changes (safe)git restore --staged <file>unstage, keep editsgit commit --amendfix the last commit (message or files)git reset --soft HEAD~1undo last commit, keep stagedgit reset --hard HEAD~1undo last commit AND discard changes (dangerous)git revert <sha>safely undo a pushed commit (adds a new commit)
Log, stash, tags
git log --oneline --graph -10compact history with branchesgit stashshelve changes temporarilygit stash popbring them backgit tag v1.0tag a release;git push --tagsto publish
Frequently asked questions
I committed to the wrong branch. Now what?
Don't panic: git log to find the commit SHA, git switch to the right branch, git cherry-pick <sha>, then go back and git reset --hard HEAD~1 on the wrong branch (only if unpushed).
Merge or rebase?
Rule of thumb: rebase private branches before merging to keep history linear; never rebase commits someone else has already pulled.
How do I undo a pushed commit?
Use git revert — it adds a new commit that undoes the change, so everyone's history stays consistent. Reserve reset --hard + force-push for branches only you use.