Skip to content

Commit 074371e

Browse files
committed
Incorporated suggestions into git page and edit for more brevity.
The Advanced Rebasing section has been mostly rewritten to include both a major suggestion from jyn and a general rewrite. Additional thanks to camelid for some suggestions!
1 parent 360d4a5 commit 074371e

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
- [Introduction](./contributing.md)
3333
- [About the compiler team](./compiler-team.md)
34-
- [Using git](./git.md)
34+
- [Using Git](./git.md)
3535
- [Mastering @rustbot](./rustbot.md)
3636
- [Walkthrough: a typical contribution](./walkthrough.md)
3737
- [Bug Fix Procedure](./bug-fix-procedure.md)

src/git.md

+51-46
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
# Using git
1+
# Using Git
22

3-
The Rust project uses [git] to manage its source code. In order to
3+
The Rust project uses [Git] to manage its source code. In order to
44
contribute, you'll need some familiarity with its features so that your changes
55
can be incorporated into the compiler.
66

7-
[git]: https://git-scm.com
7+
[Git]: https://git-scm.com
88

99
The goal of this page is to cover some of the more common questions and
10-
problems new contributors face. Although some git basics will be covered here,
10+
problems new contributors face. Although some Git basics will be covered here,
1111
if you find that this is still a little too fast for you, it might make sense
12-
to first read some introductions to git, such as the Beginner and Getting
12+
to first read some introductions to Git, such as the Beginner and Getting
1313
started sections of [this tutorial from Atlassian][atlassian-git]. GitHub also
1414
provides [documentation] and [guides] for beginners, or you can consult the
15-
more in depth [book from git].
15+
more in depth [book from Git].
1616

17-
[book from git]: https://git-scm.com/book/en/v2/
17+
[book from Git]: https://git-scm.com/book/en/v2/
1818
[atlassian-git]: https://www.atlassian.com/git/tutorials/what-is-version-control
1919
[documentation]: https://docs.github.com/en/github/getting-started-with-github/set-up-git
2020
[guides]: https://guides.github.com/introduction/git-handbook/
2121

2222
## Prequisites
2323

24-
We'll assume that you've installed git, forked [rust-lang/rust], and cloned the
24+
We'll assume that you've installed Git, forked [rust-lang/rust], and cloned the
2525
forked repo to your PC. We'll use the command line interface to interact
26-
with git; there are also a number of GUIs and IDE integrations that can
26+
with Git; there are also a number of GUIs and IDE integrations that can
2727
generally do the same things.
2828

2929
[rust-lang/rust]: https://github.com/rust-lang/rust
@@ -33,13 +33,13 @@ in your local repo. It may be helpful to also set up a remote for the official
3333
rust-lang/rust repo via
3434

3535
```sh
36-
git remote add rust https://github.com/rust-lang/rust.git
36+
git remote add upstream https://github.com/rust-lang/rust.git
3737
```
3838

3939
if you're using HTTPS, or
4040

4141
```sh
42-
git remote add rust [email protected]:rust-lang/rust.git
42+
git remote add upstream [email protected]:rust-lang/rust.git
4343
```
4444

4545
if you're using SSH.
@@ -51,7 +51,7 @@ and PRs:
5151

5252
1. Ensure that you're making your changes on top of master:
5353
`git checkout master`.
54-
2. Get the latest changes from the Rust repo: `git pull rust master`.
54+
2. Get the latest changes from the Rust repo: `git pull upstream master`.
5555
3. Make a new branch for your change: `git checkout -b issue-12345-fix`.
5656
4. Make some changes to the repo and test them.
5757
5. Stage your changes via `git add src/changed/file.rs src/another/change.rs`
@@ -60,7 +60,7 @@ and PRs:
6060
unintentionally commit changes that should not be committed, such as submodule
6161
updates. You can use `git status` to check if there are any files you forgot
6262
to stage.
63-
6. Push your changes to your fork: `git push -u origin issue-12345-fix`.
63+
6. Push your changes to your fork: `git push --set-upstream origin issue-12345-fix`.
6464
7. [Open a PR][ghpullrequest] from your fork to rust-lang/rust's master branch.
6565

6666
[ghpullrequest]: https://guides.github.com/activities/forking/#making-a-pull-request
@@ -82,19 +82,19 @@ to rust-lang/rust since then are in conflict with the changes you've made.
8282

8383
When this happens, you need to resolve the conflicts before your changes can be
8484
merged. First, get a local copy of the conflicting changes: Checkout your local
85-
master branch with `git checkout master`, then `git pull rust master` to
85+
master branch with `git checkout master`, then `git pull upstream master` to
8686
update it with the most recent changes.
8787

8888
### Rebasing
8989

90-
You're now ready to start the rebasing process. Check out the branch with your
90+
You're now ready to start the rebasing process. Checkout the branch with your
9191
changes and execute `git rebase master`.
9292

9393
When you rebase a branch on master, all the changes on your branch are
94-
reapplied to the most recent version of master. In other words, git tries to
94+
reapplied to the most recent version of master. In other words, Git tries to
9595
pretend that the changes you made to the old version of master were instead
9696
made to the new version of master. During this process, you should expect to
97-
encounter at least one "rebase conflict." This happens when git's attempt to
97+
encounter at least one "rebase conflict." This happens when Git's attempt to
9898
reapply the changes fails because your changes conflicted with other changes
9999
that have been made. You can tell that this happened because you'll see
100100
lines in the output that look like
@@ -113,24 +113,24 @@ Your code
113113
>>>>>>> 8fbf656... Commit fixes 12345
114114
```
115115

116-
This represents the lines in the file that git could not figure out how to
116+
This represents the lines in the file that Git could not figure out how to
117117
rebase. The section between `<<<<<<< HEAD` and `=======` has the code from
118118
master, while the other side has your version of the code. You'll need to
119119
decide how to deal with the conflict. You may want to keep your changes,
120120
keep the changes on master, or combine the two.
121121

122-
Generally, resovling the conflict consists of two steps: First, fix the
122+
Generally, resolving the conflict consists of two steps: First, fix the
123123
particular conflict. Edit the file to make the changes you want and remove the
124124
`<<<<<<<`, `=======` and `>>>>>>>` lines in the process. Second, check the
125-
surrounding code. If there was a conflict, its because someone else changed the
126-
same code you did. That means its likely there are some logical errors lying
127-
around too!
125+
surrounding code. If there was a conflict, its likely there are some logical
126+
errors lying around too! It's a good idea to run `x.py check` here to make sure
127+
there are no glaring errors.
128128

129129
Once you're all done fixing the conflicts, you need to stage the files that had
130130
conflicts in them via `git add`. Afterwards, run `git rebase --continue` to let
131-
git know that you've resolved the conflicts and it should finish the rebase.
131+
Git know that you've resolved the conflicts and it should finish the rebase.
132132
Once the rebase has succeeded, you'll want to update the associated branch on
133-
your fork with `git push -f`.
133+
your fork with `git push --force-with-lease`.
134134

135135
Note that `git push` will not work properly and say something like this:
136136

@@ -143,32 +143,37 @@ hint: 'git pull ...') before pushing again.
143143
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
144144
```
145145

146-
The advice this gives is incorrect! Because of the "no-merge" policy, running
147-
`git pull` will create a merge commit, defeating the point of your rebase. Use
148-
`git push -f` instead.
146+
The advice this gives is incorrect! Because of Rust's
147+
["no-merge" policy](#no-merge-policy) the merge commit created by `git pull`
148+
will not be allowed in the final PR, in addition to defeating the point of the
149+
rebase! Use `git push --force-with-lease` instead.
149150

150151
## Advanced Rebasing
151152

152-
Sometimes, you may want to perform a more complicated rebase. There are two
153-
common scenarios that might call for this.
154-
155153
If your branch contains multiple consecutive rewrites of the same code, or if
156-
the rebase conflicts are extremely severe, it is possible that just trying to
157-
reapply the changes you made on top of the updated code will be too much of a
158-
headache. In this case, you can use the interactive rebase feature via
159-
`git rebase -i master` to gain more control over the process. This allows you
160-
to choose to skip commits because they represent changes you no longer need,
161-
edit the commits that you do not skip, or change the order in which they are
162-
applied.
163-
164-
The other common scenario is if you are asked to or want to "squash" multiple
165-
commits into each other. If you PR needs only a minor revision, a single commit
166-
at the end with message "fixup small issue" is usually unhelpful, and it is
167-
easier for everyone if you combine that commit with another that has a more
168-
meaningful commit message. Run `git rebase -i HEAD~2` to edit the last two
169-
commits so you can merge them together. By selecting the `-i` option, you give
170-
yourself the opportunity to edit the rebase, similarly to above. This way you
171-
can request to have the most recent commit squashed into its parent.
154+
the rebase conflicts are extremely severe, you can use
155+
`git rebase --interactive master` to gain more control over the process. This
156+
allows you to choose to skip commits, edit the commits that you do not skip,
157+
change the order in which they are applied, or "squash" them into each other.
158+
159+
Alternatively, you can sacrifice the commit history like this:
160+
161+
```
162+
# squash all the changes into one commit so you only have to worry about conflicts once
163+
git rebase -i $(git merge-base master) # and squash all changes along the way
164+
git rebase master
165+
# fix all merge conflicts
166+
git rebase --continue
167+
```
168+
169+
"Squashing" commits into each other causes them to be merged into a single
170+
commit. Both the upside and downside of this is that it simplifies the history.
171+
On the one hand, you lose track of the steps in which changes were made, but
172+
the history becomes easier to work with.
173+
174+
You also may want to squash just the last few commits together, possibly
175+
because they only represent "fixups" and not real changes. For example,
176+
`git rebase --interactive HEAD~2` will allow you to edit the two commits only.
172177

173178
## No-Merge Policy
174179

0 commit comments

Comments
 (0)