1
- # Using git
1
+ # Using Git
2
2
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
4
4
contribute, you'll need some familiarity with its features so that your changes
5
5
can be incorporated into the compiler.
6
6
7
- [ git ] : https://git-scm.com
7
+ [ Git ] : https://git-scm.com
8
8
9
9
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,
11
11
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
13
13
started sections of [ this tutorial from Atlassian] [ atlassian-git ] . GitHub also
14
14
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 ] .
16
16
17
- [ book from git ] : https://git-scm.com/book/en/v2/
17
+ [ book from Git ] : https://git-scm.com/book/en/v2/
18
18
[ atlassian-git ] : https://www.atlassian.com/git/tutorials/what-is-version-control
19
19
[ documentation ] : https://docs.github.com/en/github/getting-started-with-github/set-up-git
20
20
[ guides ] : https://guides.github.com/introduction/git-handbook/
21
21
22
22
## Prequisites
23
23
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
25
25
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
27
27
generally do the same things.
28
28
29
29
[ 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
33
33
rust-lang/rust repo via
34
34
35
35
``` 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
37
37
```
38
38
39
39
if you're using HTTPS, or
40
40
41
41
``` sh
42
- git remote add
rust [email protected] :rust-lang/rust.git
42
+ git remote add
upstream [email protected] :rust-lang/rust.git
43
43
```
44
44
45
45
if you're using SSH.
@@ -51,7 +51,7 @@ and PRs:
51
51
52
52
1 . Ensure that you're making your changes on top of master:
53
53
` 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 ` .
55
55
3 . Make a new branch for your change: ` git checkout -b issue-12345-fix ` .
56
56
4 . Make some changes to the repo and test them.
57
57
5 . Stage your changes via ` git add src/changed/file.rs src/another/change.rs `
@@ -60,7 +60,7 @@ and PRs:
60
60
unintentionally commit changes that should not be committed, such as submodule
61
61
updates. You can use ` git status ` to check if there are any files you forgot
62
62
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 ` .
64
64
7 . [ Open a PR] [ ghpullrequest ] from your fork to rust-lang/rust's master branch.
65
65
66
66
[ 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.
82
82
83
83
When this happens, you need to resolve the conflicts before your changes can be
84
84
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
86
86
update it with the most recent changes.
87
87
88
88
### Rebasing
89
89
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
91
91
changes and execute ` git rebase master ` .
92
92
93
93
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
95
95
pretend that the changes you made to the old version of master were instead
96
96
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
98
98
reapply the changes fails because your changes conflicted with other changes
99
99
that have been made. You can tell that this happened because you'll see
100
100
lines in the output that look like
@@ -113,24 +113,24 @@ Your code
113
113
>>>>>>> 8fbf656... Commit fixes 12345
114
114
```
115
115
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
117
117
rebase. The section between ` <<<<<<< HEAD ` and ` ======= ` has the code from
118
118
master, while the other side has your version of the code. You'll need to
119
119
decide how to deal with the conflict. You may want to keep your changes,
120
120
keep the changes on master, or combine the two.
121
121
122
- Generally, resovling the conflict consists of two steps: First, fix the
122
+ Generally, resolving the conflict consists of two steps: First, fix the
123
123
particular conflict. Edit the file to make the changes you want and remove the
124
124
` <<<<<<< ` , ` ======= ` 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.
128
128
129
129
Once you're all done fixing the conflicts, you need to stage the files that had
130
130
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.
132
132
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 ` .
134
134
135
135
Note that ` git push ` will not work properly and say something like this:
136
136
@@ -143,32 +143,37 @@ hint: 'git pull ...') before pushing again.
143
143
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
144
144
```
145
145
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.
149
150
150
151
## Advanced Rebasing
151
152
152
- Sometimes, you may want to perform a more complicated rebase. There are two
153
- common scenarios that might call for this.
154
-
155
153
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.
172
177
173
178
## No-Merge Policy
174
179
0 commit comments