Skip to content

Commit fe09e68

Browse files
authored
Merge pull request Azure#3249 from cormacpayne/update-commit-doc
Update "cleaning up commits" document with an easier method for rebasing
2 parents 040141e + f23cd34 commit fe09e68

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

documentation/cleaning-up-commits.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,28 @@ It can be difficult to follow the changes in a pull request when the number of c
1717

1818
### Rebasing
1919

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).
2121

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**:
2523

26-
You can also rebase with the following command:
27-
> `git pull --rebase`
24+
```
25+
git pull upstream dev
26+
```
2827

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
3042

3143
For more information on rebasing, click [here](https://git-scm.com/docs/git-rebase).
3244

@@ -35,7 +47,10 @@ For more information on rebasing, click [here](https://git-scm.com/docs/git-reba
3547
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.
3648

3749
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+
```
3954

4055
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.
4156

@@ -46,13 +61,19 @@ For more information on squashing, click [here](https://git-scm.com/book/en/v2/G
4661
If you want to merge specific commits from another branch into the current one you are working from, use **cherry-picking**.
4762

4863
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+
```
5169

5270
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.
5371

5472
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+
```
5778

5879
For more information on cherry-picking, click [here](https://git-scm.com/docs/git-cherry-pick).

0 commit comments

Comments
 (0)