What is Git? Basic Git Commands

With each and every day, the applications we are developing on the internet are getting complex. And, If you are a developer, you know how hard it gets to manage your code and versions as your application grows. One person faced the same issue while writing the Linux Kernel. Yes, Git is a version control system that Linus Torvalds created to manage the application code efficiently. In this guide, I will explain to you the meaning and use cases of git as well as the basic git commands that you will use to manage your code using Git.

First of all, let us assume that we want to create an application. An application to manage tasks. Anyone can register to use our application and anyone can create and manage tasks in their account. Now, We will see How git can help us manage our application code and growth.

Now, Let us first see what is git exactly.

What is Git?

Git is a distributed version control system that can help track changes in the source code of the application. The goal of git is to keep the development clean and to make developers/programmers collaborate with each other without breaking things and with full integrity.

Let’s take an example of our Task management application. Let’s say we now have a programmer who is helping us further develop our Task management application. He changed the code in two files and you too, changed the code in two files. As you are not using git now, You will turn on your SFTP client and replace your two files on the server.

Now, It’s time for your programmer to upload those two files. But he does not know that one file is in conflict, If he uploads a file that you have updated, you will lose your version of the file. You see the problem? Now Imagine your company has 10 developers, It will be bad this way.

Instead, Imagine this scenario. You are using Git to manage your application’s code. And you hired one programmer who will help you code. You made changes in two files and committed the changes. Your programmer updated the two files too. One of those files is the same that you updated. He will commit his changes. As git will look for changed lines and not files, Both of you can update a single file without worrying about how it will reach in production. Git will replace the lines in files that you have updated and it will do the same in your programmer’s case. So, no conflicts!

How to install Git

Now, Let us see how to install git in different operating systems that you might be using. I will include some popular operating systems in this guide. So, Let us start with the Ubuntu. Make sure you have a sudo privilege on your machine. If not, you have to login as a root.

INSTALL GIT ON UBUNTU

To Install Git on Ubuntu, Execute the following command on your computer/server/machine.

$ sudo apt-get install git

It might take some time to install git. Let the process complete and you have git installed and configured on your system.

INSTALL GIT ON CENTOS

To install Git on CentOS, execute the following command on your machine.

$ sudo yum install git

Once done, you can manage your application code using Git. If you are not using any one of this application, you can go to the Official Git website to see how to install Git on the operating system you are using.

Now, Let us see some basic git commands that you will use when you will start working with Git version control system.

Basic Git Commands

In this section, we will take a look at the most useful and basic git commands that you will use while getting started and in majority of the cases. The first and most important Git command is git config. You will use this command first as you will not be able to commit your changes before updating your git configuration.

GIT CONFIG

Using git config command, you can update the git configuration on your system. This way, you do not have to go to the configuration file to update the content. So, After installing Git, execute the following commands to update your name and email in git configuration.

$ git config --global user.name "YOUR_NAME"
$ git config --global user.email "YOUR_EMAIL"

The --global option we used in this command will update the information for all the repositories on your computer. However, If you want to update the configuration for a single repository, Just remove the --global option and run the commands in your application directory.

GIT INIT

The git init command will initialise an empty git repository in the current directory. You will use this command when you already have a project that you want to manage with Git. To do so, just execute the git init command in the application directory and Git will initialise an empty git repository with you. Then you can add a remote(We will cover this too) and push(this one too!) your code.

GIT REMOTE

With git remote, you can add/remove your remotes. In simple words, Remote is a remote git server that will hold your repository. You will get a remote to add when you will sign up for Github or Bitbucket. The most popular git repository hosting services that everyone use.

Here is a syntax of command to add a new remote.

$ git remote add REMOTE_NAME REMOTE_URL

To remove a remote, just replace the add with remove and you are good to go.

GIT CLONE

You can use Git clone command to clone a repository on your computer. You just need a URL to the git repository with proper permissions to clone a git repository. Here is the syntax of the git clone command.

$ git clone URL_OF_REPOSITORY

For example, Execute the following command to clone the Laravel (PHP Framework) to your computer directly from Github.

$ git clone https://github.com/laravel/laravel.git

Git clone is a very important command. You will use the same when you will start working on an on-going project or when you will change your computer, you get the idea.

GIT STATUS

Git status command will show you the list of files that are changed. For example, If you will run git git status command after update the 3 files, It will show you the list of files that are changed. In case you have created a new file, It will show that file in the Untracked files section.

GIT DIFF

You made some changes in the file. But you forgot the changes you made, or whatever the reason may be, you want to verify the changes made in the file. Execute the git diff command to see what changes are made in the file. The syntax of this command is as given below.

$ git diff NAME_OF_THE_FILE

It will show you the lines that are added and lines that are removed from a file in Green and Red colors respectively.

GIT ADD

Git add command is used to add files to the repository or to the staging area. For example, you updated a file called example.conf and now you want to upload or submit or commit (In the Language of git) the changes. Before you can commit, you have to add that file to the staging area. Here is the syntax to add a file to staging area using Git add command.

$ git add NAME_OF_THE_FILE

After running the git add command, If you will run the git status command, you will see that the files added to the staging area will have a green coloured fonts.

GIT COMMIT

Git commit command is used to commit the changes you have made in batch. For example, you wanted to make some changes in your application and you had to update 6 files to do so. Now, It’s time to commit all the changes along with the message so that you can get an idea when you look at the list of commits in future. Execute the following command to commit.

$ git commit -m "YOUR_MESSAGE_HERE"

After running the commit command, you have to run the push command to send the updates to your central git repository.

GIT PUSH

Git push command is used to push the pending commits to the central repository, in most cases, it will be the service you use to host your git repository. Run the following command to push your commits.

$ git push REMOTE_NAME BRANCH_NAME

Once done, Others in your team and you on your production server can run the git pull command to update the code with changes.

GIT PULL

Git pull command is used to pull the changes from remote to your local repository. For example, you updated 4 files to make some changes in your application. You added it to staging area, committed the changes and pushed the code. Now, you want that changes on the production server. To do so, execute the following command on your production server.

$ git pull REMOTE_NAME BRANCH_NAME

When your team members make some changes, you can use the same command to pull changes on your local computer.

GIT LOG

Git log command is very easy. It will show you the version history of the current branch. You can have multiple branches in your git repository to manage multiple versions and features and stuff in your git repository.

To see the version history of the current branch, simply execute the git log command. You will see the commits along with the messages. Normally you would not use git log command as you can see the commits in most user friendly way on Bitbucket or Github.

GIT BRANCH

Git branch command is a multipurpose command. You can use the git branch command without any arguments to get list of all the branches in your repository. To create a new branch in your repository for a feature or a bugfix or anything, execute the following command.

$ git branch NEW_BRANCH_NAME

Similarly, If you want to delete a specific branch from your git repository, use the above given command with -d option, Just like this.

$ git branch -d NEW_BRANCH_NAME

So, this is how you can manage branches in your git repository. Now let us see How to move on from one branch to another in the same git repository.

GIT CHECKOUT

Git checkout command is used to change the current branch. For example, If you are on some feature development branch and now you want to move to some other branch, to do so, execute the following command.

$ git checkout BRANCH_NAME

It will update the code base according to your new branch.

GIT MERGE

Git merge command is used to merge two branches into one. For example, You were developing a notification system that will send your user a notification as soon as a task is due. And Now, you want to put it in the production. It means that you want to merge this feature development branch with the master branch.

To merge any other branch with your current branch, execute the following command.

$ git merge BRANCH_NAME

It will merge the BRANCH_NAME branch with the current branch. It will also show you if there are some conflicts in the code base.

 

Conclusion: So, this is what the git is. And This is how you can manage your application’s code in a git repository. Git will the overall performance of yourself and your team of programmers. Even the huge companies like Microsoft and Google use git to manage their codebase. It is an efficient little program that can save you from hundreds of headaches. The basic git commands we discussed in this guide are the ones you will use 95% of the time. So, learn them practically and with all your mind.

If you are stuck somewhere and want some help, please use the comment section given below. We will get back to you with the answers or help.

Was this answer helpful? 0 Users Found This Useful (0 Votes)