OPERATOR REFERENCE · GIT

Git Field Reference
Local, Branching & Remote

setup → snapshot → branch → sync → recover — one page, every day
WORKING DIR · untracked / modified
add → commit
LOCAL REPO · origin/main
push / pull
REMOTE · origin
01 Setup & Config

Identity

git config --global user.name "Your Name" git config --global user.email "you@example.com"
+

Start a Repo

git init git clone git@host:org/repo.git

Useful Defaults

git config --global init.defaultBranch main git config --global core.editor "code --wait"
02 Snapshotting

Check State

git status git status -s
Short form: M modified, A staged-add, ?? untracked.

Stage Changes

git add file.txt git add . git add -p
-p lets you stage hunks interactively instead of whole files.

Commit

git commit -m "message" git commit -am "message"
-a auto-stages tracked, modified files — skips new/untracked ones.
Δ

Inspect Changes

git diff git diff --staged git show HEAD
03 Branching & Merging

Create / Switch

git branch feature-x git switch feature-x git switch -c feature-x
-c creates and switches in one step.

List / Delete

git branch -a git branch -d feature-x git branch -D feature-x
⚠ -D force-deletes even with unmerged commits

Merge

git switch main git merge feature-x git merge --no-ff feature-x
--no-ff keeps a merge commit even when fast-forward is possible.

Rebase

git switch feature-x git rebase main git rebase --continue
⚠ never rebase commits already pushed & shared

Cherry-Pick

git cherry-pick a1b2c3d
Applies one specific commit onto the current branch.

Resolve Conflicts

git status # edit conflicted files git add resolved-file.txt git commit
04 Working with Remotes
🔗

Manage Remotes

git remote -v git remote add origin git@host:org/repo.git

Fetch & Pull

git fetch origin git pull origin main git pull --rebase origin main

Push

git push origin feature-x git push -u origin feature-x git push --force-with-lease
⚠ prefer --force-with-lease over --force on shared branches
🏷

Tags

git tag v1.0.0 git tag -a v1.0.0 -m "release" git push origin v1.0.0

Track Upstream

git branch --set-upstream-to=origin/main git branch -vv

Prune Stale Branches

git fetch --prune git remote prune origin
05 Undo & Recover

Unstage / Discard

git restore --staged file.txt git restore file.txt
⚠ discards uncommitted local edits permanently

Amend Last Commit

git commit --amend -m "new message"
Rewrites history — avoid on already-pushed shared commits.

Reset

git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1
⚠ --hard discards working-directory changes too

Revert (safe undo)

git revert a1b2c3d
✓ creates a new commit — safe on shared/pushed history
📦

Stash

git stash git stash pop git stash list
🕘

Reflog Recovery

git reflog git checkout a1b2c3d
Reflog tracks HEAD moves — the safety net for "lost" commits.

Log & History

git log --oneline --graph --all git log -p file.txt git blame file.txt

Clean Untracked

git clean -n git clean -fd
⚠ always run -n (dry run) first
06 Feature-Branch Workflow
init branch -c feature-x commit commit push -u merge push main feature-x
main branch commit
feature branch commit
merge commit
07 Common Errors & Fixes
SymptomFix
Detached HEADgit switch -c new-branch
Wrong branch commitgit cherry-pick, then git reset --hard on wrong branch
Committed to main by mistakegit branch feature-x; git reset --hard origin/main
Merge conflict markers left in fileresolve manually, git add, git commit
Push rejected (non-fast-forward)git pull --rebase, then push
Accidentally deleted branchgit reflog → git checkout <sha> → git switch -c
CommandRisk
git reset --harddestructive loses uncommitted work
git push --forcedestructive can overwrite others' commits
git push --force-with-leasecaution safer, checks remote hasn't moved
git clean -fddestructive deletes untracked files
git rebase (shared branch)destructive rewrites history others rely on
git revertsafe adds a new commit, no history rewrite
08 .gitignore & Quick Reference
🚫

.gitignore Basics

node_modules/ *.log .env
Already-tracked files need git rm --cached to stop tracking.
?

Get Help

git help commit git commit -h

Aliases (optional)

git config --global alias.st status git config --global alias.co checkout