Git AND GitHub

This article explains about the basics of Git and GitHub, how it works and the basic commands used in the command line terminal.

What is Git:

Its is a command tool to operate GitHub. This helps to manage the changes that is made to a project. This allows to record changes to the files and to view the specific changes made to the file.

Why Use Git?

Git is a standard development for the technology industry. It enables the developers to screenshot their codes at a particular time which is very much helpful for the developers to grow their codebase and to have reference to their previous version of their code.

What is Repository?

Repository, also know as repo is the storage folder where the developers store their files.

What is Version control?

The version control is to track the changes that is being made to the code by the developers.

Types of Version Control:

Local version control:

This is type of version control where the developers work on the local repository present in the local system. Here they work on the same version of file without any new changes.

Centralized Version control:

In Centralized Version control, a centralized server will be present. This enables the team to have collaboration among them during developing.

Here the changes are made to the shared copy and then pushed back to the centralized repository. The main problem in centralized version control is that the repos are not locally available. Hence it requires the developers to have connected to the network all the time.

Distributed Version control:

In distributed version control, the developers can able to clone the repo from the server. They can update the new changes to the data coming from the central server by pulling the files and affect the changes to the main repo by pushing the file to the server.

Git is a distributed version control.

Commands used in Git Command Line:

cd : to change the directory

mkdir <directory name> : to create a new folder in the directory

ls <directory/folder name>: to check the folders/files in the directory.

touch <file name>: to create a file in the folder inside the directory. The files may be of txt, html, css or js .

git init : This is to initialize the project. This helps the git to create a hidden git file inside the project directory and turns your project into a repo.

git add . or git add <file name> : This command makes your file to move from unstaged stage to staging stage.

git commit -m <message> : To commit the changes made in the staging stage to the file along with a message about the changes made.

git diff : To check for the changes made between two versions of the file.

git status : To check the summary of the changes made in the file.

For many more git commands check out the below link:

https://git-scm.com/docs/git#_git_commands

How Git Works?

The Git has four stages

Workflow of Git

The first stage in git is called unstaged stage. This is the stage where the project is initialized using git init command. When the command is run on the terminal all the files from the project file enters in to the unstaged stage. In this stage the tracking of git does not takes place.

Using the git add command the files enters from the unstaged stage to the staging stage. In staging stage the files are tracked by git. When changes are made in the staging state, the file enters into a modified stage. After changes are made the files have to be pushed back to the staging state using git add command. The modified files cannot move to the commit stage directly.

The file is commit from the staging stage using git commit -m command.

Further when the code is written and modified the files can be pushed and commit using add and commit command.

GitHub is a service that allows developers to host their Git repositories online and collaborate with other developers on it. One can use GitHub by their web page as well as GitHub desktop GUI and the Git terminal.

One can create a new repository in the GitHub for the project that has been initialized. You can copy the repository from another developer using the Forking method which is available in your GitHub.

For copying the repository to your local computer, cloning method is used. One can clone the repository to local repo using git clone command along with the URL of the concerned repository. git clone <url>

How GitHub works?

The developer can add his/her local repository to GitHub. This enhance other developers to work on the same file by pulling the files to their local computer. The changed file can further be pushed into the GitHub repository.

GitHub Workflow

Branching and Merging :

Branching is an integral part of version control. This helps to create a new space to make new changes to the file without making changes in the master/ main file. The branch may be a local branch which is connected to the local repo or a remote branch which connects the local repo to the central repository.

The branch can be created using the command git checkout -b <branchname>.

After the changes are made in the branch it should be merged with the main / master branch using git merge <branchname> command.

When different changes are made to the same files on more than one branch, you may run into merge conflicts. This must be resolved manually by the developer using merge tools.

Merging Branches

https://git-scm.com/docs/git-branch

https://git-scm.com/docs/git-merge

Rebasing:

This moves the entire content of the branch to beginning of the tip of master branch effectively. It incorporates all the new changes to the master branch. Unlike merge commit, rebasing re-writes the project by creating brand new commit for each commit made in the master branch. git rebase command is used to rebase the branch to master branch. https://git-scm.com/docs/git-rebase

Rebasing

Stashing:

This helps the developers to create a non commit checkpoint. It temporaly stores changes made in the working file which can be used in the later part of development. git stash command is used to create a stash.

https://git-scm.com/docs/git-stash

Check the below resources for better understanding on Git and GitHub

https://git-scm.com/docs — Git Documentation.

https://www.youtube.com/watch?v=41tsyReTloA&t=2749s

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store