08 - Cheat Sheet and Practice Lab
#08 - Cheat Sheet and Practice Lab
##Complete Git Command Reference
Organized by what you are trying to do, not alphabetically.
###Setting Up
1
2
3
4
5
6
git config --global user.name "Your Name" #Set your name (once per machine)
git config --global user.email "[email protected]" #Set your email (once per machine)
git config --list #Verify your settings
git init #Turn current folder into a Git repository
git clone <url> #Download a remote repository to your machine
###Checking What’s Happening
1
2
3
4
5
6
7
8
9
git status #See what is staged, modified, untracked
git diff #Changes in working dir NOT yet staged
git diff --staged #Changes that ARE staged (what will be committed)
git diff <file> #Diff a specific file
git log #Full commit history
git log --oneline #Compact one-line history
git log --oneline --graph --decorate #Visual graph of history + branches
git log --oneline -10 #Last 10 commits only
git show <commit-hash> #See what a specific commit changed
###The Daily Commit Loop
1
2
3
4
5
6
git add <file> #Stage a specific file
git add . #Stage ALL changed/new files
git add -p #Stage changes interactively (chunk by chunk)
git commit -m "message" #Commit with a message
git commit #Commit — opens vi for message (no -m flag)
git commit --amend -m "new message" #Fix the last commit message (BEFORE push only)
###Fixing Mistakes
1
2
3
4
5
6
7
git restore --staged <file> #Unstage a file (keeps the edit)
git restore --staged . #Unstage everything
git restore <file> #Discard edits in working dir (CANNOT undo!)
git reset --soft HEAD~1 #Undo last commit, keep changes staged
git reset --mixed HEAD~1 #Undo last commit, unstage changes (default)
git reset --hard HEAD~1 #Undo last commit AND delete changes (destructive!)
git commit --amend --no-edit #Add a forgotten file to the last commit
###Remote Repositories
1
2
3
4
5
6
7
8
9
git remote add origin <url> #Connect local repo to a remote
git remote -v #Show all remotes
git push -u origin main #First push, sets upstream tracking
git push #Push to tracked remote branch
git fetch #Download changes (SAFE — does not merge)
git pull #Fetch + merge in one step
git diff main..origin/main #Compare local vs remote before pulling
git branch -vv #See upstream tracking for each branch
git branch -a #List all local and remote branches
###Branching
1
2
3
4
5
6
7
8
9
git branch #List local branches
git branch feature-login #Create a new branch (stay on current)
git checkout -b feature-login #Create AND switch in one step
git switch -c feature-login #Create AND switch (modern syntax)
git checkout feature-login #Switch to an existing branch
git branch -d feature-login #Delete a merged branch
git branch -D feature-login #Force-delete (even if unmerged)
git push -u origin feature-login #Push branch to remote
git push origin --delete feature-login #Delete remote branch
###Merging
1
2
git merge feature-login #Merge feature-login into current branch
git merge --abort #Abort an in-progress merge
###Advanced
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git stash #Save work in progress temporarily
git stash save "description" #Stash with a label
git stash list #See all stashes
git stash pop #Apply most recent stash + remove it
git stash apply #Apply most recent stash, keep in list
git rebase main #Replay current branch on top of main
git rebase -i HEAD~3 #Interactive rebase: clean up last 3 commits
git rebase --continue #Continue after resolving rebase conflict
git rebase --abort #Abort rebase entirely
git cherry-pick <hash> #Apply a specific commit to current branch
git reflog #Show all recent HEAD movements (recovery tool)
git tag v1.0.0 #Create a tag at current commit
git tag -a v1.0.0 -m "Release" #Annotated tag with message
git push origin --tags #Push all tags to remote
##Good Habits for Professional Development
###1. Run git status constantly
Before staging, after staging, before committing, after committing. It takes 2 seconds and prevents 90% of mistakes.
###2. Review before you commit
1
git diff --staged #Read every line of what you are about to save
Never commit blindly.
###3. Write commit messages that explain WHY, not just WHAT
1
2
3
4
5
6
7
8
9
#BAD — tells you nothing useful
git commit -m "fix"
git commit -m "changes"
git commit -m "stuff"
#GOOD — tells the story
git commit -m "Fix crash when user submits empty login form"
git commit -m "Add input validation to prevent XSS in signup"
git commit -m "Increase timeout from 5s to 30s for slow connections"
###4. Commit small, logical units of work
One commit = one complete thought (one bug fix, one feature, one refactor). Do not mix unrelated changes.
###5. Pull before you push in team projects
1
2
git pull #Bring in teammates' work first
git push #Then push your own
###6. Never commit directly to main in team projects
Create a branch, do your work, open a pull request. This enables code review and keeps main stable.
###7. Add .gitignore before your first commit
Secrets and log files committed even once are very hard to remove from Git history.
##45-Minute Practice Lab
Work through these in order. Each step builds on the last.
###Part 1: Full Local Workflow (15 min)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#Create a fresh project
mkdir git_lab && cd git_lab && git init
#Create files
touch index.html style.css README.md
#Add content
echo "<html><body><h1>Hello</h1></body></html>" > index.html
echo "body { font-family: sans-serif; }" > style.css
echo "#My Project" > README.md
#Stage and commit HTML + CSS together (they are related)
git add index.html style.css
git commit -m "Add initial HTML structure and base styles"
#Stage and commit README separately
git add README.md
git commit -m "Add project README"
#View history
git log --oneline
#Make another change
echo "<p>Welcome!</p>" >> index.html
git diff index.html #unstaged change
git add index.html
git diff --staged index.html #staged change
git commit -m "Add welcome paragraph to home page"
git log --oneline
###Part 2: Mistake Recovery (10 min)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#Simulate accidentally staging a secrets file
echo "password=super_secret" > secrets.txt
git add secrets.txt
git status #it is staged — oops
#Fix: unstage it
git restore --staged secrets.txt
git status #back to untracked
#Now simulate accidentally COMMITTING it
git add secrets.txt
git commit -m "add config"
git log --oneline #the bad commit is there
#Undo the commit with --soft (keep file available)
git reset --soft HEAD~1
git status #secrets.txt is staged again
#Unstage it
git restore --staged secrets.txt
#Prevent it permanently
echo "secrets.txt" >> .gitignore
git add .gitignore
git commit -m "Add .gitignore to exclude secrets file"
git log --oneline #history is clean
###Part 3: Remote Workflow (10 min)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#Requires a GitHub account and empty remote repository
#Connect
git remote add origin <your-remote-url>
git push -u origin main
#Go to GitHub edit README.md in the web UI commit there
#(This simulates a teammate pushing a change)
#Back in terminal: see what changed on remote
git fetch
git diff main..origin/main
#Sync
git pull
git log --oneline
###Part 4: vi Practice (5 min)
1
vi NOTES.md
Inside vi:
- Press
i - Type: “Today I learned Git: staging, committing, remote collaboration, and mistake recovery.”
- Press
ESC - Type
:wqand pressEnter
Verify:
1
2
3
cat NOTES.md
git add NOTES.md
git commit -m "Add session notes"
###Part 5: Final Check (5 min)
1
2
git status #should show: nothing to commit, working tree clean
git log --oneline #should show a clear story of what you built
##Common Beginner Mistakes
| Mistake | What to do instead |
|---|---|
git add . without reviewing first |
Run git status and git diff first |
| Vague messages like “fix” or “update” | “Fix null error in login handler when email is empty” |
Committing .env or secrets.txt |
Add to .gitignore before FIRST commit |
| Forgetting to pull before pushing | git pull before every git push on shared branches |
Working directly on main |
Always create a feature branch |
Using --hard without checking |
Use --soft unless you WANT to delete the changes |
| Panicking when vi opens | ESC then :q! — you are out immediately |
Next: 09-student-worksheet.md — Test your knowledge with the student worksheet