Things About Git and Github You Need to Know as Developer

Manisha Basra
The Startup
Published in
7 min readAug 31, 2020

Hey there, in this blog we are going to learn about Git and Github. As a developer, you might have come across the terms Git and Github but do you understand why do you need or why you are using these? Let’s dig a little deeper to understand these terms.

What is Git?

Git is a free and open-source distributed version control system. It was created by Linus Torvalds in 2005. You can find Git to download at git-scm.com and they also have great resources for documentation.

Distributed Version Control System

What is Github?

Github is a cloud-based hosting service for Git repositories. It is a tool build on the top of Git. In simple terms, it provides a graphical interface that interacts with Git repositories. It makes it easier for the individual and team to use Git for version control and provide collaboration features such as task management, bug tracking and feature request for every project.
You can use Github through a web portal or desktop GUI or Git shell.

Other hosting services for Git repository also exist: GitLab, BitBucket. But for this blog we will look at Github only.

Why do you need Git and Github?

  • Easy Collaboration — let multiple developers work on a project where everyone can upload, edit and manage the source code. And can also work in parallel (with help of Branching and Merging) so that there are no code conflicts between them and all developers can work without losing or affecting each other’s work!.
  • Version Tracking — helps you or team to track and manage changes made in the source code. So you can keep the record of what you or your team members have been doing and you can also revert to any specific version if needed.
  • Branching and Merging — Let’s say you need to work on a new feature for a website. You create a branch so that you won’t affect the main source code. While working on this new feature you get a request to fix an issue on a live website that too before completing this new feature. You create a new branch from the main branch and finish your work and merge this fix branch in the main branch and continue working on your new feature branch and can easily merge this new feature branch in the main branch without worrying about the changes you make in the fix branch.
    But without Git and Github, it will be very difficult to maintain different copies(main copy, feature copy and fix copy) of your source code.

Before we look at why do you need Git and Github, let’s look at some important Git and GitHub terms:

Distributed version control

Distributed Version Control System
This type of version control allows the clone of the complete codebase (repository) including its full history on every developer’s system. If any time server(main repository) dies, any of the developer’s repository can be copied on to the server which helps restore the server.
And it helps the software team or individual in managing and keep the track of changes made in the source code in a special kind of database. It takes the snapshots of every revision of your source code, which you can access to compare or restore them as needed. In another word, it gives you the power to review or even restore the earlier versions of your source code.

Repository(Repo)
A Repository keeps the track of all changes made to files in your project and builds history over time. Git repository .git/ is a special folder present at the root level of a project.
Local Repository —
refers to the repository on your computer or the local machine
Remote Repository — refers to the repository on a server (Github).

.git/will be a hidden folder inside your current folder.
If you delete this folder then this will delete the project history.

Master branch
When a repository is created, Git automatically creates a master branch. By convention, developers use the master branch to be the base copy of the source code, and all the code that is being written in different branches (feature, hotfix, bugfix) will eventually be merged into the master branch, and it could represent the current released version of the application being developed. As such there are no restrictions on branch names. You can call your branches anything that you would like.

Feature branch or Develop branch
It has no difference from the master branch or the base code. It’s just a clone of source code which we have check-out or created from the stable version of the code. Let’s take an example to understand why do you need a feature or separate working branch. You are working on a new feature for a website and you are making changes directly in the master branch. While working on the new feature, you got a task to fix an issue and that has to be fixed today itself. Now you can’t push your incomplete new feature task for deployment and at the time you have to push the fixed issue for the deployment.
To prevent these situations, you should create a separate working branch for every task. You will create a new feature branch and a bugfix branch from the master or base code. With these separate branches, you will be able to keep your every task in a separate branch and that can be merged in the master branch according to the need without affecting other tasks.

In the local repository, HEAD points to the current checkout branch(testing).

Head
Git Head is a pointer or reference variable which point to a specific commit in the repository. As we make new commits, the pointer(Head) is going to change or move to point to a new commit in the repository. In the currently check-out branch, Head always point to the last commit.

Unstaged Area / Untracked Files
Changes made in the working directory(or branch) that are not handled by Git are refreshed as untracked files or unstaged files. These changes can’t be committed.

Working tree, staging area, and Git directory

Staged Area / Tracked Files
The action of adding files or changes we want in the next commit or next version of the code. This is how Git knows what’s gonna change between your current commit and your next commit.
With the help of the staged area, you can move only those changes or files that you want in the next commit or next version of the code and the rest can be in the unstaged area.

Committing(creates a new version of the code)
The action of storing the changes added in the staging area in the local Git Repository. The changes added to the local Git Repository will not be reflected in the remote Git Repository until we push these commit.

The HEAD pointer moves forward when a commit is made.

Pushing
The action of uploading your local Git commits to the remote(online) repository on Github.

Pulling
The action of downloading the remote(online) Github commits and bringing them into your local machine.

Pull Request(PR)
A Github feature that allows users to easily see the changes (the difference or “diff”) that a feature branch is proposing as well as discuss any tweaks that said branch might require before it is merged into master

Merging
The action of taking the commits from a feature branch and adding them to the top of specified branch history and changing the Head to the last commit(that the last commit in the feature branch).

Check-out
The action of moving from one branch to another.

Here is the list of the some of the commonly used Git commands :

  • Clone — used to create a copy of an existing Git repository.
    $ git clone <repository name>
  • Add — add changed files to the staging area and that can be saved in the git repository via committing.
    adding a specific changed file to the staging — $ git add <file-name>
    add all the changed files to the staging — $ git add .
  • Status — show the status of the working tree(staged or unstaged files) such as which files have recently been modified.
    $ git status
  • Commit — save your changes to the local Git repository.
    $ git commit -m “commit message eg- initial commit”
  • Push uploading local changes to a specific remote branch.
    $ git push origin <remote branch name>
  • Pull — to download and integrate remote branch changes in your local branch.
    $ git pull origin <remote branch name>
  • Branch — create a new pointer head on the commits.
    $ git branch <branch-name>
  • Check-out — used for branch changing.
    change branch only — $ git checkout <branch-name>
    changing and creating branch — $ git checkout -b <branch-name>
  • Merge — its way of putting a forked history back together again. In other words, it integrates the 1 branch with the other branch. For example, merging the feature branch into the master branch.
    $ git merge <branch-name>
  • Delete — remove pointer head from the commits.
    local branch deletion — $ git branch -d <branch-name>
    remote branch deletion — $ git push origin --delete <branch-name
  • Rename — branch renaming.
    rename current branch — git branch -m <branch-name>
    rename branch while pointed to any branch — git branch -m <old-branch-name> <new-branch-name>

I hope this article is useful to you. Thanks for reading, and keep learning!

--

--

Manisha Basra
The Startup

JavaScript Developer. Having a passion for understanding things at a fundamental level and sharing them as clearly as possible. *jscodelover on Github*