MNNIT Computer Club

This repository contains the codes, support links and other relevant materials for every class under Computer Club, MNNIT Allahabad.

View on GitHub

Git and Github Class - II

March 7th, 2019

Git diff

git diff is a multi-use git command that when executed runs a diff function on git data sources. These data sources can be commits, branches, files and more. The git diff command is often used along with git status and git log to analyze the current state of a git repo.

Reading git diff output

diff --git a/file1 b/file1
index aba2e68..e024a2a 100644
--- a/file1
+++ b/file1
@@ -2,11 +2,8 @@
 #include<stdlib.h> 
 #include<stdio.h> 
   
-// Merges two subarrays of arr[]. 
-// First subarray is arr[l..m] 
-// Second subarray is arr[m+1..r] 
-void merge(int arr[], int l, int m, int r) 
-{ 
+
+void merge(int arr[], int l, int m, int r){ 
     int i, j, k; 
     int n1 = m - l + 1; 
     int n2 =  r - m; 
@@ -24,15 +21,12 @@ void merge(int arr[], int l, int m, int r)
     i = 0; // Initial index of first subarray 
     j = 0; // Initial index of second subarray 
     k = l; // Initial index of merged subarray 
-    while (i < n1 && j < n2) 
-    { 
-        if (L[i] <= R[j]) 
-        { 
+    while (i < n1 && j < n2){ 
+        if (L[i] <= R[j]){ 
             arr[k] = L[i]; 
             i++; 
         } 
-        else
-        { 
+        else{ 
             arr[k] = R[j]; 
             j++; 
         } 
   

Above shows the difference between two versions of c++ code.
You can see multiple differences denoted by hunks.
A hunk example: @@ -2,11 +2,8 @@ can be broken down into 3 main parts:-
-/+ :: removal and addition respectively. first number :: start line. second number :: number of lines after the start line (including it). For a detailed discussion, refer this article or this article

Branching

In simple terms, a branch in git is a unique series of code changes with a unique name. Each repo can have one or more branches.
A branch represents an independent line of development. You can think of them as a way to request a brand new working directory, staging area, and project history.

In a little advance yet relatively simple term they are labels to your commits.

Branches

For a detailed discussion, refer this

Merge and Merge Conflicts

The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.
Git merge will combine multiple sequences of logically unrelated commits into one unified history. In the most frequent use cases, git merge is used to combine two branches.
Remember merge is also a commit
Since merge is a commit with n parents (generally 2) (here parent refer to the previous branches) it stores info about all the commits

Not Merged

Merge Conflicts

<<<<<<< HEAD
this is some content to mess with
content to append
(this section is the code of your current branch, ie code you have added)
=======
totally different content to merge later
(this section is the changed code)
>>>>>>> new_branch_to_merge_later
same code in both files
(this section is the unchanged code present in both the files)

Remotes

git remote add origin https://github.com/<your-username>/<name-of-the-repo-on-GitHub>

Additionally you also need to specify which exact branch are you uploading when you, for the first time try to push (upload) your code into the repository. This could be done by the following command git push --set-upstream origin master. Since github requires atleast one branch in a repository, we by convention use the master branch as our uploading branch.

Clone, Fetch, Pull, Push

Clone
Fetch

The git fetch command downloads commits, files, and refs from a global repository (like from github) into your remote repository. Fetching is what you do when you want to see what everybody else has been working on. It doesn’t force you to actually merge the changes into your repository. Git isolates fetched content and it has absolutely no effect on your local development work.

Pull

The git pull command first runs git fetch which downloads content from the specified global repository. Then a git merge is executed to merge the remote content refs and heads into a new local merge commit.

Push

For further discussion on fetch, pull, push refer this.

Some More topics not covered in class.

Pull Requests
Stash

Think when you are in a branch and you have made lots and lots of changes but you are still not ready to commit them and you need to switch to another branch to do a quick bug fix or something else

When we are in a branch and make changes but don’t commit them, then when we switch branch those changes are still present. This could be a big problem as these branches make the working directory unclean. Commands like git status will always record their existence but since you don’t want them to commit what should you do?

Stash is the answer to all you questions.

Think of stash as a reverse bucket when you want to change branches while working on something in your current branch just put those changed files in the stash bucket and git won’t dare to touch this bucket, when you are done on the other branch come back and empty the contents of the bucket back.

Command to put changes in the bucket: git stash Command to empty the bucket i.e. take out the changes from the bucket: git stash apply

Reset
Issues on Github

Resources for further study: