Skip to content
SDETRole
SDETRole

Learn / Cheat sheet

Git Cheat Sheet for Testers

The Git commands a tester needs to work on a test framework alongside developers: branch, commit, keep up with main, undo mistakes without losing work, and find the commit that introduced a bug. Commands that discard work are marked.

Checked against Git 2.40+

Set up

TaskCode
Your identity
git config --global user.name "Mei Lin"
git config --global user.email "mei@example.com"
Default branch name git config --global init.defaultBranch main
Clone a repo git clone https://github.com/org/tests.git
Pull rebases by default git config --global pull.rebase true

Daily work

TaskCode
What changed? git status
New branch from where you are git switch -c fix/login-flaky-wait
Stage files git add tests/login.spec.ts
Stage parts of a file git add -p
Commit git commit -m "Wait for login response instead of sleep"
Push a new branch git push -u origin fix/login-flaky-wait
Get the latest changes git pull
Switch branch git switch main

Look around

TaskCode
Compact history git log --oneline --graph --decorate -20
History of one file git log --follow -p -- tests/login.spec.ts
Unstaged changes git diff
Staged changes git diff --staged
Your branch vs main git diff main...HEAD
One commit git show 3f2a9c1
Who changed these lines git blame -L 20,40 tests/login.spec.ts
All branches git branch -a

Keep up with main

TaskCode
Download without changing files git fetch origin
Replay your commits on top of main git rebase origin/main
Or merge main into your branch git merge origin/main
After fixing a conflict git add <file> then git rebase --continue
Give up on the rebase git rebase --abort
Push a rebased branch safely git push --force-with-lease
Copy one commit to this branch git cherry-pick 3f2a9c1

Only rebase branches that are yours. --force-with-lease refuses to overwrite commits someone else pushed.

Undo

SituationCommand
Discard edits to a file (loses them) git restore tests/login.spec.ts
Unstage a file, keep the edits git restore --staged tests/login.spec.ts
Fix the last commit message or add a file git commit --amend
Undo the last commit, keep changes staged git reset --soft HEAD~1
Undo a pushed commit (adds a new commit) git revert 3f2a9c1
Throw away all local changes (loses them) git reset --hard HEAD
Find a commit you "lost" git reflog

On shared branches, use revert. reset and amend rewrite history and force everyone else to fix their copy.

Park work

TaskCode
Stash changes git stash push -m "half-done locator refactor"
Include untracked files git stash push -u
List stashes git stash list
Re-apply and drop the latest git stash pop

Find the commit that broke it

TaskCode
Start git bisect start
Mark the current commit bad git bisect bad
Mark a known good commit git bisect good v2.3.0
Let a test decide each step git bisect run npx playwright test tests/checkout.spec.ts
Finish and return git bisect reset

bisect run treats exit code 0 as good and 1–127 as bad (125 means skip this commit).

Practise and go deeper

More cheat sheets

Type to search across SDETRole.

Full-text search by Pagefind · runs in your browser Open search page →