Skip to content

Commit 65e584a

Browse files
committed
Add post "2023-05-30-github-repository-setup.md"
1 parent 8817911 commit 65e584a

12 files changed

Lines changed: 130 additions & 0 deletions
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
layout: post
3+
title: "Sane defaults for Github repositories"
4+
date: 2023-05-30 06:20:32 -0700
5+
categories: scala
6+
post_photo: assets/posts/github-repository-setup/post_photo.png
7+
permalink: /blog/:slug
8+
---
9+
10+
While working with multiple customers, we commonly suggest potential improvements to their Github repository setup, there is a chance others can benefit from this which is why I'm sharing the settings we follow while create new repositories.
11+
12+
![branch-list](/assets/posts/github-repository-setup/branch-list.png)
13+
14+
Have you ever seen a situation like this? Many stale branches because developers forgot to delete those, this is a very common problem, I have even seen repositories with 1000+ stale branches which makes it hard to navigate through them, the below settings mention a way to prevent this as well as other useful details.
15+
16+
It would be nice if we could store these settings so that they get applied to new repositories by default, unfortunately, Github does not seem to support this.
17+
18+
19+
## Summary
20+
21+
We'll tweak the repository `General` and `Branches` settings so that:
22+
23+
1. Enable `Automatically delete head branches`.
24+
2. Enable `Always suggest updating pull request branches`.
25+
3. Disable `Allow merge commits` (read below before arguing).
26+
4. Update `Allow squash and merging` -> `Default commit message`.
27+
28+
Then, create a branch protection rule for important branches:
29+
30+
1. Enable `Require a pull request before merging` -> `Require approvals`.
31+
2. Enable `Require status checks to pass before merging` -> `Require branches to be up to date before merging`
32+
3. Enable `Require linear history`.
33+
34+
The reasons behind these settings are explained below.
35+
36+
37+
## General settings
38+
39+
Access these items through the `Settings` button that's on each repository:
40+
41+
![general-item](/assets/posts/github-repository-setup/general-item.png)
42+
43+
44+
## 1. Automatically delete head branches
45+
46+
This is likely the item that most projects can apply, I don't know why Github does not set this by default.
47+
48+
![auto-delete-branches](/assets/posts/github-repository-setup/auto-delete-branches.png)
49+
50+
Remember the stale branches issue that's mentioned in the intro? This is how you can prevent it, as soon as a Pull Request gets merged, the branch will be deleted automatically by Github.
51+
52+
Is there any reason to keep these stale branches around?
53+
54+
55+
## 2. Always suggest updating pull request branches
56+
57+
There are cases when a Pull Request has passed all the CI workflows but these workflows fail when merging the code just because the PR was out of date.
58+
59+
Updating branches manually can be tedious, gladly, Github can place a button to update the branch:
60+
61+
![suggest-updating-pull-requests](/assets/posts/github-repository-setup/update-branch-suggestion.png)
62+
63+
The only reason I can find to not enable this option is when the repository requires signed commits, because Github can't sign the merge commit that updates the branch.
64+
65+
This option can be enabled at the General settings too.
66+
67+
![suggest-updating-pull-requests](/assets/posts/github-repository-setup/suggest-updating-pr.png)
68+
69+
70+
## 3. Pull Request settings
71+
72+
On the PR settings, I disable the `Allow merge commits` to keep a simple commit history, before you start arguing about this, let me tell you, this option unlocks the usage of [git bisect](https://git-scm.com/docs/git-bisect) which is totally worth it:
73+
74+
![pull-request-settings](/assets/posts/github-repository-setup/pr-settings.png)
75+
76+
Also, on the `Squash and merging` item, update the default commit message to `Default to pull request title and description`, this will make sure that most merged PRs will get a single commit with the message that's included on the PR description (your team includes a decent one, right?).
77+
78+
`Squash` means mixing all the commits from a PR into a single one, this means that devs can still push many commits into a single PR but rolling back a feature requires reverting a single commit. Which is another advantage.
79+
80+
81+
## 4. Branch protection rules
82+
83+
We are interested in the `Branches settings` entry which can be found a few items below the `General` settings:
84+
85+
![branches-item](/assets/posts/github-repository-setup/branches-item.png)
86+
87+
Let's add rules to protect our important branches, usually, a single branch needs to be protected, `master`/`main`, this depends on your team setup:
88+
89+
![add-branch-protection-rule](/assets/posts/github-repository-setup/add-branch-protection-rule.png)
90+
91+
92+
The options to enable are shown in this screenshot:
93+
94+
![branch-protection-rules](/assets/posts/github-repository-setup/branch-protection-rules.png)
95+
96+
All of these options are explained below.
97+
98+
99+
### 4.1. Require a pull request before merging
100+
101+
Most teams aren't ready to allow all team-members to submit commits directly to `main` branch, if this is your case, enabling the `Require a pull request before merging` option would prevent devs to introduce any code directly to the protected branches, requiring them to submit a PR.
102+
103+
104+
### 4.2. Require approvals
105+
106+
If your team does code reviews, enabling the `Require approvals` options would prevent any PR to get merged unless it has `N` approvals, `1` is usually enough for most projects.
107+
108+
Btw, if you do code reviews, you can also do code previews with https://codepreview.io, mention this post to get an extended trial.
109+
110+
111+
### 4.3. Require status checks to pass before merging
112+
113+
Enabling the `Require status checks to pass before merging` option allows you to define some conditions that need to be met before being able to merge a PR, for example:
114+
115+
1. Enabling `Require branches to be up to date before merging` prevents that outdated branches get merged.
116+
2. Enabling other status checks is also possible, this can prevent PRs to get merged when the CI workflows are failing.
117+
118+
![pull-request-status-checks](/assets/posts/github-repository-setup/pr-status-checks.png)
119+
120+
121+
### 4.4 Require linear history
122+
123+
This ones goes hand by hand with the `Allow merge commits` option disabled by a previous step, this step will make sure that no merge commits will be allowed, which unlocks the power from [git bisect](https://git-scm.com/docs/git-bisect).
124+
125+
126+
## Conclusion
127+
128+
We have seen many useful settings for Github repositories which can be applied to most projects, these can improve your experience while working with Github.
129+
130+
Do you know anything else that could possibly be set as default?
46.7 KB
Loading
22.5 KB
Loading
150 KB
Loading
339 KB
Loading
36.7 KB
Loading
36.7 KB
Loading
7.08 KB
Loading
82.8 KB
Loading
76.6 KB
Loading

0 commit comments

Comments
 (0)