Git and Pull Requests
Git records every change to the project, and GitHub holds the team's copy. Each change happens on its own branch, and a teammate reads it in a pull request before it reaches main.
- The
Workshopproject from Project Setup, building clean. - Git installed from git-scm.com. The WPILib installer does not include it.
- A GitHub account, and a teammate with one, for the review step.
Three words carry this page. A commit is a saved snapshot of the project with a message saying what changed. A branch is a line of commits you can work on without touching anyone else's. The main branch is the one the robot runs, so it only ever holds code somebody else has read.
Put the project on GitHub
One person on the team does this once. Everyone else clones the result.
- Open the
Workshopfolder in the WPILib VS Code and open Source Control with Ctrl+Shift+G. - Click Initialize Repository. You should see: every project file listed under Changes, and no
buildfolder among them. - Type
Generated projectin the message box and click Commit. Answer Yes when it offers to stage all your changes. - Click Publish Branch, sign in to GitHub when asked, and choose a private repository.
A teammate gets a copy with Git: Clone from the command palette, then Clone from GitHub, then the repository name. Clone into Downloads or another plain local folder, for the same reason as the project.
Branch, commit, push
Every change starts from an up to date main. Pull first, then branch, so your work starts from what the team has now.
- Click the branch name at the bottom left of the window. Check it says
main, then open … in Source Control and choose Pull. - Click the branch name again, choose Create new branch, and name it for the change:
arm-intake-angle, notmy-branch. - Make the change and build it. Then commit with a message that says what the code now does:
Lower the intake angle to 0.48. - Click Publish Branch. Later commits on the same branch go up with Sync Changes.
Commit whenever the project builds and one thing is finished. Five small commits are easier to read in review, and easier to undo, than one commit holding a whole evening.
git switch maingit pullgit switch -c arm-intake-anglegit add -Agit commit -m "Lower the intake angle to 0.48"git push -u origin arm-intake-anglePull request and review
- Open the repository on GitHub. A banner offers Compare & pull request for the branch you just pushed. Click it.
- Check the base is
main. In the description, write what changed and how you tested it: "Ran in hardware simulation, arm stops at the new angle." - Add a teammate under Reviewers and click Create pull request.
- The reviewer opens Files changed, leaves a comment on any line they question, and chooses Approve or Request changes. New commits pushed to the branch join the same pull request.
- Once approved, click Merge pull request, then Delete branch. Back in VS Code, switch to main and pull.
The reviewer checks that the change does what the description says and nothing else, and that it follows the conventions below. When a line is unclear, they ask. The answer usually belongs in a comment in the code, where the next reader will find it.
Team conventions
Every branch of Workshop-Code formats itself with Spotless, using the same Google Java Format that WPILib uses. Add it to your build.gradle once: the plugin line goes inside the existing plugins block, and the rest goes at the bottom of the file.
plugins { // ...the three plugins already here id "com.diffplug.spotless" version "8.9.0"}spotless { lineEndings = 'PRESERVE' java { target fileTree('.') { include 'src/**/*.java' exclude '**/build/**', '**/bin/**' } toggleOffOn() googleJavaFormat('1.35.0') removeUnusedImports() trimTrailingWhitespace() endWithNewline() }}// Format on every build.tasks.withType(JavaCompile).configureEach { dependsOn 'spotlessApply'}With that last block, every Build Robot Code formats the project first, so nobody argues about indentation in review. To format without building, run .\gradlew spotlessApply in the VS Code terminal. .\gradlew spotlessCheck only reports.
The rest of what this course already does, written down:
- A mechanism class is named for the thing:
Arm,Flywheel. Its command methods are named for the result:vertical(),runFast(). - A method that answers yes or no starts with
is, likeisAtTarget(). - Lambdas are written
() -> stopMotor(), neverthis::stopMotor. - One change per pull request, on a branch named for it.
- Nobody commits to main directly, including mentors.
GitHub can enforce the last rule. In the repository, open Settings, then Branches, add a rule for main, and tick Require a pull request before merging.
When it goes wrong
- Merge conflict. GitHub says the branch has conflicts, because two branches changed the same lines. In VS Code, switch to main and pull, switch back to your branch, then …, Branch, Merge, and pick
main. Each conflict shows both versions between<<<<<<<and>>>>>>>markers. Pick Accept Current, Accept Incoming, or edit by hand, then build, commit and sync. - Build output in the commit. Files under
build/or.gradle/appear in Changes. The generated.gitignoreexcludes both, so it is missing or the project sits inside another repository. Copy.gitignoreback from a fresh project, rungit rm -r --cached build, and commit. - Forgot to pull. The push is rejected and VS Code says to pull first, because the remote branch has commits you do not. Pull, build, then push again. A branch made from a stale main shows up later as a conflict, so the branch step starts with a pull.
Check your work
Make one small change on a branch, such as a comment above Robot, and take it all the way through review.
You should see
- A merged pull request on GitHub, with a teammate's approval on it.
- Your commit message at the top of main's history on GitHub.
- Your teammate pulls main, builds, and gets
BUILD SUCCESSFULwith your change in it.
Check yourself
You want to change the flywheel's fast speed. You are on main. What do you do first?
Source Control lists two hundred files under build/ after your first build. What went wrong?
Your pull request fixes the arm angle and also renames every method in Flywheel. What should the reviewer ask for?
Your push is rejected with a message to pull first. What happened?