Skip to content

Git commands every QA automation engineer should know

41 commands, grouped by when you need them: testing a PR, finding the commit that broke a test, undoing a bad reset. Each section shows real output.

  • Updated
  • Checked on Git 2.53
Where your change lives
Working treefiles on disk
Staging areathe next commit
Local repoyour machine
RemoteGitHub / GitLab, CI
do
undo

login.spec.ts has an unsaved edit. Click a lit command to move it to the next box.

Pick your situation

Start with 1 and 2 if Git is new to you. The rest you’ll need the day something breaks.

  1. 1Get the code, stay in sync7 commands
  2. 2Save and share your tests6 commands
  3. 3Test someone else’s branch5 commands
  4. 4Find who changed what, and when5 commands
  5. 5Find the commit that broke the test4 commands
  6. 6Undo almost anything6 commands
  7. 7Switch tasks without losing work4 commands
  8. 8Get a clean environment4 commands

no tag: nothing is lostrewrites history: fine before you pushcan’t be undone: read twice

I need to…

Type what you’re trying to do. Each answer links to its section.

22 matches

Get the code, stay in sync

WhenFirst day on the project, or every morning before you run the suite.

  • git clone <url>

    Download the repo with its full history.

  • git status -sb

    Which branch you are on and what changed. Run it before every other command.

  • git switch <branch>

    Move to an existing branch.

  • git switch -c test/login-flaky

    Create a branch and move to it.

  • git switch -

    Jump back to the branch you were on before.

  • git fetch --prune

    Download new commits and drop branches deleted on the server. Touches none of your files.

  • git pull --rebase

    Fetch, then put your local commits on top. No noisy merge commits in your test branch.

What you’ll see

terminalbash
$ git status -sb## test/login-flaky M notes.txt          # changed, not stagedM  src.js             # staged, goes into the next commit?? debug.log          # new file Git doesn't track yet

TipStill typing git checkout for everything? It works, but one command does two jobs. git switch changes branches, git restore changes files, and neither can surprise you with the other's job. Both are stable since Git 2.51.

Save and share your tests

WhenYou fixed a locator, added a spec, or updated a fixture.

  • git add -p

    Pick changes hunk by hunk. Leaves your console.log and test.only behind.

  • git add tests/

    Stage everything under a folder.

  • git commit -m "test: cover expired token"

    Save the staged changes as one commit.

  • git commit --amend --no-edit

    Add forgotten changes to the last commit. Only before you push. rewrites history

  • git push -u origin HEAD

    Publish the current branch under the same name and remember it.

  • git push --force-with-lease --force-if-includes

    Overwrite your branch after an amend or rebase, but refuse if someone else pushed to it. rewrites history

What you’ll see

terminalbash
# a teammate pushed to your branch while you amended$ git push --force-with-lease --force-if-includes ! [rejected]  test/login-flaky -> test/login-flaky (remote ref updated since checkout)error: failed to push some refshint: Updates were rejected because the tip of the remote-tracking branch hashint: been updated since the last checkout. # plain --force would have deleted their commit$ git pull --rebase && git push --force-with-lease --force-if-includes

TipUse --force-if-includes together with --force-with-lease. On its own, the lease is defeated by a background fetch, for example the one your IDE runs every few minutes.

Test someone else’s branch

WhenA developer asks you to check their PR before it merges.

  • git fetch origin pull/123/head:pr-123

    Download GitHub PR #123 as a local branch pr-123 (gh pr checkout 123 does the same).

  • git diff --stat main...HEAD

    Which files the branch changed since it left main, and how much.

  • git diff --name-only main...HEAD -- '*.spec.ts'

    Only the changed test files. Feed the list to your runner.

  • git worktree add ../pr-123 pr-123

    Check the PR out in a second folder. Your own branch stays untouched.

  • git worktree remove ../pr-123

    Delete that folder when you are done.

What you’ll see

terminalbash
$ git diff --stat main...test/login-flaky tests/check.sh      | 1 + tests/login.spec.ts | 1 + 2 files changed, 2 insertions(+) # run main and the PR side by side, in two terminals$ git worktree add ../pr-123 pr-123$ cd ../pr-123 && npm ci && npx playwright test

TipThree dots (main...HEAD) compare against the point where the branch split off, so you see only the PR's changes. Two dots also show everything that landed on main since then. Playwright can do the selection itself: npx playwright test --only-changed=origin/main.

Find who changed what, and when

WhenA locator stopped matching, or a test suddenly has a new timeout.

  • git log --oneline --graph -20

    The last 20 commits as a compact tree.

  • git log -p --follow -- tests/login.spec.ts

    Every change to one file, with diffs, even across renames.

  • git log -S"getByTestId('login')" --oneline

    Commits that added or removed this exact text. The fastest way to find when a selector appeared or vanished.

  • git blame -w -C -L 10,30 tests/login.spec.ts

    Who last touched lines 10–30, ignoring whitespace and code moved from other files.

  • git show <sha> --stat

    What one commit changed.

What you’ll see

terminalbash
$ git log -S"getByTestId('login')" --oneline -- tests/04b923d test: role locator      # removed itdfcae02 test: login             # added it

Tip-S finds commits where the number of matches changed. Need every commit that touched lines matching a pattern? Use -G<regex> instead.

Find the commit that broke the test

WhenThe test passed last week. 200 commits later it fails, and nobody knows why.

  • git bisect start <bad> <good>

    Start a binary search between a failing commit and a passing one.

  • git bisect run npx playwright test checkout.spec.ts

    Let Git run the test on each step. Exit code 0 = good, 1–127 = bad, 125 = skip this commit.

  • git bisect good / git bisect bad

    Mark a step by hand when the check can't be scripted.

  • git bisect reset

    Stop and go back to where you started.

16 commits. The test passed on #1 and fails on #16.
  1. 1good
  2. 2not tested
  3. 3not tested
  4. 4not tested
  5. 5not tested
  6. 6not tested
  7. 7not tested
  8. 8not tested
  9. 9not tested
  10. 10not tested
  11. 11not tested
  12. 12not tested
  13. 13not tested
  14. 14not tested
  15. 15not tested
  16. 16bad

$ git bisect start c16 c1

4 test runs instead of 15. Each run halves what’s left: 1,000 commits take about 10.

What you’ll see

terminalbash
# a 16-commit demo repo; in yours, the command is your test:# git bisect run npx playwright test checkout.spec.ts$ git bisect start HEAD HEAD~15$ git bisect run sh -c 'grep -q 42 src.js'running 'sh' '-c' 'grep -q 42 src.js'...41879dbeda4d2b065ce5ffc34b18add79409be27 is the first bad commit    feat: change 11 src.js    | 2 +-bisect found first bad commit$ git bisect reset

TipIf the test file is newer than the good commit, old checkouts won't have it. Copy it outside the repo and copy it back in a small script. In that script, make setup failures exit 125 (npm ci || exit 125), or a broken install counts as a bad commit. Git 2.56 added --reset-when-found to return you to the start automatically.

Undo almost anything

WhenWrong file staged, bad commit, or a reset you regret.

  • git restore --staged <file>

    Unstage a file. The edits stay.

  • git restore <file>

    Throw away uncommitted edits in a file. They are gone for good. can’t be undone

  • git reset --soft HEAD~1

    Undo the last commit, keep its changes staged. Only if not pushed. rewrites history

  • git revert <sha>

    Add a new commit that undoes an old one. The safe undo on a shared branch.

  • git reflog

    Every place HEAD has been. Your safety net after a reset or a lost branch.

  • git reset --hard HEAD@{1}

    Jump back to the state before your last move. Uncommitted edits are lost. can’t be undone

What happened?
Runsafe
git restore --staged <file>

The file leaves the next commit. Your edits stay.

What you’ll see

terminalbash
$ git reset --hard HEAD~1        # oops$ git reflog -3dfcae02 HEAD@{0}: reset: moving to HEAD~104b923d HEAD@{1}: commit: test: role locatordfcae02 HEAD@{2}: commit: test: login$ git reset --hard HEAD@{1}      # the commit is back

TipAnything you committed stays findable in the reflog for at least 30 days (90 for commits still on a branch). Edits you never committed or stashed can't. When in doubt, commit or stash first.

Switch tasks without losing work

WhenHalfway through a test, an urgent bug needs a check on another branch.

  • git stash push -u -m "wip: login debug"

    Put away all changes, new files included, with a name you'll recognise.

  • git stash list

    See what you've put away.

  • git stash pop

    Bring the latest stash back and remove it from the list.

  • git cherry-pick -x <sha>

    Copy one commit, say a test fix, onto the release branch. -x notes where it came from.

What you’ll see

terminalbash
$ git stash push -u -m "wip: login debug"Saved working directory and index state On test/login-flaky: wip: login debug$ git switch release/2.4$ git cherry-pick -x 04b923d$ git switch - && git stash pop

TipWithout -u, new files such as a fresh spec stay behind and follow you to the next branch.

Get a clean environment

WhenThe suite fails locally, passes in CI, and you suspect old reports or stale files.

  • git clean -n -d

    Dry run: list untracked files and folders that would be deleted.

  • git clean -fd

    Delete untracked files and folders. Ignored files stay. can’t be undone

  • git clean -fdx

    Also delete ignored files: node_modules, .env, .auth, reports. Closest to a fresh clone. can’t be undone

  • git check-ignore -v <path>

    Which .gitignore line ignores this file, and where it is.

What you’ll see

terminalbash
$ git clean -n -dWould remove debug.logWould remove playwright-report/Would remove test-results/

TipAlways run -n first. -x also deletes .env and saved logins, which nothing can restore.

Old habits worth dropping

Most tutorials still teach the left column. It works, and it’s how people lose work.

  • git checkout <branch>git switch <branch>

    One command, one job. checkout also overwrites files when you mistype a name.

  • git checkout -- <file>git restore <file>

    Same result, and the name says it out loud: these edits are about to go.

  • git push --forcegit push --force-with-lease --force-if-includes

    Refuses if someone pushed while you weren't looking.

  • git reset --hard as a reflexgit stash, then reset

    A stash can be popped back. Uncommitted work wiped by reset can't.

  • Assuming the branch is mastermain

    GitHub, GitLab and Bitbucket default to main, and Git 3.0 will too.

Set up once

A .gitignore for a test repo, and seven settings that make the commands above safer by default.

.gitignorebash
# Playwright/test-results//playwright-report//blob-report//playwright/.cache/# saved logins: never commit them/playwright/.auth/ # Allure, Cypress, coverage/allure-results//allure-report//cypress/videos//cypress/screenshots//coverage/ # Secrets and local noise.env.env.*!.env.examplenode_modules/.DS_Store
~/.gitconfig setupbash
git config --global pull.rebase true             # pull never creates merge bubblesgit config --global rebase.autoStash true        # rebase with uncommitted editsgit config --global push.autoSetupRemote true    # first push needs no -ugit config --global push.useForceIfIncludes true # safer --force-with-leasegit config --global fetch.prune true             # drop deleted remote branchesgit config --global init.defaultBranch maingit config --global alias.lg "log --oneline --graph --decorate -20"

If you committed it, Git can give it back.
Commit early, push with a lease, and the reflog keeps the rest.

Download the poster

All eight groups on one sheet, with what each command does. Free PNG, print it or pin it in your team chat.

AQAPro poster: Git commands for QA automation engineers in eight groups: sync, save and share, test a PR, history, bisect, undo, stash and cherry-pick, clean environment.

FAQ

What Git commands does a QA engineer need?

Daily: git status, git switch, git pull --rebase, git add -p, git commit and git push. For testing PRs: git fetch, git diff --stat main...HEAD and git worktree. For investigations: git log -S, git blame and git bisect run. For mistakes: git restore, git revert and git reflog. That covers almost everything a test automation engineer runs.

What is the difference between git switch and git checkout?

git checkout does two jobs: it changes branches and it overwrites files. git switch only changes branches and git restore only restores files, so a typo can't make one do the other's job. They were added in Git 2.23 and are no longer marked experimental since Git 2.51. git checkout still works.

How do I find which commit broke a test?

Use git bisect. Run git bisect start <bad> <good>, then git bisect run followed by your test command, for example npx playwright test checkout.spec.ts. Git checks out the middle commit, runs the test and halves the range until it prints the first bad commit. 16 commits take 4 runs, 1,000 take about 10. Finish with git bisect reset.

How do I undo git reset --hard?

Run git reflog. It lists every commit HEAD pointed to, including the ones a reset left behind. Find the line from before the reset and run git reset --hard HEAD@{n} with that number. This only brings back committed work: edits you never committed or stashed are lost.

Is git push --force safe?

No. It overwrites the remote branch even if a teammate pushed in the meantime, and their commits disappear. git push --force-with-lease --force-if-includes refuses in that case and reports the push as rejected ('stale info' or 'remote ref updated since checkout'). Pull, rebase and push again.

What should a test automation repo put in .gitignore?

Test output and secrets: test-results/, playwright-report/, blob-report/, playwright/.auth/ (saved logins), allure-results/, Cypress videos and screenshots, coverage/, .env files and node_modules/. Check a single path with git check-ignore -v <path>.