Skip to content

Commit 6e0a8eb

Browse files
gkalpakjosephperrott
authored andcommitted
docs: add info about working with fixup commits (angular#39110)
Using fixup commits when addressing review feedback can considerably improve the review experience on subsequent reviews. This commit adds information and guidelines for contributors on how to work with fixup commits. Fixes angular#33042 PR Close angular#39110
1 parent 21dfb33 commit 6e0a8eb

File tree

3 files changed

+120
-9
lines changed

3 files changed

+120
-9
lines changed

.pullapprove.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ groups:
11141114
'docs/DEBUG.md',
11151115
'docs/DEBUG_COMPONENTS_REPO_IVY.md',
11161116
'docs/DEVELOPER.md',
1117+
'docs/FIXUP_COMMITS.md',
11171118
'docs/GITHUB_PROCESS.md',
11181119
'docs/PR_REVIEW.md',
11191120
'docs/SAVED_REPLIES.md',

CONTRIBUTING.md

+24-9
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Before you submit your Pull Request (PR) consider the following guidelines:
107107
Adherence to these conventions is necessary because release notes are automatically generated from these messages.
108108

109109
```shell
110-
git commit -a
110+
git commit --all
111111
```
112112
Note: the optional commit `-a` command line option will automatically "add" and "rm" edited files.
113113

@@ -119,16 +119,31 @@ Before you submit your Pull Request (PR) consider the following guidelines:
119119

120120
11. In GitHub, send a pull request to `angular:master`.
121121

122-
If we ask for changes via code reviews then:
123122

124-
* Make the required updates.
125-
* Re-run the Angular test suites to ensure tests are still passing.
126-
* Rebase your branch and force push to your GitHub repository (this will update your Pull Request):
123+
#### Addressing review feedback
127124

128-
```shell
129-
git rebase master -i
130-
git push -f
131-
```
125+
If we ask for changes via code reviews then:
126+
127+
1. Make the required updates to the code.
128+
129+
2. Re-run the Angular test suites to ensure tests are still passing.
130+
131+
3. Create a fixup commit and push to your GitHub repository (this will update your Pull Request):
132+
133+
```shell
134+
git commit --all --fixup HEAD
135+
git push
136+
```
137+
138+
For more info on working with fixup commits see [here](docs/FIXUP_COMMITS.md).
139+
140+
> Fixup commits (as shown above) are preferred when addressing review feedback, but in some cases you may need to amend the original commit instead of creating a fixup commit (for example, if you want to update the commit message).
141+
> To amend the last commit and update the Pull Request:
142+
>
143+
> ```shell
144+
> git commit --all --amend
145+
> git push --force-with-lease
146+
> ```
132147

133148
That's it! Thank you for your contribution!
134149

docs/FIXUP_COMMITS.md

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Working with fixup commits
2+
3+
This document provides information and guidelines for working with fixup commits:
4+
- [What are fixup commits](#about-fixup-commits)
5+
- [Why use fixup commits](#why-fixup-commits)
6+
- [Creating fixup commits](#create-fixup-commits)
7+
- [Squashing fixup commits](#squash-fixup-commits)
8+
9+
[This blog post](https://thoughtbot.com/blog/autosquashing-git-commits) is also a good resource on the subject.
10+
11+
12+
## <a name="about-fixup-commits"></a> What are fixup commits
13+
14+
At their core, fixup commits are just regular commits with a special commit message:
15+
The first line of their commit message starts with "fixup! " (notice the space after "!") followed by the first line of the commit message of an earlier commit (it doesn't have to be the immediately preceding one).
16+
17+
The purpose of a fixup commit is to modify an earlier commit.
18+
I.e. it allows adding more changes in a new commit, but "marking" them as belonging to an earlier commit.
19+
`Git` provides tools to make it easy to squash fixup commits into the original commit at a later time (see [below](#squash-fixup-commits) for details).
20+
21+
For example, let's assume you have added the following commits to your branch:
22+
23+
```
24+
feat: first commit
25+
fix: second commit
26+
```
27+
28+
If you want to add more changes to the first commit, you can create a new commit with the commit message:
29+
`fixup! feat: first commit`:
30+
31+
```
32+
feat: first commit
33+
fix: second commit
34+
fixup! feat: first commit
35+
```
36+
37+
38+
## <a name="why-fixup-commits"></a> Why use fixup commits
39+
40+
So, when are fixup commits useful?
41+
42+
During the life of a Pull Request, a reviewer might request changes.
43+
The Pull Request author can make the requested changes and submit them for another review.
44+
Normally, these changes should be part of one of the original commits of the Pull Request.
45+
However, amending an existing commit with the changes makes it difficult for the reviewer to know exactly what has changed since the last time they reviewed the Pull Request.
46+
47+
Here is where fixup commits come in handy.
48+
By addressing review feedback in fixup commits, you make it very straight forward for the reviewer to see what are the new changes that need to be reviewed and verify that their earlier feedback has been addressed.
49+
This can save a lot of effort, especially on larger Pull Requests (where having to re-review _all_ the changes is pretty wasteful).
50+
51+
When the time comes to merge the Pull Request into the repository, the merge script [knows how to automatically squash](../dev-infra/pr/merge/strategies/autosquash-merge.ts) fixup commits with the corresponding regular commits.
52+
53+
54+
## <a name="create-fixup-commits"></a> Creating fixup commits
55+
56+
As mentioned [above](#about-fixup-commits), the only thing that differentiates a fixup commit from a regular commit is the commit message.
57+
You can create a fixup commit by specifying an appropriate commit message (i.e. `fixup! <original-commit-message-subject>`).
58+
59+
In addition, the `git` command-line tool provides an easy way to create a fixup commit via [git commit --fixup](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---fixupltcommitgt):
60+
61+
```sh
62+
# Create a fixup commit to fix up the last commit on the branch:
63+
git commit --fixup HEAD ...
64+
65+
# Create a fixup commit to fix up commit with SHA <COMMIT_SHA>:
66+
git commit --fixup <COMMIT_SHA> ...
67+
```
68+
69+
70+
## <a name="squash-fixup-commits"></a> Squashing fixup commits
71+
72+
As mentioned above, the merge script will [automatically squash](../dev-infra/pr/merge/strategies/autosquash-merge.ts) fixup commits.
73+
However, sometimes you might want to manually squash a fixup commit.
74+
75+
76+
### Rebasing to squash fixup commits
77+
78+
The easiest way to re-order and squash any commit is via [rebasing interactively](https://git-scm.com/docs/git-rebase#_interactive_mode). You move a commit right after the one you want to squash it into in the rebase TODO list and change the corresponding action from `pick` to `fixup`.
79+
80+
`Git` can do all these automatically for you if you pass the `--autosquash` option to `git rebase`.
81+
See the [`git` docs](https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt---autosquash) for more details.
82+
83+
84+
### Additional options
85+
86+
You may like to consider some optional configurations:
87+
88+
89+
#### Configuring `git` to auto-squash by default
90+
91+
By default, `git` will not automatically squash fixup commits when interactively rebasing.
92+
If you prefer to not have to pass the `--autosquash` option every time, you can change the default behavior by setting the `rebase.autoSquash` `git` config option to true.
93+
See the [`git` docs](https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt-rebaseautoSquash) for more details.
94+
95+
If you have `rebase.autoSquash` set to true, you can pass the `--no-autosquash` option to `git rebase` to override and disable this setting.

0 commit comments

Comments
 (0)