Source control
Git with a single main branch, short-lived branches, pull requests, and branch protection so main only moves when the verify check is green.
The DevOps overview starts from a change a person can trace. This lab is that trace: Git, one long-lived branch, and a pull request that cannot land until the check from the continuous integration lab is green.
The model is trunk-based development. main is the trunk. A branch lives for one change and is deleted after it merges. You do not keep a develop branch that drifts away from what you deploy.
Prerequisites
- Git 2.40 or newer (
git --version) - A hosted repository you can administer, on GitHub or GitLab
- The GitHub CLI
gh, if the remote is GitHub (gh auth status)
The examples use a warehouse config file so the commands do not depend on the Java labs. The same rules apply to the catalog, the locations service, and the manifests that deploy them.
One branch that you can release
main is always in a state you would deploy. That is the whole policy. A few rules keep it true:
- Branch from
main, and merge back tomain. There is no second long-lived branch. - One change per branch. A branch that has been open for weeks is two changes pretending to be one.
- Merge through a pull request. Nobody pushes straight to
main, including you. - The
verifycheck has to pass. That check is defined in the CI lab. Protection is what makes it mandatory. - Delete the branch after the merge.
- Unfinished work that has to sit on
mainis behind a flag in the application, not behind a branch nobody can see.
Force-push and deletion of main stay off. A mistake on main is a new commit.
Step 1: Start the repository
Set user.name and user.email first if this machine has never committed.
mkdir warehouse && cd warehouse
git init -b main
printf 'warehouse: dhk-1\nreorder-point: 5\n' > config.yaml
git add config.yaml
git commit -m "Add the warehouse code and the reorder point"
git remote add origin git@github.com:<owner>/<repo>.git
git push -u origin main
git init -b main names the trunk main from the first commit. If your Git is older and still defaults to master, rename it with git branch -m main before the first push. Use the SSH or HTTPS URL of the repository you actually created. The commands are the same against a GitLab remote.
git status is clean. git log --oneline shows one commit.
Step 2: A short-lived branch and a pull request
git switch main
git pull
git switch -c feat/reorder-point
printf 'warehouse: dhk-1\nreorder-point: 3\n' > config.yaml
git diff
git add config.yaml
git commit -m "Lower the reorder point to 3"
git push -u origin HEAD
git diff before git add is the review you do yourself. git add -p is the same habit when a file holds two unrelated edits: stage one of them.
Open the pull request against main:
gh pr create --base main --title "Lower the reorder point to 3" \
--body "Warehouse dhk-1 reorders at 3. The verify check must be green."
On GitLab, push the same branch and open a merge request into main. The page lists the commits that main does not have. That list should be the one commit above.
Ask for one review. When verify is green and the review is in, merge with the hosting site's merge button. Then remove the branch:
git switch main
git pull
git branch -d feat/reorder-point
git push origin --delete feat/reorder-point
git branch -d refuses to delete a branch you have not merged. That refusal is the point.
Step 3: Protect main
Protection is a setting on the remote. A local Git hook can be skipped. The host cannot.
On GitHub, save protection.json:
{
"required_status_checks": {
"strict": true,
"contexts": ["verify"]
},
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1,
"dismiss_stale_reviews": true
},
"restrictions": null,
"allow_force_pushes": false,
"allow_deletions": false,
"required_conversation_resolution": true
}
strict means the branch has to be up to date with main before it merges, so two green pulls cannot land in an order nobody tested. contexts names the check from CI. Until that check exists, GitHub reports it as expected and missing, and the pull request stays blocked. Add the check in the CI lab, then come back to this file.
gh api --method PUT -H "Accept: application/vnd.github+json" \
/repos/<owner>/<repo>/branches/main/protection \
--input protection.json
gh api /repos/<owner>/<repo>/branches/main/protection --jq '.required_status_checks.contexts'
The second call should print verify. A direct push now fails:
git switch main
printf 'warehouse: dhk-1\nreorder-point: 1\n' > config.yaml
git commit -am "Try to skip the pull request"
git push
The push is rejected. Put the commit on a branch instead. git reset --hard origin/main drops the rejected commit from your local main if you do not want it.
On GitLab the same rules are a protected branch. push_access_level=0 means nobody pushes. merge_access_level=40 means a Maintainer merges. Replace the host and the project id:
curl --request POST --header "PRIVATE-TOKEN: <token>" \
"https://<gitlab>/api/v4/projects/<id>/protected_branches?name=main&push_access_level=0&merge_access_level=40&allow_force_push=false"
In the project settings, require a pipeline named like verify to succeed before merge. The GitLab CI lab is that pipeline.
What you can see afterwards
git log --oneline --decorate -5
gh pr list --state merged
Every change on main has a pull request, an author, and a green check. That is the record the rest of the DevOps labs assume. A release tag points at a commit on main, not at a branch:
git switch main
git pull
git tag -a rel-2026-10-29 -m "Warehouse config, reorder point 3"
git push origin rel-2026-10-29
Tag a commit you have already merged. Do not tag a feature branch.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Push to main succeeds |
Protection was applied to another branch, or you pushed before it | gh api the protection on main and read required_status_checks |
Pull request is blocked on verify |
The check has never reported | Add the CI job, or remove the context until the job exists |
git branch -d refuses |
The branch is not merged into the current branch | git switch main && git pull, then delete |
Two people edited config.yaml |
main moved while the branch was open |
git switch feat/... && git rebase main, then push |
What's next
Continuous integration is the verify check this page requires. Jenkins and GitLab CI are two places that check can run. OpenShift Pipelines is the third.