GIT – A Simple Getting Started Guide For Git

GIT – A Simple Getting Started Guide For Git

There is a term called “Git” in IT field, which may not be much familiar to most people despite being one of the most useful and handy tools in the industry. So, in this post, I’m going to cover the introduction and getting started guide for Git so that more people will be able to understand it and leverage its benefits.

Git Logo
Git logo

What is Git?

Basically, Git is a distributed version control system. So, what is version control system and why should we care about it? Well, have you ever been in a project, where many people collaborate together from various sections and everyone keeps changing/growing their work? In this scenario, each member may be well-aware of the work done by himself/herself, but what about the work done by other team members? In large or long projects, even keeping track of one’s own work is pretty trivial task. Although it feels like we can remember what we’re currently doing, in reality, our memory keeps fading away with the time. There might be some specific circumstances which led us to do something the particular way, but we’ll more likely forget what we were thinking then, when we’ll look at our work sometimes later. This is the problem domain that Git correctly addresses and solves for us.

Especially in the software or application development sector, Git is a must have tool. It’ll truly be a very hectic thing to develop, manage and maintain a software without the help of some version control system like Git. It records changes to the program codes over time and maintain separate versions for each major changes. This way, you don’t need to worry about remembering what changes you made during various stages of development and why you made those changes.

Installing Git

Git is available in almost all platforms including Linux, Windows, MacOSX, Unix, and so on. All you need is to download the appropriate installer package of Git for your operating system and install it. Here’s a quick summary of how to install Git in various platforms:

Git On Windows

Simply, download the Git installer file from this link: https://git-scm.com/download/win. Note, you may need to download the particular setup file based on your PC’s architecture (32bit / 64bit). You then need to install the downloaded file and follow the setup wizard to complete the installation. Once installed, you should be able to run git command in your command prompt.

Git On Linux

Since there are numerous linux based distros available and the installation method may vary for each of those distros, I’m just going to cover some prominent ones:

In Ubuntu or distros using deb based packager like Debian, Linux Mint, Netrunner, CubLinux, and so on, you can simply install git using apt-get utility.

# sudo apt-get update
# sudo apt-get install git-all

In RPM based distros like Fedora, CentOS, Red Hat and OpenSUSE, use yum to install it:

# sudo yum update
# sudo yum install git-all

Git On Mac

You can install it on Mac by downloading the installer from http://git-scm.com/download/mac and installing it. Once installed, you should be able to run the command git in terminal.

Getting Started With Git

Now that we’ve got Git installed in our system, its time to have some fun with it. First, let’s check what’s the installed version. To do so, open up your Command Prompt (for Windows) or Terminal (Linux or Mac), and then this command:

# git version

Next, let’s tell Git who we are so that it identifies us when we create repository and push contents to repos. It can be done like this:

# git config --global user.name "sajjanbh"
# git config --global user.email "sajjanbhattarai@gmail.com"

Then, let’s begin creating our local repository and adding some files to it. First, let’s initialise a local repo in our computer and then, I’ll make it working with popular Git systems like Github and BitBucket.

To initialise a local repository:

# git init <project-folder>

If you’re already inside your project folder, you shouldn’t enter project-folder argument. However, if you already have a remote repository or need to collaborate in already available remote repository (eg. https://github.com/sajjanbh/Test.git), you just need to clone that repository in your local computer and start contributing. You can clone this remote repo in any local directory you want.

# git clone <path-to-remote-repository>

Then, you can start making changes to the files of repository and/or adding files to it. Once done, simply add those changes to git, commit your changes with a description and push them to remote repository.

# git add .
# git commit -m "Change Description"
# git push

Doing this will push the changes that you’ve made to the version control system and your team members sharing that repository can view your changes. Now, you might be wondering about what will happen if different people make different changes to different parts of the project simultaneously. It is also common to question how we can keep and manage our separate instance of the project and merge it with other’s work after reaching certain milestone. Well, these questions are addressed by Git through the use of master and branches. Master is the main instance of the project and branches are the separate instantiated versions of the master, which can be managed independently. I’ll discuss about using these a little later in this post. But before that I’d like to talk about two popular and freely available Git based solutions:

Github

Github is probably the most popular web-based Git repository hosting service offering distributed version control and source code management (SCM). We can create and manage public repositories free of cost, while we need to pay for private ones. To start using it, we first need to create an account in Github and login. Once inside, we can create a new repository and start working in it. Repository creation looks like this:

Github New Repository
Github New Repository

After creating a repository, Github also provides some initial steps to get ourselves started with in Git. Here’s how these suggestions look like:

Github Quick Start Tips
Github Quick Start Tips

Along with version control and source code management, Github also allows us to publish our project application, documentation and Wikis. This feature is particularly useful when we need to present our work to others. If you’re wondering what I mean by this, here’re some small projects I’ve published in my Github account. Actually, I did these as assignments of the courses I studied in Coursera so that I could allow my peers to view and review my work.

BitBucket

BitBucket is my next favourite repository hosting service. I prefer BitBucket to Github when I need to maintain a private repository because it also provides free private repository hosting with some limitations, but enough for small projects. To start using it, we first need to signup in BitBucket and then login into our account. Next, we can create our repository as follows:

BitBucket Create Repository
BitBucket Create Repository

BitBucket also provides a getting started tips, which looks like this:

BitBucket Getting Started
BitBucket Getting Started

Master and Branches

Do you remember the repository that we’ve created or cloned earlier? Well, that one is our Master. Normally, if we’re working alone in a project and that project isn’t much complicated, working on this master node might be enough. However, real-time projects tend to get complex and difficult to manage with time. In that case, having branches of project can be highly helpful. Branch allows us to instantiate the current version of the master and initiate a separate copy of it, in which we can make our changes and test them without affecting the main project version. If everything in our branch is working correctly, we can merge its content with that of other branches or master and implement our work in the project mainstream.

Here are some useful commands to manage branches in Git:

# git pull
# git branch
# git checkout -b <new_branch_name>

Here, I’m only pulling the latest version of repository, listing the available branches and selecting the preferred branch in above commands. In this case, I’m assuming that I already have my branch created in remote hosting service and I’m only checking it. To learn about the full list of options available with branch management, you can refer to this guide or any other available in Internet.

I think this will be adequate to get yourself started with git. I understand it still might feel difficult or confusing, but the key to get past this difficulty is by taking a step forward and practising. I recommend you to immediately signup with any repository hosting service like GitHub or BitBucket and starting working with your first repository. Trust me, you’ll begin to realize its advantages and fall in love with it.

I hope this post has been informative and useful for you. Please let me know of your feedback or queries in the Comments section below. I’d truly appreciate your constructive comments. Thank you and Happy Learning!

Sajjan Bhattarai
System Engineer at DristiTech Pvt. Ltd.
I’m a tech-enthusiast and blogger. However, I believe “Learner” will be the best title for me. I’m mainly interested in Science, Technology and Humanity. I love solving problems and challenges. I think passion, discipline and calm nature are my key strengths. Apart from these, I’ve a highly adaptive personality because I excel at quick learning, hard-working, and thinking out-of-the-box. I share my thoughts and learning stories in my blog (https://blog.sajjan.com.np), so it’s the best place to know more about me.
Comments are closed.