Git Cheat Sheet
Git is a distributed version control system that tracks changes to files, allowing multiple people to collaborate on a project safely.
Configuration
Set your identity.
git config --global user.name "John Doe"
git config --global user.email "john@example.com"View your configuration.
git config --listEdit the configuration manually.
git config --global --editCreate a Repository
Initialize a new repository.
git initClone an existing repository.
git clone <repository-url>Clone into a specific directory.
git clone <repository-url> my-projectRepository Status
See modified, staged, and untracked files.
git statusShort version.
git status -sStaging Files
Stage one file.
git add file.txtStage multiple files.
git add file1 file2Stage everything.
git add .Stage modified and deleted files.
git add -uInteractively choose changes.
git add -pCommitting
Create a commit.
git commit -m "Add login page"Commit staged changes using your editor.
git commitStage and commit tracked files.
git commit -am "Fix bug"Amend the last commit.
git commit --amendViewing History
Compact history.
git log --onelineGraph view.
git log --graph --oneline --allShow commit details.
git show <commit>See who modified each line.
git blame file.txtBranches
List branches.
git branchCreate a branch.
git branch featureSwitch branches.
git switch featureCreate and switch.
git switch -c featureOld equivalent.
git checkout featureDelete a merged branch.
git branch -d featureForce delete.
git branch -D featureRename current branch.
git branch -m new-nameMerging
Merge another branch.
git merge featureAbort a conflicted merge.
git merge --abortRebasing
Rebase onto another branch.
git rebase mainInteractive rebase.
git rebase -i HEAD~5Abort.
git rebase --abortContinue after conflicts.
git rebase --continueRemote Repositories
List remotes.
git remote -vAdd a remote.
git remote add origin <url>Change remote URL.
git remote set-url origin <url>Remove a remote.
git remote remove originFetch & Pull
Download remote changes.
git fetchDownload and merge.
git pullRebase instead of merge.
git pull --rebasePush
Push current branch.
git pushPush and set upstream.
git push -u origin mainForce push (use carefully).
git push --force-with-leaseUndo Changes
Discard unstaged changes.
git restore file.txtUnstage a file.
git restore --staged file.txtRestore both.
git restore --source=HEAD --staged --worktree file.txtReset last commit (keep files).
git reset --soft HEAD~1Reset and unstage.
git reset HEAD~1Delete last commit completely.
git reset --hard HEAD~1Stashing
Save work temporarily.
git stashList stashes.
git stash listRestore latest stash.
git stash popRestore without deleting.
git stash applyDelete stash.
git stash dropTags
Create a lightweight tag.
git tag v1.0Annotated tag.
git tag -a v1.0 -m "Version 1.0"List tags.
git tagPush tags.
git push --tagsComparing Changes
Working tree vs staged.
git diffStaged vs last commit.
git diff --cachedCompare two commits.
git diff commit1 commit2Cleaning
Remove untracked files.
git clean -fRemove directories too.
git clean -fdPreview before deleting.
git clean -nIgnore Files
Create a .gitignore.
Example:
node_modules/
dist/
.env
*.log
*.tmpUseful Inspection Commands
Current branch.
git branch --show-currentRepository root.
git rev-parse --show-toplevelTracked files.
git ls-filesRecent commits.
git reflogCommon Workflows
Create a New Feature
git switch -c feature/navbar
git add .
git commit -m "Implement navigation bar"
git push -u origin feature/navbarUpdate From Main
git switch main
git pull
git switch feature/navbar
git rebase mainResolve a Conflict
git status
# Edit conflicting files
git add .
git rebase --continueor
git merge --continueGit Areas
Working Directory
│
│ git add
▼
Staging Area (Index)
│
│ git commit
▼
Local Repository
│
│ git push
▼
Remote RepositoryMost Useful Commands
git init
git clone
git status
git add
git commit
git log
git diff
git switch
git branch
git merge
git rebase
git fetch
git pull
git push
git stash
git restore
git reset
git tag
git remote
git cleanGit Lifecycle
Create file
│
▼
Modified
│
git add
▼
Staged
│
git commit
▼
Committed
│
git push
▼
Remote RepositoryGolden Rule
Before making major changes, remember this sequence:
git status
git pull --rebase
# Work on your code
git add .
git commit -m "Describe your changes"
git pushChecking git status frequently is one of the best habits you can develop—it tells you exactly where your files are in Git’s workflow.