|
| 1 | +# 4 Learn the workflow from case studies |
| 2 | + |
| 3 | +We have now installed Git on our local machine, familiarised ourselves with the interfaces, and tested out a local repository. However, to realize more advantages of version control (e.g. collaboration, improving reproducibility), we will need to take it online. |
| 4 | + |
| 5 | +This chapter will guide you through exactly how this is done, step by step, from simpler use cases to more advanced ones. |
| 6 | + |
| 7 | +As discussed previously, there are a wide variety of hosting services. Here, only as an example, we use the well-known GitHub platform as our remote server. |
| 8 | + |
| 9 | +## 4.1 Starting a new single person repository |
| 10 | + |
| 11 | +In this example, we will make a fresh repository on GitHub, make some changes from the browser, and clone a local copy so that we can work on it offline. |
| 12 | + |
| 13 | +!!! info Note |
| 14 | + Chapter 4.2 describes the variation if you already have a local repository. |
| 15 | + |
| 16 | +### Create a repo on GitHub |
| 17 | + |
| 18 | +First, log in to GitHub. Click the "+" and then "New repository" on the top right corner. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +Then, give the new repo a meaningful name (we will call it `learn-git`), and why not add a README file. Click the big green button at the bottom. Simple as that, we made a repository on GitHub. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +### Made changes from the browser |
| 27 | + |
| 28 | +A very convenient feature of the online platforms like GitHub is that they allow us to make certain changes directly from the browser. For example, click into the `README.md` file, and then click on the pencil button on the right hand side: |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +Now we are in a text editor, and can make any changes we want to this file. Once finished, click the green "Commit changes" to save. |
| 33 | + |
| 34 | +!!! info Note |
| 35 | + Why the "Commit changes", but not a simple "Save"? Remember, Git keeps a history for everything. By editing on from the browser, we have essentially done "saving the file", "stage the file" and "commit changes" with one step! |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +Here you can input a commit message to describe what changes you have made. Then click the green "Commit changes" button again, and all done. |
| 40 | + |
| 41 | +### Clone the repo to local computer |
| 42 | + |
| 43 | +For day to day coding and scripting, we probably still want to work locally on our computers with IDEs or more sophisticated editors. To do this in our new repository, we first need to make a local clone on our computer. This is very easy to do. |
| 44 | + |
| 45 | +Go to the homepage of our repo on Github. Click the green "< > Code" button at the top right corner above the file list. Click on the "SSH" tab. |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +!!! info Note |
| 50 | + SSH is more secure than HTTPS. If you have set up your SSH keys successfully in the previous chapter, always try to use the SSH links. |
| 51 | + |
| 52 | +Now there are several options: |
| 53 | + |
| 54 | +- If you are working with GitHub Desktop, simply click on the link "Open with GitHub Desktop". You can choose where you want to keep the local copy. |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +- Otherwise, copy the line of link (with the button on the right). Go to your preferred IDE and clone the repo there. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +- If you work with commandlines, go to the parent folder where you want the new repo to be copied into, and then use the `git clone` command (replacing `<some-parent-folder>` and `<your-github-id>`): |
| 63 | + |
| 64 | +```bash |
| 65 | +cd <some-parent-folder> |
| 66 | +git clone git@github.com:<your-github-id>/learn-git.git |
| 67 | +``` |
| 68 | + |
| 69 | +Look inside the `learn-git` folder. You have the new repo cloned onto your computer! |
| 70 | + |
| 71 | +### Make changes locally and keep the remote updated |
| 72 | + |
| 73 | +As you make new changes in this folder, you can make local commits just as you did in #3.6. |
| 74 | + |
| 75 | +!!! example "Exercise" |
| 76 | + If you want to make an edit to the `README.md` file, what steps do you need to take to make the commit? |
| 77 | + |
| 78 | + ??? success "Solution" |
| 79 | + 1. Edit with an editor of your choice. |
| 80 | +  |
| 81 | + 2. Stage `README.md` file. |
| 82 | +  |
| 83 | + 3. Make commit. |
| 84 | +  |
| 85 | + |
| 86 | +As long as you are on a single branch, the only extra step to take to update the remote is *pushing*. This sends all the local changes to the GitHub remote server. |
| 87 | + |
| 88 | +In GitHub Desktop, this is done by simply clicking the `Push` button on the top banner: |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +Have a look on the GitHub website. It's updated with the local changes! |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +## 4.2 Setting up a new online repository for an existing local repository |
| 97 | + |
| 98 | +Sometimes, we may have already written some code and created a local repository (like what we did in #3.6). Now we want to set up a remote repository and send our code to it, so that our local work is backed up online, and later be shared. |
| 99 | + |
| 100 | +Here we use an example of `learn-git1` local repository with 2 files already commited. |
| 101 | + |
| 102 | +### Publish with GitHub Desktop |
| 103 | + |
| 104 | +If you use GitHub Desktop, the process is very easy. On the top banner, click the `Publish repository` button. |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +Then enter descriptions and click publish! All done. |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +### Without GitHub Desktop |
| 113 | + |
| 114 | +The whole process is similar to Chapter 4.1, except in some small details. |
| 115 | + |
| 116 | +### Create a repo on GitHub |
| 117 | + |
| 118 | +First, log in to GitHub. Click the "+" and then "New repository" on the top right corner. |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +Then, give the new repo a meaningful name (probably sensible to be the same as your local folder name). To make life simple, do NOT initialize or add any files. |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +!!! example "Exercise" |
| 127 | + What will happen if we initialized with, e.g. a README file? |
| 128 | + |
| 129 | + ??? success "Solution" |
| 130 | + The remote repo on Github will have the README file, while our local repo does not. On the other hand, remember that our local repo has some existing codes that the remote does not have. Therefore, as soon as we try to link the two together, there will be a conflict. |
| 131 | + |
| 132 | +Click the big green button at the bottom to create the repo. |
| 133 | + |
| 134 | +### Connect to the local repo |
| 135 | + |
| 136 | +Since the new online repository is completely empty, GitHub then shows you exactly how to connect to the local repository. |
| 137 | + |
| 138 | +With your IDE, go to the version control tab and look for `Manage remote`. |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +Click `Add remote`, and paste in the URL to the GitHub repository page. |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +Now, do a *push*, which will update all the existing local commits to the remote. |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +That's all done! Have a look on the GitHub website. It's updated with the local changes! |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +## 4.3 Making branches and collaborating |
| 156 | + |
| 157 | +Now the project is growing, and some colleagues want to work on the code together. Without Git, this would be a slow process, as only one of you can work on the code at a time. Otherwise, it would be difficult to combine the changes. |
| 158 | + |
| 159 | +With Git, however, code collaboration can work very efficiently. This is achieved by branching, merging and pull requests. |
| 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 | + |
| 170 | +### Editing on a new branch |
| 171 | + |
| 172 | +As explained in Chapter 3 and 5, the purpose of *branches* is to separate different strands of work. |
| 173 | + |
| 174 | +Here for example, while your colleague will write some hypothesis tests on the data, you would like to make more plots to visualize data. So, before coding, you create and checkout a branch `visualization`. |
| 175 | + |
| 176 | +> *Checkout* simply means making this the active branch, so that the following edits will be made on this branch. |
| 177 | +
|
| 178 | +In GitHub Desktop, click the branch box on the top banner, and click new branch. |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | +Enter the new branch name and create. |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | +After switching, the active branch is now `visualization` which can be seen on the top banner. |
| 187 | + |
| 188 | +Alternatively, in PyCharm, create the new branch from the Git menu: |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | +To verify the active branch, look for the Git symbol at the bottom of the screen. Most IDEs have a similar display. |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +Now you can modify the code, without the need to worry about what your colleague is doing. After some hard work in coding, you can stage, commit and push as usual. Everything is saved on the new `visualization` branch. |
| 197 | + |
| 198 | +### Merging and pull requests |
| 199 | + |
| 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. |
| 201 | + |
| 202 | +To do this, we first open a *pull request* (PR). |
| 203 | + |
| 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. |
| 205 | + |
| 206 | + |
| 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: |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | +!!! info "Note" |
| 236 | + What exactly is causing this conflict? |
| 237 | +  |
| 238 | + 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, |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | +Click resolve, and then click merge. |
| 266 | + |
| 267 | + |
| 268 | + |
| 269 | +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. |
| 297 | + |
0 commit comments