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: docs/chapters/chapter_04.md
+100-1Lines changed: 100 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -158,6 +158,15 @@ Now the project is growing, and some colleagues want to work on the code togethe
158
158
159
159
With Git, however, code collaboration can work very efficiently. This is achieved by branching, merging and pull requests.
160
160
161
+
### Adding collaborators
162
+
163
+
You may have noticed the setting of *public* or *private* during the creation of the GitHub repository. They control whether this repository is visible to everyone. Being public, however, doesn't mean anyone can change the code in your repository.
164
+
165
+
By default, only you, the repository owner, can contribute to the repository (push code). To add collaborators, go to the repository settings, and then "collaborators".
166
+
167
+

168
+
169
+
161
170
### Editing on a new branch
162
171
163
172
As explained in Chapter 3 and 5, the purpose of *branches* is to separate different strands of work.
@@ -190,9 +199,99 @@ Now you can modify the code, without the need to worry about what your colleague
190
199
191
200
When you think you have finished all the codes related to `visualization`, you will want to *merge* this branch back to the base branch (in our case `master`), so that collaborators and users will be able to see and use these codes by default.
192
201
193
-
To do this, we first open a *pull request*.
202
+
To do this, we first open a *pull request* (PR).
194
203
195
204
Go to the repository webpage on GitHub, and click on the "Pull requests" tab. GitHub likely realizes that you are opening the pull request for the most recent branch, `visualization`. Click the green "Compare & pull request" button.
196
205
197
206

198
207
208
+
!!! info Note
209
+
If there is no "Compare & pull request" prompt, click on the "New pull request" green button instead, and then manually choose which branch to merge.
210
+
211
+
Then, GitHub will show what is different between the `visualization` and `master` branches.
212
+
213
+

214
+
215
+
When opening the pull request, remember to include a clear description of what changes are made in the new branch.
216
+
217
+

218
+
219
+
It may seem a little unnecessary if it is only you and your colleague sitting right next to your desk coding this together, but this will become really important when 15 collaborators end up working on the same project, most of whom only checking in once in a while...
220
+
221
+
On the right of the screen, there are several further details you can add to the PR:
222
+
223
+
- Reviewers are the people who will read and check your new code. This is usually different from the author of this PR. The importance of code review is covered in other parts of the CodeRep course.
224
+
- Assignees are the people who are responsible to work on the PR until it's merged into the master.
225
+
- Labels, projects and milestones are all 'tags' to signify the importance of the code in this PR. They are commonly used in larger projects.
226
+
227
+
Let's go ahead and open the PR.
228
+
229
+
### Conflict resolution
230
+
231
+
In this case, your colleague has already finished their part on hypothesis tests. They have merged to `master` before you opened `visualization` PR. This has caused a conflict in the codes, which GitHub detects:
In short, both `master` and `visualization` have changed the same file(s).
239
+
240
+
In detail: When you started working on the master branch, you started from the commit labelled "SPLIT" on `master` branch. Now, you are trying to merge the "CURRENT" commit on `master` with "LAST" commit on `visualization`. Both "CURRENT" and "LAST" include changeds to `script.py`, but independent changes, from "SPLIT". Therefore, Git is not sure how to merge these.
241
+
242
+
Luckily, with Git, this is not scary at all. There are several ways to make this merge, and here we introduce a recommended workflow.
243
+
244
+
> Always try to keep `master` (or `main` or any base branches) clean.
245
+
246
+
On the local computer, do a *pull*. Literally, this is the opposite of *push*, so the local is up-to-date with remote. Now the local repository also contains the work your colleague has done.
247
+
248
+
In GitHub Desktop: 
249
+
250
+
In PyCharm: 
251
+
252
+
Check that you are still on `visualization`. Then *merge*`master` into `visualization`.
253
+
254
+
In GitHub Desktop, there is a quick one-step button to do this: 
255
+
256
+
In PyCharm or other IDEs, go to merge: 
257
+
258
+
!!! info "Which way to merge?"
259
+
The direction of merging can be confusing in the beginning. If you want to merge A into B (B end up containing changes of A), stay on (checkout) B, and perform merge A.
260
+
261
+
Either way, you will hopefully be prompted the same conflict seen on the GitHub. This will automatically tell any IDE you use, that you are trying to resolve a merge conflict. For example in PyCharm,
This will then bring up the Merge Revision window.
270
+
271
+

272
+
273
+
Here, one branch to merge is on the left (e.g. `visualization`), the other to merge on the right (e.g. `master`), and your desired merge results is in the middle. You can pick and choose whether you want to keep each changes on the two branches by clicking the "<<" or ">>" arrows next to the changes.
274
+
275
+
In this case, both branches are adding new codes, so we want to include all changes. Finally click apply.
276
+
277
+
If there are multiple files that have been changed both sides, go through this process for each file. Hopefully, in the end, everything will be resolved.
278
+
279
+
GitHub Desktop shows this good new explicitly:
280
+
281
+

282
+
283
+
Click "contiunue merging", to make a commit after the merge work. Alternatively, in IDEs, simply make a new commit for all the merged files.
284
+
285
+
Now, make a *push* again, and look at the PR page.
286
+
287
+

288
+
289
+
Great, no more conflicts to merge! The new code is now ready to be merged into the base branch - if the reviewer approves. So, suppose your colleague has no objection, click the green "Merge pull request" button and confirm.
290
+
291
+
All the new visualization codes are now in the `master` branch. Meanwhile, the `visualization` branch is no longer needed, so GitHub suggests that you can delete that branch.
292
+
293
+

294
+
295
+
!!! info "Important"
296
+
Why all the convoluted steps? In these steps, we introduced how to keep our working feature branch up-to-date with the new developments on `master`. In general, it is **a very good practice** to frequently pull and merge `master` (or another base branch) into the feature branch, instead of after all the work has finished. This way, it is less likely to see conflicts, and even if there are, they will be smaller ones which do not require a lot of head-scratching to resolve.
0 commit comments