- Ensure that you install the
gitgraphextension
-
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.jsprovides 2 functionsumandminussum- 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-1orcalculator-2) you are supposed to perform the operation.
- Do pay attention to which repository (
-
(
calculator-1) Let's modifysumto 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.jsOR - 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 theminusfunction 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
originand are targeting at the same github url. -
(
calculator-1) Push the changes ontooriginat 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 theaddis done. Let us merge ourmasterbranch 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
gitgraphand 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 theminusfunction. We know that we can rewrite a subtraction in the following way:x - y = x + (-y) -
(
calculator-2) Let us modify theminusfunction so that we are addingxwith-yusing thesumfunction.function minus(x, y) { return sum(x, -y); }
-
(
calculator-2) Save the changes, stage and commit with message "Usesumto dominus". -
(
calculator-2) Head back togitgraphand 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 masterYou 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
pullis a combination offetchandmergegit pull origin master -
(
calculator-2) You should observe a message mentioning a "Conflict". The reason is because there are now 2 possibleminusimplementation and git is not able to resolve it automatically. -
(
calculator-2) let's open thecalculator.jsfile 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 ofminusand you are to manually fix it by modifying the file. Let's modify the file such that we use thex - yimplementation.- 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
gitgraphyou 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 branchaheadorbehindorigin's master branch? (Ans: ahead) - If I make another commit in
calculator-1before 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 moveHEADback 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