PUBLIC REFERENCE // GIT
Git Cheatsheet
Git tracks your project as a series of snapshots rather than file-by-file diffs, which is why operations like branching and merging are so fast — a branch is just a movable pointer to a commit, not a copy of the codebase.
- Commands
- 83
- Sections
- 10
- Source policy
- Official links
FIELD NOTES
Decisions worth remembering
- git merge keeps the full history of both branches via a merge commit; git rebase rewrites commits onto a new base — never rebase commits you've already pushed and others may have pulled.
- git push --force-with-lease is safer than git push --force: it aborts if the remote branch has commits you have not seen yet, instead of silently discarding them.
- Checking out a specific commit hash (not a branch) puts you in a detached HEAD state — any commits made there become unreachable once you switch branches, unless you create a new branch first.
- Adding a path to .gitignore only stops new files from being tracked — for files already committed, you also need git rm --cached <file> to actually stop tracking them.
GIT // REFERENCE
Setup & Config
git config --global user.name 'Your Name'Set your global username
git config --global user.email 'you@example.com'Set your global email
git config --global core.editor vimSet default editor
git config --listShow all config values
git config --global alias.st statusCreate a command alias (st → status)
git config --global color.ui autoEnable colored output
GIT // REFERENCE
Creating & Cloning
git initInitialize a new local repository
git init my-projectCreate a new repo in a named directory
git clone https://github.com/user/repo.gitClone a remote repository
git clone https://github.com/user/repo.git my-dirClone into a specific directory
git clone --depth 1 https://github.com/user/repo.gitShallow clone (last commit only)
GIT // REFERENCE
Staging & Committing
git statusShow working tree status
git add file.txtStage a specific file
git add .Stage all changes in current directory
git add -pInteractively stage hunks
git commit -m 'Your message'Commit staged changes with a message
git commit -am "message"Stage tracked files and commit in one step
git commit --amendModify the last commit (message or files)
git diffShow unstaged changes
git diff --stagedShow staged changes (ready to commit)
GIT // REFERENCE
Branching
git branchList all local branches
git branch -aList all branches (local + remote)
git branch feature-nameCreate a new branch
git checkout feature-nameSwitch to a branch
git checkout -b feature-nameCreate and switch to new branch
git switch feature-nameSwitch branches (modern syntax)
git switch -c feature-nameCreate and switch (modern syntax)
git branch -d feature-nameDelete a merged branch
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
git branch -D feature-nameForce delete a branch (unmerged)
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
git branch -m old-name new-nameRename a branch
GIT // REFERENCE
Merging & Rebasing
git merge feature-nameMerge a branch into the current branch
git merge --no-ff feature-nameMerge with a merge commit (no fast-forward)
git merge --squash feature-nameSquash all commits into one before merging
git merge --abortAbort an in-progress merge
git rebase mainRebase current branch onto main
git rebase -i HEAD~3Interactive rebase: edit last 3 commits
git rebase --abortAbort an in-progress rebase
git rebase --continueContinue rebase after resolving conflicts
git cherry-pick abc1234Apply a specific commit to current branch
GIT // REFERENCE
Stashing
git stashStash current uncommitted changes
git stash push -m "work in progress"Stash with a descriptive message
git stash listList all stash entries
git stash popApply the latest stash and remove it
git stash apply stash@{2}Apply a specific stash (keep in list)
git stash drop stash@{0}Delete a specific stash entry
git stash clearDelete all stash entries
git stash branch new-branchCreate a branch from a stash
GIT // REFERENCE
Remote Repositories
git remote -vShow all remote connections
git remote add origin https://github.com/user/repo.gitAdd a remote connection
git remote remove originRemove a remote
git remote rename origin upstreamRename a remote
git fetch originDownload changes without merging
git pull origin mainFetch and merge from remote
git pull --rebase origin mainFetch and rebase (cleaner history)
git push origin mainPush commits to remote
git push -u origin feature-namePush and set upstream tracking
git push --force-with-leaseSafe force push (checks remote state)
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
git push origin --delete feature-nameDelete a remote branch
GIT // REFERENCE
Undoing Changes
git restore file.txtDiscard changes in a working file
git restore --staged file.txtUnstage a file (keep changes)
git revert abc1234Create a new commit that undoes a commit
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --mixed HEAD~1Undo last commit, keep changes unstaged
git reset --hard HEAD~1Undo last commit and discard changes
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
git clean -fdRemove untracked files and directories
Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.
GIT // REFERENCE
Log & Diff
git logShow commit history
git log --onelineCompact one-line log
git log --oneline --graph --allVisual branch graph
git log -pShow patch (diff) for each commit
git log --author="Name"Filter commits by author
git log --since="2 weeks ago"Filter by time range
git log -- file.txtShow history for a specific file
git show abc1234Show details of a specific commit
git blame file.txtShow who last modified each line
git diff main..featureShow diff between two branches
COMMON QUESTIONS
Git command FAQ
How do I undo the last commit without losing my changes?
Run git reset --soft HEAD~1 — it removes the commit but keeps your changes staged, ready to re-commit.
What is the difference between git fetch and git pull?
git fetch downloads remote commits without touching your working branch; git pull runs fetch and then immediately merges (or rebases) into your current branch.
How do I recover a deleted branch?
If you know the last commit hash (check git reflog), run git checkout -b branch-name <hash> to recreate it — Git doesn't actually delete commit objects immediately.
Why does git status show a file as modified right after I clone or pull?
Usually a line-ending mismatch (CRLF vs LF) — configure core.autocrlf consistently across your team, or add a .gitattributes file.