Git and GitHub

Akash Pandey
16 min readFeb 2, 2021

Hello, In this Article you will get to know about Git and GitHub from the basic to advance level. Git is one of the most important and powerful DevOps tool and GitHub is a Central Repository where multiple people collaborate together from different location and work on Single project from their local repository.
So, In this Article You will learn the following :-

  • What is Git and GitHub?
  • How to use Git in local repository and Central Repository
  • What and how to use git add, git commit, git push, git pull, git clone, git stash, git checkout, git reset, git branch, git log, hooks, git cherry-pic etc.
  • How to Fork a Repository , Creation of Pull request and how to merge pull request ?
  • How to Collaborate together?
  • Git Kraken GUI Tool
  • Git as a DVCS

What is Git ?

Git is a free, open-source distributed version control system tool used for Continuous Development. It is designed to handle minor to major projects with high speed and efficiency. It is developed to co-ordinate the work among the developers. The version control allows us to track and work together with our team members at the same workspace. Git can be used privately and publicly.

Git was created by Linus Torvalds in 2005 for development of Linux Kernel.

Note :- In order to use git command in windows, you have to download one program/software which is Git Bash.

Installation of Git in Linux ( RHEL8) :-

By default, Git is installed in most of the Linux Operating System. To check whether Git is installed or not in your OS , you have to use

# git --version

This command will help you to find whether git is installed or not.

So, Here Git is not installed . So , You have to download git using

# dnf install git

Output :- git version 2.18.2

What is GitHub?

GitHub is a Git repository hosting service. It offers both distributed version control and source code management (SCM) functionality of Git. It also facilitates with some collaboration features such as bug tracking, feature requests, task management for every project.

  • Workspace is like a folder where you keep files and write the code.
  • Commit Area :- It is used to record the changes in the repository and Commit are the Snapshot of the repository.
  • Git tool gives the facility of Source Code Management (SCM).
  • Controlling the version of code you commit the changes multiple time and updated in commit area is managed by facility provided by Git known as Version Control System (VCS)
  • Staging Area :- This area keep tracking of update/change in repository contains the index/versions of data but not the actual data.

First, You have to initialize the repository

# git init

This will create one hidden folder .git This is very important folder and store all the things and Every git repository have their own .git folder.

Now, If you use git status then there’s to commit yet because you haven’t created any file and added in the staging area.

Now, You have to create the file

# cat > aks.txt
Hello Akash

Now, you have to add this file in Staging area to add tracker/pointer/reference

# git add aks.txt

git add command is used to add file contents to the Index (Staging area).

Now, aks.txt is tracking by git in Staging area but it is not committed yet. So, you have to commit this file.

Whenever you commit, Git automatically create versions, which is very important to track, and attach the version to the committed file. This is also known as reference.

When you commit the file, Developer have to add message/comment at every commit time which helps in debugging the code.

# git commit -m "first commit" aks.txt

Now, One file is changed and commit successfully. commit-id is a cryptographic number created by SHA (Secure Hash Algorithm) algorithm.

HEAD is a pointer which points to the last commit versions.

Similarly, You can add and commit the file as per needs.

To see the git log in one line and commit id and message you attach while commit to the file, So you must use

# git log --oneline

Now, If you have to undo the changes means May be you have written the wrong code that is not working or you have to discard some changes at specific commit id or at specific commit place. Finally, you want to Rollback your changes to some specific point or reset the code. So, there you have to use git reset and git checkout command to rollback dynamically without editing the code because editing the code might result in some other changes or bug.

# git reset commit-id file-name# git checkout -- file-name

It’s good to work in a separate branch as master contains all the data and information of all the branches as well as contains history/versions. Once you done all the changes in separate branch , you can merge to the master to make everything synchronized and it’s easy to manage and Normally, This is the good way to manage the code where developer have to manage thousand lines of code and likewise local repo , In real world , There’s central Repository, like GitHub, Where multiple developers collaborate together and work on a single repository in different branch from their local repo.

So, You can create budget and go to that budget using

# git checkout -b branch-name

First, Git create one branch and then switch to that branch. Now, the entire history is copied to the newly created branch. So, Whatever data Master have , is copied to the new branch as well HEAD pointer will point to Master and aks1(new branch name) branch.

# git branch --show-current

This above command will show your current branch.

Now, If you can make any changes in this branch and commit the changes you have done in the file. Then, whatever you will change and commit in this branch will not reflect in master branch until you merge.

Now, Here you can see that HEAD pointer is points to the aks1 branch , Previously before commit HEAD pointer points to both aks1 and master branch.

So, If you want to merge the master branch with ask1 branch. First , You have to switch to master then merge the aks1 branch.

# git checkout master

# git merge aks1

# git log --oneline

Now, Here you can see that There’s some change in master branch but not merge to the aks1 branch. So, HEAD Pointer points to the master branch and aks1 branch is 1 commit behind the master. So, You have to merge aks1 branch with master to make the version also copied in the aks1 branch.

Here, Instead of using merge command, You can use git pull command after setting the upstream branch.

# git branch --set-upstream-to=master

# git pull

Now, HEAD points to master and aks1 branch at same point.

If you are working on a large project then there’s different people collaborate together to work on the same git repository. So, if you are working in local repository then no other people can collaborate together (though via DVCS other will contribute in your project , discuss later in this article). So, Here you need Centralized Version Control System ( CVCS ) and GitHub is one of the example of CVCS where multiple people can contribute in a single project from their local repository. Here, It means that Every individual can work on their own local repository and push their code to central repository GitHub in their respective branch and continuously Integrating and developing the code to finalize the project and when everything is tested and works fine then Master will pull all the code from different branch to make the final code and In Case , any further changes needs at any point of time, It can be tracked by the index stored in the staged area.

Now, First you have to integrate Git and GitHub together to push the code to GitHub repository what you have change in the local repository.

If Local Repository is already created then at a time of creating GitHub Repository don’t initialize it because internally it create .git folder and at time of integrating local repo with GitHub repository , merge conflict problem will arise.

In local repository , you have to add the location of GitHub repository to push the code from local repo to GitHub repo.

# git remote add origin https://github.com/whoaks/git-github-ws.git

To check whether remote Repository is added to local repo or not then use

# git remote -v

Push the code from local repository to GitHub

# git push -u origin master

Code is successfully pushed to GitHub.

If You want to directly make change in GitHub repository , though it’s not a good approach but you can do it. Then Local repository is 1 commit behind the GitHub Repository.

There might be chance that you have already created GitHub Repository and initializes it. Then, you can clone the repository from the GitHub to your Local Repository using git clone command.

Creation of GitHub Repository and initializes it using ReadMe file.

By default, base branch of GitHub repo is main branch. So, if you want to update the base branch then click on the setting and update the branch name.

Now, Clone this GitHub Repository

# git clone https://github.com/kshitiz21/mygithub.git

Now, You can see GitHub Repository also created .git folder where they contain the information of metadata.

Similarly, You can push the code from local repository to GitHub repository as shown in the figure.

Here , one thing you have noticed after commit the code in local repository, I have to manually push the code in the GitHub repository. So, If you want to automate the process such as , when you commit the code in local repository , It will automatically push the code in GitHub repository. So, here you have to configure local hook and write one post commit file.

So, Create one post-commit file in hooks folder .

# cd .git 
# cd hooks
# cat > post-commit
#!/bin/sh
git push

After commit , Code is already pushed to the GitHub repository without manual commit. So, you write many hooks to automate the process after every event like push, add, commit, pull etc.

Similarly , You can use post-commit hook such that as soon as you commit the code , log automatically shows to us.
Here, I created one more repository to explain some more git command.

Similarly, You can initialize the repository, add the file, commit the file , Go inside .git folder then hooks folder and create one post-commit file.

Here, As soon as , you run the git commit command , log will automatically displays.

How to Fork a Repository ?

If you want to contribute your code to github repository or collaborate together to work on a single project. Then, there you need to fork repository of master branch, who is the actual owner of the repository.

So, if you want to work together , There is two way to contribute your code .

Using fork

If you want to fork someone’s code, then Search the repository name and fork.

Creation of file from you ( who want to contribute his/her code to owner of the repository)

Now, Here you have to create a pull request so Owner of the repository will pull your code and you will able to contribute your code.

Now, Here you can see that there’s one pull request to whoaks github repository (owner of the repo).

Now, Here whoaks (owner)will merge the pull request from kshitiz21 repository ( contributor).

Now, Code is merged and kshitiz21 contribute his code with whoaks

How to Collaborate together?

Now, In owner repository (whoaks) you have to provide access to the collaborator (kshitiz21)

Follow the steps to collaborate together on a single project :-

Now, Collaborator will receive one mail regarding collaboration with whoaks GitHub repository.

Here, kshitiz21 is successfully contributed his code to whoaks repository.

You can use GUI tool to make collaboration easy and visualize the changes and track progress. So, here you can use GitKraken Software to visualize the changes. It’s Open Source and anyone can use it.

Download Gitkraken :-

After download , Install Gitkraken Git GUI Tool to visualize the progress in real-time.

Open the Local repository in Gitkraken.

You can clearly visualize the changes and progress tracks and there is only one master branch.

You can also make changes from this tool and commit the code as shown above.

If you create any new branch and make any changes in that branch, you will visualize the HEAD pointer from this tool.

Here, I create one branch name aks1 and edit the aks.txt file and commit without merging to master. So that HEAD Pointer points to aks1 branch.

Now, Master branch is merge with aks1 branch. So that HEAD pointer points to both the branch at same time.

So, Sometimes problem arises as master have to manage various versions which takes time and also consumes CPU and RAM. This will affect the performance of the events sometimes when there’s thousands of versions. So , here I want that master only fetch the data from the other branches without fetching/downloading the history/timelines/versions. This will improve the performance and decrease the workload of master branch. So, here you have to squash the merge.

# git merge --squash aks1

This will download the data of aks1 branch but not it’s history/logs.

Now, If you switch to aks1 branch and edit the file, commit the changes and try to merge with master then merge conflict arises as both the master and aks1 branch commit the same line and Git is not able to know whose commit to merge with each other and discard the commit of other branch. So, Either you can manually solve merge conflict problem or use some third party tool which solve this problem, So you can also use p4merge tool which solve above problem.
Here, I solved the problem manually by editing the aks.txt file manually to solve the merge conflict.

Here, you can see that as soon as aks1 branch try to merge with master , merge conflict arise.

Here, When Merge conflict solve and both the branches merge together then that Version is know as merge commit. You can see that merge commit version has two parents. one from Master branch and other from ask1 branch.

At this point, Git uses Recursive Strategy automatically.

If you want to use p4merge tool to solve the merge conflict automatically then you have to specify it’s location in git global config file so that git can know about merge conflict tool.

# git config --global -e
[merge]
tool = p4merge
[mergetool "p4merge"]
path = C:/Program Files/Perforce/p4merge.exe

In Some use case, You have to merge or pick only particular version or commit changes from other branch lets say aks4 branch to merge with different branch, lets say master branch. Then, you have to use git cherry-pick concept.

git cherry-pick is the way to pick only data or particular version you need from other branch and put cherry on the top of your code. It’s like putting a cherry on the top of delicious cake. It’s very small in size but it’s make the cake more delicious and complete when put on the cake.

So, Here I have created another branch aks4 and commit some changes. Then, particular version of data from aks4 branch and merge with the master branch. Since, I have pick particular version of data. So, here I have to provide commit-id of the version i need. Version of cherry-pick commit changes as soon as merge with master branch.

Creation of aks4 branch (new-branch) :-

# git branch aks4

Switch from master branch to aks4 branch

# git switch aks4

Now, I have make some commit changes So that aks4 branch is ahead of master branch.

Here aks4 branch is 3 commit ahead of master branch as it’s visible from the above picture. Now, I switch to master and pick one cherry from aks4 branch using commit-id.

# git cherry-pick commit-id

There is sometime requirement comes up where you have to load the data inside memory but don’t need to commit until needed. At that you can git stash command.

git stash record the current state of the working directory and the index. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Here, I created one new branch, aks_stash, to show the use of git stash command.

To know the modifications stashed by stash , you can use

# git stash list

So, to stored the changes on the top of another commit, you can use

# git stash apply

Now, Commit changes successfully apply to master branch.

Git as a Distributed Version Control System

Git is a distributed version control system (DVCS), or peer-to-peer version control system, as opposed to centralized systems like Subversion. There’s no notion of a “master” or “central” repository with Git.

This will improves the performance and reduce latency as Git don’t depend upon the central repository.

To demonstrate Git as a DCVS , I have pull the code from a Server (RHEL8).

So, If git is installed in server then you can initialize the repository , commit the changes and then pull the code from another system using ssh or any other means. By default, ssh port is enabled in git.

Creation of git repository in one Server :-

Now, you can successfully clone this repository from other Server , where git installed.

# git clone root@ip:/repo-name

Successfully clone the repository from other Server into your server without need of Central Repository. This is known as Peer-to-Peer Connection.

Conclusion

Now we have discussed the features and benefits of Git and GitHub and it’s important. Undoubtedly Git and GitHub as the leading version control system.

Thank you :)

--

--

Akash Pandey

I am a Computer Science Undergraduate , who is seeking for opportunity to do work in challenging work environment .