40
40
41
41
``` console
42
42
$ git remote -v
43
- origin [email protected] :jni/r-string-calc (fetch)
44
- origin [email protected] :jni/r-string-calc (push)
43
+ origin [email protected] :jni/pycalc (fetch)
44
+ origin [email protected] :jni/pycalc (push)
45
45
```
46
46
47
47
That tells both the name of the remote, * and its location* . "origin" is
@@ -60,25 +60,48 @@ Delta compression using up to 4 threads.
60
60
Compressing objects: 100% (36/36), done.
61
61
Writing objects: 100% (36/36), 3.33 KiB | 0 bytes/s, done.
62
62
Total 36 (delta 25), reused 0 (delta 0)
63
- To [email protected] :jni/r-string-calc
63
+
64
64
8ab0457..8de6fb7 master -> master
65
65
```
66
66
67
67
Read the above as "push to "origin" my branch "master" onto its branch
68
- "master".
68
+ "master". Branches are managed locally for each repository, so the branch names
69
+ don't actually have to match. That is, we could easily have written:
69
70
70
- After this, you'll be able to refresh your repo page on GitHub and
71
+ ``` console
72
+ $ git push origin master:other-branch-name
73
+ ```
74
+
75
+ and then the contents of our branch ` master ` locally would be mirrored in the
76
+ remote branch ` other-branch-name ` on ` origin ` . In order to tell git to keep
77
+ track of matching branch names, use the option ` --set-upstream ` :
78
+
79
+ ``` console
80
+ $ git push origin --set-upstream master
81
+ ```
82
+
83
+ This tells git: "push ` master ` onto ` origin ` 's ` master ` , and note that they are
84
+ mirrors of each other." This means that later, we only need to do:
85
+
86
+ ``` console
87
+ $ git push origin
88
+ ```
89
+
90
+ And git will know that ` master ` goes onto ` origin ` 's ` master ` .
91
+
92
+ After this, you'll be able to refresh your page on GitHub and
71
93
browse your code's history.
72
94
73
95
## Exercise 4: GitHub pull requests
74
96
75
97
For this exercise you will have to pair up with your neighbour, which
76
98
we will name Alice. (And your name is Bob, in keeping with the computer
77
- science literature.)
99
+ science literature.) Decide now who will be Bob and who will be Alice in the
100
+ pair.
78
101
79
- Delete your "r-string-calc " repository on GitHub (this is done under "Settings"
80
- in the right-hand menu). You've realised that Alice has her own version and
81
- that you can both save effort by collaborating on this project.
102
+ As Bob, you should delete your "pycalc " repository on GitHub (this is done
103
+ under "Settings" in the right-hand menu). You've realised that Alice has her
104
+ own version and that you can both save effort by collaborating on this project.
82
105
83
106
You've been wanting to do some arithmetic on some data, but the first
84
107
number you need to add is often a decimal number. In those cases, the
@@ -88,39 +111,41 @@ You want to modify it so that the first number is allowed to be a decimal
88
111
number.
89
112
90
113
Navigate to Alice's repository on GitHub
91
- (https://github.com/[Alice's username] /r-string-calc ), and click the "Fork"
114
+ (https://github.com/[Alice's username] /pycalc ), and click the "Fork"
92
115
button.
93
116
This will create a copy of Alice's repo on your GitHub account, which
94
117
you can then clone on your machine as before. But note that you need to
95
- delete your existing repository! Instructions below (some of the directories
96
- and obviously the "bob" username needs to be changed to yours!):
118
+ delete your existing work, or git will complain! Instructions below (some of
119
+ the directories and obviously the "bob" username needs to be changed to
120
+ yours!):
97
121
98
122
``` console
99
123
$ pwd
100
- /Users/bob/projects/r-string-calc
124
+ /Users/bob/projects/pycalc
101
125
$ cd ..
102
- $ rm -rf r-string-calc
103
- $
git clone [email protected] :bob/r-string-calc.git
104
- $ cd r-string-calc
105
- $ git checkout -b decimals
126
+ $ rm -rf pycalc
127
+ $
git clone [email protected] :alice/pycalc
128
+ $ cd pycalc
129
+ $ git switch --create decimals
106
130
```
107
131
108
- Edit the ` strcalc.R ` file so that ` num0 ` is converted with ` as.numeric `
109
- instead of ` as.integer ` .
132
+ Edit the ` calc.py ` file so that ` num0 ` is converted with ` float `
133
+ instead of ` int ` . (Leave ` num1 ` unchanged for now.)
110
134
111
135
Now commit those changes and push them * to a new branch on GitHub* . If you use
112
136
the ` --set-upstream ` flag, you tell git to create a branch with the same
113
137
name that "tracks" the current branch. This makes future pushes easier.
114
138
115
139
``` console
116
- $ git add strcalc.R
140
+ $ git add calc.py
117
141
$ git commit -m " Allow num0 to be any decimal number"
118
142
$ git push origin --set-upstream decimals
119
143
```
120
144
121
145
Go to the GitHub page for the project. You should see a new button
122
146
showing that you've recently updated a branch and prompting you to
123
- * initiate a pull request* .
147
+ * initiate a pull request* . (You can also copy the "new pull request" address
148
+ from git's message when you push.)
124
149
125
150
Here's how this works: you don't know Alice. You probably have never met
126
151
her. So it's natural that you can't just push random stuff willy-nilly to her
@@ -131,7 +156,7 @@ changes, you ask *her* to *pull* from your own history.
131
156
The PR will tell Alice that you've made some
132
157
changes to the code and you would like her to incorporate them into
133
158
her project. Notice that you did this * without needing any special
134
- access from Alice!* This is the magic of GitHub.
159
+ access from Alice!* This is the magic of GitHub and open source .
135
160
136
161
Check out the impact that GitHub has had on a few open source Python projects:
137
162
@@ -153,30 +178,28 @@ title and message here is very important!
153
178
> When in Rome, do as the Romans do. Look at their existing codebase
154
179
> and try to follow their example. (This is not to say that you can't
155
180
> improve on it; but make sure your documentation and testing * at least*
156
- > least meets their standards.)
157
-
181
+ > meets their standards.
158
182
159
183
160
184
Alice should get an email notification that there is a pull request to
161
185
her project. Clicking on it, she will be taken to the web form for the
162
- PR, where she can examine the changes that Bob has made (the "Files changed"
163
- tab).
186
+ PR, where she can examine the changes that Bob has made (the "Files" tab).
164
187
165
188
Alice will note that this great change would be made much more useful if it
166
- also used ` as.numeric ` for ` num1 ` ! She comments on the
189
+ also used ` float ` for ` num1 ` ! She comments on the
167
190
PR page: "This is a great addition, thanks! Could you please do the same for
168
191
num1?"
169
192
170
193
On his machine, Bob makes the requested change, commits, and pushes his changes:
171
194
172
195
``` console
173
- $ # ... edit strcalc.R ...
174
- $ git add strcalc.R
175
- $ git commit -m " Use as.numeric for num1's conversion also"
196
+ $ # ... edit calc.py ...
197
+ $ git add calc.py
198
+ $ git commit -m " Use float for num1's conversion also"
176
199
$ git push # no need to specify repo or branch anymore, having `set-upstream`
177
200
```
178
201
179
- If both Bob and Alice go back to the PR page, they will see that the PR has
202
+ If either Bob or Alice go back to the PR page, they will see that the PR has
180
203
been automagically updated with Bob's new changes! (Though they may need to
181
204
refresh the page.)
182
205
@@ -194,28 +217,29 @@ For this, he needs to add it to his list of remotes (remember remotes?):
194
217
195
218
``` console
196
219
$ git remote -v
197
- origin [email protected] :bob/r-string-calc (fetch)
198
- origin [email protected] :bob/r-string-calc (push)
199
- $
git remote add upstream [email protected] :alice/r-string-calc
220
+ origin [email protected] :bob/pycalc (fetch)
221
+ origin [email protected] :bob/pycalc (push)
222
+ $
git remote add upstream [email protected] :alice/pycalc
200
223
$ git remote -v
201
- origin [email protected] :bob/r-string-calc (fetch)
202
- origin [email protected] :bob/r-string-calc (push)
203
- upstream [email protected] :alice/r-string-calc (fetch)
204
- upstream [email protected] :alice/r-string-calc (push)
205
- $ git checkout master
224
+ origin [email protected] :bob/pycalc (fetch)
225
+ origin [email protected] :bob/pycalc (push)
226
+ upstream [email protected] :alice/pycalc (fetch)
227
+ upstream [email protected] :alice/pycalc (push)
228
+ $ git switch master
206
229
$ git pull upstream master # get upstream's master branch, and merge
230
+ $ git push origin master
207
231
```
208
232
209
233
Bob can now inspect his history log and see that both his changes and Alice's
210
- merge are there. Use GitX for this or the ` lsd ` alias we learned earlier, or
211
- a simple git-log will also do.
234
+ merge are there. Use GitKraken/ GitX/GitTower/other GUI for this, or the ` lsd `
235
+ alias we learned earlier, or a simple git-log will also do.
212
236
213
237
## Bonus exercise 1: self-PRs and code review
214
238
215
239
Do the reverse approach, which is a bit different. Alice has gained a
216
240
collaborator in Bob. Even though she still maintains control of the project
217
- repository, she wants to enlist his help. She sprouts a branch, adds a line or
218
- two (for example, she might want to add a test function, ` test.compute ` , that
241
+ repository, she wants to enlist his help. She creates a branch, adds a line or
242
+ two (for example, she might want to add a test function, ` test_compute ` , that
219
243
runs ` compute ` for a few known values and makes sure the results match up), then
220
244
* creates a PR against her own repository.* She then asks Bob to review her
221
245
change by mentioning his username (e.g. ` @bob ` , as in Twitter) in a comment on
@@ -243,7 +267,7 @@ The common solution is to *rebase*, that is, to replay the changes on Bob's
243
267
branch on top of the latest ` master ` from Alice's repository.
244
268
245
269
``` console
246
- $ git checkout master
270
+ $ git switch master
247
271
$ git pull upstream master
248
272
$ git rebase master test-2
249
273
```
0 commit comments