Initial Setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Why: Identifies you in commits. When: First time Git setup or when changing identity.
Create Repo
git init
Why: Start version control in a folder. When: Beginning a new project.
Check Status
git status
Why: See staged/unstaged/untracked files. When: Before committing or pushing.
Stage Changes
git add <file> # stage file
git add . # stage all changes
Why: Prepare files for commit. When: After editing or creating files.
Commit
git commit -m "message"
Why: Save a snapshot of staged changes. When: After staging logically-related changes.
Remove from Stage
git reset <file>
Why: Unstage without losing edits. When: If you staged a file by mistake.
Wildcards
git add '*.txt'
Why: Stage matching files in multiple dirs. When: Adding file types project-wide.
View History
git log
git log --summary
Why: Inspect commit history. When: Reviewing changes or finding a commit.
Work with Remotes
git remote add origin <url>
git push -u origin master
git pull origin master
Why: Connect local repo to a remote. When: Pushing for backup, collaboration, or deployment.
Undo Last Commit
git reset --soft HEAD~1 # keep changes
git reset --hard HEAD^ # discard changes
Why: Fix or remove recent commits. When: If a commit is wrong or premature.
View Changes
git diff HEAD # since last commit
git diff --staged # staged changes only
Why: Review differences before committing. When: Quality-check before finalizing.
Revert File
git checkout -- <file>
Why: Restore file to last commit. When: Discard unwanted changes.
Branching
git branch <name> # create
git checkout <name> # switch
git checkout -b <name> # create + switch
Why: Isolate features/fixes. When: Working on separate tasks in parallel.
Remove Files
git rm <file>
git rm -r <dir>
Why: Remove and stage deletion. When: Deleting tracked files or folders.
Merge
git checkout master
git merge <branch>
Why: Combine branch changes. When: Feature/fix is complete and tested.
Delete Branch
git branch -d <branch> # safe delete
git branch -D <branch> # force delete
Why: Clean up branches. When: After merge or abandoning work.
Final Push
git push
Why: Send commits to remote. When: Sharing, backing up, or deploying.
Stash Changes
git stash
git stash apply
Why: Temporarily save uncommitted work. When: Switching branches without committing.