Automation7 min read

Git Commands Every QA Should Master

Beyond commit and push — the commands that separate beginners from pros.

Why Git Matters for QA

You know git add, git commit, git push. That’s enough to get by, but it’s like driving a car using only first gear.

As a QA engineer, you’ll face situations that basic commands can’t handle:

  • A test was passing last week — which commit broke it?
  • You need to switch branches but have uncommitted changes
  • Someone asks “who wrote this flaky test?”
  • You accidentally committed to the wrong branch
  • A merge conflict appeared and you’re not sure what to do

This guide covers the Git commands that solve these real problems. Bookmark this page — you’ll come back to it.

Inspection Commands

git status -sb (Short Status)

The cleaner, faster version of git status:

git status -sb

## main...origin/main
 M src/tests/LoginTest.java
?? src/tests/NewTest.java

M = modified, ?? = untracked, A = added, D = deleted

git log —oneline (Compact History)

git log --oneline -10

a1b2c3d Fix flaky test in LoginTest
e4f5g6h Add retry logic for API calls
i7j8k9l Update Selenium to 4.15
m0n1o2p Refactor page objects

git log —graph (Visual Branch History)

git log --oneline --graph --all

* a1b2c3d (HEAD -> feature/login) Fix login test
| * e4f5g6h (main) Hotfix for prod
|/
* i7j8k9l Common ancestor commit

git diff (What Changed?)

# Unstaged changes
git diff

# Staged changes (ready to commit)
git diff --staged

# Changes between branches
git diff main..feature/login

# Changes in specific file
git diff src/tests/LoginTest.java

Git Stash (Your Best Friend)

Scenario: You’re halfway through writing a test. Urgent bug comes in. You need to switch branches but don’t want to commit half-done work.

Basic Stash

# Save current changes
git stash

# Switch to hotfix branch
git checkout hotfix/urgent-bug

# ... fix the bug, commit, push ...

# Return to your branch
git checkout feature/my-tests

# Restore your changes
git stash pop

Named Stashes (Better)

# Stash with a description
git stash push -m "WIP: login test validation"

# List all stashes
git stash list
# stash@{0}: WIP: login test validation
# stash@{1}: WIP: api test refactor

# Apply specific stash
git stash apply stash@{1}

# Apply and remove from list
git stash pop stash@{0}

Stash Untracked Files

# Include new files that aren't tracked yet
git stash -u

# Include everything (even ignored files)
git stash -a

Branch Operations

Create and Switch in One Command

# Old way (two commands)
git branch feature/new-tests
git checkout feature/new-tests

# Better way
git checkout -b feature/new-tests

# Modern way (Git 2.23+)
git switch -c feature/new-tests

Track Remote Branch

# Someone created a branch, you need to work on it
git fetch origin
git checkout -b feature/login origin/feature/login

# Or simply (Git auto-tracks if names match)
git checkout feature/login

Delete Branches

# Delete local branch (safe - warns if unmerged)
git branch -d feature/old-tests

# Force delete local branch
git branch -D feature/old-tests

# Delete remote branch
git push origin --delete feature/old-tests

# Clean up stale remote references
git fetch --prune

Rename Branch

# Rename current branch
git branch -m new-name

# Rename any branch
git branch -m old-name new-name

Undoing Mistakes

Unstage Files

# Unstage specific file
git restore --staged LoginTest.java

# Unstage everything
git restore --staged .

# Old syntax (still works)
git reset HEAD LoginTest.java

Discard Local Changes

# Discard changes in specific file
git restore LoginTest.java

# Discard ALL local changes (careful!)
git restore .

# Old syntax
git checkout -- LoginTest.java

Undo Last Commit (Keep Changes)

# Undo commit, keep changes staged
git reset --soft HEAD~1

# Undo commit, keep changes unstaged
git reset HEAD~1

# Undo commit AND discard changes (dangerous!)
git reset --hard HEAD~1

Fix Last Commit Message

git commit --amend -m "Better commit message"

Add Forgotten File to Last Commit

git add ForgottenFile.java
git commit --amend --no-edit

Debugging Commands

git blame (Who Wrote This?)

# See who changed each line
git blame LoginTest.java

# Output:
# a1b2c3d (John Smith 2026-01-15) @Test
# a1b2c3d (John Smith 2026-01-15) public void testLogin() {
# e4f5g6h (Jane Doe  2026-01-20)     driver.get(URL);  // Changed URL
# Blame specific lines only
git blame -L 10,20 LoginTest.java

# Ignore whitespace changes
git blame -w LoginTest.java

git bisect (Find the Breaking Commit)

Scenario: Tests were passing 2 weeks ago, now they fail. 50 commits happened. Which one broke it?

# Start bisect
git bisect start

# Current commit is bad (tests fail)
git bisect bad

# This commit from 2 weeks ago was good
git bisect good a1b2c3d

# Git checks out middle commit
# Run your tests, then tell Git:
git bisect good  # Tests pass
# or
git bisect bad   # Tests fail

# Git narrows down, repeat until:
# "e4f5g6h is the first bad commit"

# Done - return to normal
git bisect reset

git log -S (Search for Code Changes)

# Find commits that added/removed "flaky"
git log -S "Thread.sleep" --oneline

# Find commits changing a specific method
git log -S "loginWithCredentials" --oneline

git show (Inspect a Commit)

# See full details of a commit
git show a1b2c3d

# See only files changed
git show --stat a1b2c3d

# See changes in specific file from that commit
git show a1b2c3d:src/tests/LoginTest.java

Cherry-Pick

Scenario: There’s a test fix on the develop branch. You need it on your feature branch, but don’t want to merge everything.

# Copy specific commit to current branch
git cherry-pick a1b2c3d

# Cherry-pick without committing (just stage)
git cherry-pick -n a1b2c3d

# Cherry-pick multiple commits
git cherry-pick a1b2c3d e4f5g6h

QA Use Case: Backport a Fix

# You're on release/v2.0, need a fix from main
git checkout release/v2.0
git cherry-pick abc123  # The fix commit from main
git push

Git Reflog (Recovery)

Scenario: You ran git reset --hard and lost work. Don’t panic — Git rarely truly deletes anything.

# See all recent HEAD movements
git reflog

# Output:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# e4f5g6h HEAD@{1}: commit: Add login tests
# i7j8k9l HEAD@{2}: commit: Fix API tests
# m0n1o2p HEAD@{3}: checkout: moving to feature/tests

Recover Lost Commits

# Find the commit hash in reflog
git reflog

# Recover by resetting to it
git reset --hard e4f5g6h

# Or create a branch at that point
git branch recovered-work e4f5g6h

Commands for CI/CD

Shallow Clone (Faster CI)

# Clone only recent history (faster in CI)
git clone --depth 1 https://github.com/user/repo.git

# Clone specific branch only
git clone --single-branch --branch main repo-url

Get Latest Without Checkout

# Update remote tracking branches
git fetch origin

# See what changed before pulling
git log HEAD..origin/main --oneline

Clean Build Directory

# Remove untracked files (dry run first!)
git clean -n

# Actually remove untracked files
git clean -f

# Remove untracked files AND directories
git clean -fd

# Remove ignored files too (full clean)
git clean -fdx

Get Current Commit for Reports

# Short hash (for reports)
git rev-parse --short HEAD
# Output: a1b2c3d

# Full hash
git rev-parse HEAD

# Current branch name
git branch --show-current

# Use in scripts
COMMIT=$(git rev-parse --short HEAD)
echo "Running tests on commit: $COMMIT"

Quick Reference Card

# INSPECTION
git status -sb              # Short status
git log --oneline -10       # Compact history
git diff --staged           # Review before commit
git show abc123             # Inspect commit

# STASH
git stash push -m "msg"     # Save with name
git stash list              # Show stashes
git stash pop               # Restore latest

# BRANCHES
git switch -c feature/name  # Create & switch
git branch -d branch-name   # Delete local
git push origin --delete x  # Delete remote

# UNDO
git restore --staged file   # Unstage
git restore file            # Discard changes
git reset --soft HEAD~1     # Undo commit, keep staged
git commit --amend          # Fix last commit

# DEBUG
git blame file              # Who wrote this?
git bisect start/good/bad   # Find breaking commit
git log -S "text"           # Search history

# RECOVERY
git reflog                  # Find lost commits
git reset --hard abc123     # Restore to point

# CI/CD
git clone --depth 1 url     # Shallow clone
git clean -fd               # Remove untracked
git rev-parse --short HEAD  # Get commit hash

Start Using These Today

You don’t need to memorize everything. Start with these three:

  1. git stash — Use it next time you need to switch context
  2. git diff —staged — Review before every commit
  3. git reflog — Remember it exists when you mess up

The rest will come naturally as you face situations that need them. Keep this page bookmarked — it’s your Git survival guide.