repl.it
linkeLearning instructions
As this week is done in eLearning mode, please follow the instructions below in the given sequence.
This week, we build on our knowledge of git branching and GitHub PRs to learn how to use them when the code is updated in parallel multiple branches. Do the following activity (in-lieu of the usual in-lecture activity) to learn the relevant techniques which you will need for the iP this week. The activity can earn you lecture participation points too. The deadline is next Tuesday midnight.
Activity - please do it if you are not a Git expert
This activity teaches you how to merge parallel git branches,
To keep things simple, we use a simple repo that uses two files food.txt
and stationary.txt
to keep track of items in the shop. There are 2 shop employees (John and Amy) and two vendors (Ravi and Musa) who have updated the files, but each person using their own branch. Your job is to merge the four branches so that the files reflect the final tally of goods in the shop.
First, let us set up the repo and checkout the branches.
Step 1.a Fork the https://github.com/nus-cs2113-AY1920S2/shop-inventory to your Github account.
Step 1.b Clone it to your computer. No need to set it up in your IDE because it is not a Java project.
Step 1.c Note how the remote repo has 4 branches in addition to the master
branch. If you can't see the branches, make sure you have ticked the Show Remote Branches
box (shown in the top center of the screenshot below).
Now you are in the master
branch. Open the food.txt
file to check its contents.
Step 2 Checkout the employee-john
branch by double-clicking it.
See an animation of this step
Now, you should be in the employee-john
branch. This also means now you have a local branch named employee-john
which has the exact content of the remote branch origin/employee-john
and it is i.e., git knows that this branch is meant to be a copy of the corresponding remote branch'tracking' the corresponding remote branch. This is how the revision graph should look like now:
master
branch. The revision graph should look this this now:Next, let us merge the branch created by John.
employee-john
branch to the master
branch: Right-click on the employee-john
branch label and choose merge
. Remember to request a merge commit even if a fast-forward merge is possible.See an animation of this step
master
branch).Reset current branch to this commit
and choose the Hard
option in the next dialog.See an animation of this step
employee-john
again by re-doing the Step 4 again. The revision tree should look like this again:Now that you know how to merge a branch, and undo a merge, let's try to merge Amy's branch. We are going to encounter a dreaded merge conflict this time around.
employee-amy
branch to the master
branch, similar to the previous step. Note how git reports a merge conflict
halfway into the merge, with a dialog like this:When you open the conflicted file food.txt
, the content will be like this:
banana
<<<<<<< HEAD
carrot
cheese
=======
beans
>>>>>>> employee-amy
dates
egg
fish
grapes
Note how git has marked the conflicting changes done by the two branches using markers <<<<<<<
, =======
, and >>>>>>>
We can modify that part in any way we like. For example, we can edit the file to make the content look like this.
banana
cheese
beans
dates
egg
fish
grapes
When we are happy with the content of the conflicted file(s), we can simply stage the changes we did and complete the merge by committing the changes (similar to how you do a normal commit).
See an animation of this step
Good. Now we can handle branch merging in our local repo even if there are merge conflicts. Next, let us find out how to do the same in a remote repo, using GitHub PRs.
But first, let's learn how to create PRs using branches, within the same remote repo.
Creating PRs between branches in the same repo is a way for different team members to propose changes to the code and get the changes reviewed by others.
Step 7 Push the master
branch to the fork, to upload the new merge commits to the fork.
Step 8 Create a PR within your own fork, from the vendor-ravi
branch to the master
branch:
compare
field to the vendor-ravi
branch.Step 9 Create another PR for the vendor-musa
branch. As you did in the previous step, create another PR to the master
branch from the vendor-musa
branch.
Step 10 Update the PR of vendor-musa
with reviews and new commits, to simulate PRs going through a review process.
vendor-musa
branch, add a line at the end of the stationary.txt
and commit it. This is to simulate a developer updating a PR based on review comments.vendor-musa
to your fork to upload the new commit to the fork.The next step is to learn how to merge PRs, thereby merging the corresponding branch.
vendor-musa
branch).
So far so good. Next, let's learn how to merge PRs when there are merge conflicts.
Step 12 Observe the other PR is now showing conflicts: If you go to the vendor-ravi
PR, it will show something like this at the bottom, indicating that the code in that branch has a conflict with the master
branch (caused by the other PR we merged just now).
Step 13 Resolve the conflict. Note that GitHub gives a button that provides a way to resolve conflicts on the Web UI itself. However, that method is more suited for simple conflicts. In this activity, let's use the method that works for even more complicated conflicts.
master
branch on the fork only, pull the master
branch from your fork to your repo to get the latest master
branch onto your Computer.master
branch to the vendor-ravi
branch to sync the vendor-ravi
branch with the latest master
. This will result in a conflict due to both vendors editing the same location of the stationary.txt
.vendor-ravi
branch to the fork.When you open the conflicted file food.txt
, the content will be like this:
banana
<<<<<<< HEAD
carrot
cheese
=======
beans
>>>>>>> employee-amy
dates
egg
fish
grapes
Note how git has marked the conflicting changes done by the two branches using markers <<<<<<<
, =======
, and >>>>>>>
We can modify that part in any way we like. For example, we can edit the file to make the content look like this.
banana
cheese
beans
dates
egg
fish
grapes
When we are happy with the content of the conflicted file(s), we can simply stage the changes we did and complete the merge by committing the changes (similar to how you do a normal commit).
See an animation of this step
vendor-ravi
PR as you merged the previous PR in Step 11.Step 11 (for reference)
vendor-musa
branch).
master
by pulling the master
branch from your fork. After that, your revision graph should look like this:Nice! Now you know how to merge work done in parallel branches.
Read the relevant topics scheduled for this week.
Although the following video appears in an optional section, we recommend you to watch it before the next tutorial as you will be asked to demo debugging in an IDE during the tutorial. If you use a different IDE, learn how to debug in it from other resources.
[W6.1] Java: Generics
[W6.2] Java: Collections
[W6.3] Java: File Access
[W6.4] Java: JAR Files
[W6.5] Java: Varargs : OPTIONAL
[W6.6] Java: streams : OPTIONAL
[W6.7] Requirements: Intro
[W6.8] Requirements: Gathering
[W6.9] Requirements: Specifying [continued from last week]
Full ToC
[W6.8a] Requirements → Gathering Requirements → Brainstorming
[W6.8b] Requirements → Gathering Requirements → Product surveys
[W6.8c] Requirements → Gathering Requirements → Observation
[W6.8d] Requirements → Gathering Requirements → User surveys
[W6.8e] Requirements → Gathering Requirements → Interviews
[W6.8f] Requirements → Gathering Requirements → Focus groups
[W6.8g] Requirements → Gathering Requirements → Prototyping
Prose
User Stories [Repeated from last week]
[W6.9b] Requirements → Specifying Requirements → User Stories → Introduction
[W6.9c] Requirements → Specifying Requirements → User Stories → Details
[W6.9d] Requirements → Specifying Requirements → User Stories → Usage
Feature Lists [Repeated from last week]
Use Cases
Glossary
Supplementary Requirements
[W6.12a] Implementation → Code Quality → Error-Prone Practices → Introduction
[W6.12b] Implementation → Code Quality → Error-Prone Practices → Basic → Use the default branch
[W6.12c] Implementation → Code Quality → Error-Prone Practices → Basic → Don't recycle variables or parameters
[W6.12d] Implementation → Code Quality → Error-Prone Practices → Basic → Avoid empty catch blocks
[W6.12e] Implementation → Code Quality → Error-Prone Practices → Basic → Delete dead code
[W6.12f] Implementation → Code Quality → Error-Prone Practices → Intermediate → Minimize scope of variables
[W6.12g] Implementation → Code Quality → Error-Prone Practices → Intermediate → Minimize code duplication
Admin:
iP:
Level-6
, Level-7
A-Jar
tP: