You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/cleaning-up-commits.md
+33-12Lines changed: 33 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -17,16 +17,28 @@ It can be difficult to follow the changes in a pull request when the number of c
17
17
18
18
### Rebasing
19
19
20
-
Sometimes a pull request can be based on a much earlier commit in the branch that you are trying to merge into it, causing a large amount of commits and file changes to litter the pull request. In this case, it would be better to **rebase** (move branches around by changing the commit that they are based on). After rebasing, you will want to close the pull request and open a new one, which will now have fewer commits.
20
+
Sometimes a pull request can be based on a much earlier commit in the branch that you are trying to merge into it, causing a large amount of commits and file changes to litter the pull request. In this case, it would be better to **rebase** (move branches around by changing the commit that they are based on).
21
21
22
-
For example, if you're working from the branch **feature** and are trying to rebase with **master**, you may run one of the following commands:
23
-
> `git rebase master`
24
-
> `git rebase master feature`
22
+
For example, if you're working from the branch **feature** and are trying to rebase with **dev**, you'll first want to pull the latest changes from **dev**:
25
23
26
-
You can also rebase with the following command:
27
-
> `git pull --rebase`
24
+
```
25
+
git pull upstream dev
26
+
```
28
27
29
-
A normal `git pull` is equivalent to `git fetch` followed by `git merge FETCH_HEAD`, but when you run `git pull --rebase`, it runs `git rebase` instead of `git merge`.
28
+
Next, you'll want to "uncommit" all of the changes in **feature** that differ from **dev**:
29
+
30
+
```
31
+
git reset --soft upstream/dev
32
+
```
33
+
34
+
Finally, make a small number of commits with the changes you have made and push them to your fork:
35
+
36
+
```
37
+
< commit changes >
38
+
git push origin feature -f
39
+
```
40
+
41
+
**Note**: the `-f` must be included when pushing to your fork for the rebase to be successful
30
42
31
43
For more information on rebasing, click [here](https://git-scm.com/docs/git-rebase).
32
44
@@ -35,7 +47,10 @@ For more information on rebasing, click [here](https://git-scm.com/docs/git-reba
35
47
When your pull request has a group of commits that can be condensed into one, logical commit, use **squashing**. This will clean up the number of commits your pull request has while also grouping together common commits.
36
48
37
49
For example, if you wanted to squash the last three commits into one, you may run the following command:
38
-
> `git rebase -i HEAD~3`
50
+
51
+
```
52
+
git rebase -i HEAD~3
53
+
```
39
54
40
55
This will bring up an editor showing your last three commits. Pick a commit to keep (as the message), and squash the other two into it.
41
56
@@ -46,13 +61,19 @@ For more information on squashing, click [here](https://git-scm.com/book/en/v2/G
46
61
If you want to merge specific commits from another branch into the current one you are working from, use **cherry-picking**.
47
62
48
63
For example, if you're working on the **master** branch and want to pull commit X (the commit-hash) from the **feature** branch, you may run the following commands:
49
-
> `git checkout master`
50
-
> `git cherry-pick X -n`
64
+
65
+
```
66
+
git checkout master
67
+
git cherry-pick X -n
68
+
```
51
69
52
70
The `-n`, or `--no-commit`, is recommended for cherry-picking because it won't automatically create a commit for the cherry-picked change; this will allow you to view the changes first and make sure that you want to add all everything from the cherry-picked commit.
53
71
54
72
Now, if you want to cherry-pick a range of commits, say X through Y, from the **feature** branch, you may run the following commands:
55
-
> `git checkout -b temp-branch X`
56
-
> `git rebase --onto master Y^`
73
+
74
+
```
75
+
git checkout -b temp-branch X
76
+
git rebase --onto master Y^
77
+
```
57
78
58
79
For more information on cherry-picking, click [here](https://git-scm.com/docs/git-cherry-pick).
0 commit comments