- Ensure that you install the
gitgraph
extension
-
Fork this repository to your own Github account
-
Clone the forked repository twice.
$ git clone https://github.com/<YOUR_GITHUB_NAME>/calculator-git-pracitce calculator-1 $ git clone https://github.com/<YOUR_GITHUB_NAME>/calculator-git-pracitce calculator-2
-
Open the 2 local repository with
vscode
-
You can see that
calculator.js
provides 2 functionsum
andminus
sum
- Takes in 2 number and sum them together (e.g.sum(2,3) = 5
)minus
- Takes in 2 number, x and y, and minus y from x (e.g.minus(5,3) = 2
)
-
If you observe their implementation, it is currently unnecessarily complicated, let us modify it to simplify the implementation.
- Do pay attention to which repository (
calculator-1
orcalculator-2
) you are supposed to perform the operation.
- Do pay attention to which repository (
-
(
calculator-1
) Let's modifysum
to make use of the+
operator:function sum(x, y) { return x + y; }
-
(
calculator-1
) Stage this change, and add a new commit "Makesum(x, y)
use + operator"- To stage:
- Either enter
git add calculator.js
OR - Use VSCode's built in source control
- Either enter
- To commit:
- Either enter
git commit -m "Make...."
OR - Use VSCode's built in source control
- Either enter
- To stage:
-
(
calculator-1
) Let's also modify theminus
function to make use of the-
operator.function minus(x, y) { return x - y; }
-
(
calculator-1
) Similarly, stage the changes and make a commit. -
(
calculator-1
) Open up gitgraph, and you should observe that you have 3 commits in total- initial
- Make add ...
- Make minus ...
-
(
calculator-1
) Type the following to view the remotes configured on your local repositorygit remote -v
You should observe that there are 2 remote, 1 for push and 1 for pull, but both are named
origin
and are targeting at the same github url. -
(
calculator-1
) Push the changes ontoorigin
at the master branch.git push origin master
-
(
calculator-2
) Move tocalculator-2
-
(
calculator-2
) Open up gitgraph, you should observe that there are 3 commits- Initial (master)
- Make add....
- Make minus.... (origin/master)
-
(
calculator-2
) Suppose we like the way the theadd
is done. Let us merge ourmaster
branch with theMake add....
commit.git merge <COMMIT_HASH>
Find out what is the <COMMIT_HASH> of the
Make add....
commit It is a combination of alphabets and numbers (e.g. jd7s6a9nd9s8...)Alternatively, you can right click on the particular commit in
gitgraph
and press merge. -
(
calculator-2
) You should observe that there are still 3 commits- Initial
- Make add.... (master)
- Make minus.... (origin/master)
-
(
calculator-2
) Recall in the video that it is mentioned that merging will create a new commit, what you observed is a fast-forward merge, you can read more about it here. -
(
calculator-2
) Let us now modify theminus
function. We know that we can rewrite a subtraction in the following way:x - y = x + (-y)
-
(
calculator-2
) Let us modify theminus
function so that we are addingx
with-y
using thesum
function.function minus(x, y) { return sum(x, -y); }
-
(
calculator-2
) Save the changes, stage and commit with message "Usesum
to dominus
". -
(
calculator-2
) Head back togitgraph
and you should observe the following commitsinitial - Make add... - Use sum to do minus (master) \ - Make minus... (origin/master)
-
(
calculator-2
) Try pushing the changes to origin now.git push origin master
You should receive an error mentioning
"not up-to-date"
-
(
calculator-2
) This tells us that we need to pull the changes from origin first. (Error will occur, it's expected)Recall that
pull
is a combination offetch
andmerge
git pull origin master
-
(
calculator-2
) You should observe a message mentioning a "Conflict". The reason is because there are now 2 possibleminus
implementation and git is not able to resolve it automatically. -
(
calculator-2
) let's open thecalculator.js
file and you should observe something of the following:function minus(x, y) { >>>>>>>> HEAD return sum(x, -y); ======== return x - y; >>>>>>>> origin/master }
-
(
calculator-2
) This shows you the 2 possible implementation ofminus
and you are to manually fix it by modifying the file. Let's modify the file such that we use thex - y
implementation.- Remove the lines with
>>>>>>
and========
- Remove the line with
sum(x, -y)
- Remove the lines with
-
(
calculator-2
) Save the file, stage the change and commit (without -m). -
(
calculator-2
) Return back togitgraph
, you should observe the following commits:initial - Make add... - Use sum to do minus (master) - Merge ... \ / - Make minus... (origin/master) ------------
That is, a new merge commit has been created.
-
(
calculator-2
) Now push the changes back to the remote. -
(
calculator-1
) Head back tocalculator-1
-
Open up
gitgraph
you should now observe a graph with all the relevant commits, if not, do a fetch. -
Check your understanding & further exploration:
- Is
calculator-1
's master branchahead
orbehind
origin's master branch? (Ans: ahead) - If I make another commit in
calculator-1
before pulling, how will the graph look like? Ans:initial - Make add... ------ Make minus... - new Commit (master) \ \ - Use sum to do minus (origin/master) - Merge ...
- The video mentioned that "commits are like save points in a game", How do we load the version on
initial
? Or, how do we moveHEAD
back to initial? (Ans:git checkout <INITIAL_COMMIT_HASH>
) - The video mentioned that "we can add labels to commits (e.g. master)", how do we add a new label, "hello", to our current commit? (Ans: git branch hello)
- You should be able to see another label on your current commit on
gitgraph
.
- You should be able to see another label on your current commit on
- Is