QA AND TEST AUTOMATION · PART 2 · PROGRAMMING FOR TESTERS · TOOLS · 27 OF 112

Git for Testers: Branches, Commits, Pull Requests

Git as a tester uses it: the working tree, staging area and commits; clone, status, diff, add and commit; branches and switching; push, pull and pull requests with review; merge conflicts and how to solve them; .gitignore for build output and secrets; and the habits of a clean test repository. With a real feature branch for a new ShopRung test.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Working tree → staging → commit
  • branches and PRs
  • reviews and CI
  • conflicts
  • .gitignore and no secrets

Notes

The idea

Git keeps your work in three places. The working tree is the files you are editing. The staging area holds the changes you have chosen for the next commit. The repository holds commits, which are saved snapshots of the whole project, each with a message, an author and an id. You move a change from working tree to staging with git add, and from staging into history with git commit.

Three places
  • working tree: the files you are editing right now
  • staging area: the changes you chose for the next commit
  • repository: commits, the saved history
  • a commit = a snapshot with a message, an author and an id

Everyday commands

The daily loop. Git clone gets a copy of a repository. Git status shows what changed, and git diff shows exactly which lines. Git add stages the files you want. Git commit with a message saves them. Git log with one line lists the history. A good habit for beginners: run git status before and after every command. It tells you where things are and usually suggests the next command.

The daily loop
git clone <url>      # get a copy
git status           # what changed
git diff             # line by line
git add src/test/...  # stage
git commit -m "Test SAVE10 at
  exactly $50.00 (PRICE-3)"
git log --oneline    # history

run git status before and
after every step

Good commits

Commits should be small, one logical change each, with a message that says what and why. Changes and fixed stuff help nobody. Add boundary tests for save ten, with the rule id, tells a reviewer exactly what to expect. Write in the imperative mood, add, fix, remove, and link the ticket or requirement. Six months later, when a test starts failing, a good history is how you find out why it exists.

Commit messages
  • weak "changes"
  • weak "fixed stuff"
  • strong "Add boundary tests for SAVE10 (PRICE-3)"
  • strong "Reset ShopRung data before each API test"
  • one logical change per commit
  • imperative mood: Add, Fix
  • link the ticket or rule id

Branches

Never work directly on main, the branch everyone else runs. Switch to main, pull the latest, then create your own branch with git switch dash c and a descriptive name, like test slash save ten boundaries. Write and commit your tests there. Then push the branch to GitHub with git push dash u origin and the branch name. The dash u links your local branch to the remote one, so later pushes and pulls need no extra arguments.

A feature branch
git switch main
git pull
git switch -c test/save10-
  boundaries
... write tests, commit ...
git push -u origin test/save10-
  boundaries

main = what CI and the team run
one branch per change

Pull requests

A pull request asks the team to merge your branch into main. Give it a clear title, what you changed, why, how to check it, and proof. For test code, the best proof is that the new tests fail with the bug switched on and pass with it off. The pipeline runs the suite automatically, and it must be green before merging. Small pull requests get reviewed quickly. Huge ones wait for days.

A good pull request
  • title: Add SAVE10 boundary tests (PRICE-3)
  • what: 4 parameterized cases 4999, 5000, 5004, 5005
  • why: coupon-boundary bug escaped last release
  • how to check: mvn test
  • proof: fails with the bug on, passes with it off
  • CI must be green

Reviews

Test code deserves real review. Does it test what its name says? Would it actually fail if the feature broke? Is it independent and repeatable? Any sleeps, hard coded secrets or addresses? Are names and failure messages clear? Comment on the code, not the person, and kindly. The author answers by pushing new commits to the same branch, and the pull request updates itself. Some teams squash the commits into one when merging.

Reviewing test code
  • does it test what it says?
  • would it FAIL if the feature broke?
  • independent and repeatable?
  • no sleeps, no hard-coded secrets or URLs
  • clear names and messages
  • comment on the code, kindly;
  • fix with new commits

Conflicts

A merge conflict happens when two branches change the same lines. Git cannot guess which is right, so it marks the spot: your version above the equals signs, the other branch's below. Here one branch says free shipping starts at seventy five dollars and the other at eighty. Do not just pick one. Find out which is correct, maybe by asking, edit the file to the right result, delete the markers, then add and commit. IDEs show conflicts side by side, which helps.

A merge conflict
<<<<<<< HEAD
FREE_SHIPPING_FROM = 7500;
=======
FREE_SHIPPING_FROM = 8000;
>>>>>>> test/new-threshold

1. keep the right version
   (ask if unsure!)
2. delete the markers
3. git add + git commit

.gitignore

A dot gitignore file lists what Git must never track. Build output like target and node modules. Test reports and results folders. IDE settings. Log files. And above all, the dot env file where local secrets like passwords and API keys live. Secrets belong in the pipeline's secret variables or a local file that is ignored. Once a secret is pushed, assume it is leaked: rotate it, because deleting the file does not remove it from history.

.gitignore for a test repo
target/
node_modules/
test-results/
playwright-report/
.idea/
.env
*.log

secrets go in CI variables
or a local .env, never Git

Staying in sync

Stay in sync. Pull main often, and bring its new commits into your branch with git merge main, or git rebase main if your team prefers a straight history. Git stash parks unfinished changes so you can switch branches, and stash pop brings them back. And if you committed too early, git reset dash dash soft head tilde one undoes the last commit while keeping your files. Lab five walks through each of these on a real branch.

Keep up to date
git switch main && git pull
git switch test/save10-...
git merge main
  # or: git rebase main

git stash    # park changes
git stash pop

undo the last commit, keep
files: git reset --soft HEAD~1

Common mistakes

Git mistakes testers make. Committing straight to main. Git add dot with a secrets file or a report folder sitting there. Pull requests with forty files nobody wants to review. Messages like update. Force pushing to a branch other people use; if you must rewrite your own branch, use force with lease, which refuses if someone else pushed in the meantime. And resolving conflicts by blindly taking one side.

Avoid these
  • committing straight to main
  • git add . with secrets or reports in the folder
  • huge PRs with 40 files
  • messages like "update"
  • force-push to a shared branch (use --force-with-lease, and only on your own branch)
  • resolving conflicts blindly

Say it like this

A common question: walk me through how you add a new test to the suite. Say: I pull main and create a feature branch, write the test, and check it fails when the bug is present and passes when it is not. I commit small changes with clear messages, push, and open a pull request explaining what, why and the proof. CI must be green and a teammate reviews it before merge. Secrets and build output never go into Git.

In the interview
  • "I pull main, create a feature
  • branch, write the test and see it fail with the bug and pass without, commit small
  • changes with clear messages, push and open a PR with what, why and proof. CI must be green and a teammate reviews
  • it. Secrets stay out of Git."

Interview questions

Q1What is the difference between git add and git commit?

git add moves changes into the staging area; git commit saves the staged changes as a snapshot in the repository history.

Q2What is the difference between git pull and git fetch?

git fetch downloads new commits from the remote without changing your files; git pull fetches and then merges (or rebases) them into your current branch.

Q3What is a merge conflict and how do you resolve it?

Two branches changed the same lines. Edit the file to the correct content, remove the conflict markers, then git add and commit (or continue the rebase).

Q4What is the difference between merge and rebase?

Merge joins histories with a merge commit; rebase replays your commits on top of another branch for a linear history. Do not rebase commits others already use.

Q5What should go in .gitignore for a test project?

Build output (target, node_modules), reports (test-results, playwright-report), IDE files, logs and any local secrets file such as .env.

Q6What do you do if you committed a password?

Treat it as leaked: rotate the secret immediately, then remove it from the code and, if required, from history; deleting the file alone is not enough.

Q7What makes a good pull request?

A small, focused change with a clear title, what and why, how to verify, proof (tests failing with the bug and passing without), green CI and a reviewer.

Q8What does git stash do?

It saves uncommitted changes aside and cleans the working tree, so you can switch branches; git stash pop brings them back.