Introduction

What is Git?

Terms you need to know to proceed further

Setting up the environment!

End

Getting Started With Git - A Walkthrough Tutorial

What is Git?

Git is a distributed version control system. It is used to track changes in files, and to coordinate the work flow.

It was developed by Linus Torvalds. There are many other version control systems available, but git is the most popular version control system.

In this tutorial we will cover the basics of git, you will learn commands such as git status, log, add, commit, push and much more!


Terms you need to know to proceed further

repository - A repository (repo) is just like a folder or directory in which all your files will be contained. A repository can contain any number of files or folder as per your requirement.

commit - Whenever you make some changes to your work, the set of changes you add may be collectively referred as a commit. Each commit has a unique id reference to deal with each of it for several purposes.

branch - A branch is like a route that ends up to a specific version of your work. There might be more than one branch as per your interest, by default all your work is setup on the master branch.


Setting up the environment!

Installing git on your machine :-

Download the installation package here.

For Windows and MacOS, download the package and install it.

For Linux, you can download and install the package or you can also use the command line to install git.

# debian based
sudo apt-get install git

# Fedora
sudo dnf install git

# openSUSE
sudo zypper install git

# Arch
sudo pacman install git

# CentOS
sudo yum install git

To check the installation version of git use the following command,

git --version

You may want to start a new project and use git as a version control system for it, or you might be contributing to an existing git repository.

To setup a new git repo, we need to initialize it first, and it can be done using the following command,

git init

This creates a .git directory in your present working directory

After initiating the git repo we need to setup username and email to show up the details in our git log. We will be going through the git log command very soon.

We use the following commands to setup the username and email for our repo,

git config user.name "John Doe"
git config user.email "[email protected]"

To specify these details globally across your system, you can use --global , doing this, all your work with any git repos across your computer, will be using the same details. It can help you save your time as you don't need to setup it every time. Here are the config commands with --global flag,

git config --global user.name "John Doe"
git config --global user.email "[email protected]"

The next step is to link to the git repo we created on any of the git repo hosting services, we can do this by the following command,

git remote add origin https://github.com/username/repository.git

Replace the placeholders in the link with the details of your choice.

Now, we will see how to work with an existing git repo. We will use git clone command to create a local copy on your machine. If the repository is private, you may need to enter your account credentials. You can work with a private repository only when you are the owner of the repo or a collaborator for that repository.

Use the following command to clone a repo,

git clone https://github.com/username/repository.git

You can now make changes to the existing files or folders in the repo, or you can add files / folders, if it is a new repo.

We can check the status of the repo using the git status command, this will let us know about files or folders being modified, newly added, or deleted.

git status

The new files or folders you create are not tracked by git, you need to inform git by using git add to track them, and we use the same command to track changes made to the files that are already existing, We can use the git add command in few different ways,

We can add all the changes at once using the following command,

git add .

Or we can simply add changes of our choice,

git add file
git add folder/

You can add multiples files and folders simultaneously as shown in the following command,

git add file1 file2 folder1/ file3 folder2/

You can add as many changes as you want just by seperating changes by giving a space.

Now let's commit those changes. Every commit should be provided with a message, Use the following command to commit any changes,

git commit -m "First commit"

Let us consider an example, I have created a file hello-world.txt, just to make it simple, I will use text files as an example. I will now add hello-world.txt using the git add command,

git add hello-world.txt

Now I will commit this change,

git commit -m "Added hello-world"

You can write the commit message as you wish, but making it descriptive will help you to understand what the commit is about or what changes you have made to the contents of the repo in the future.

I will now create a new folder, namely, myfolder/ in my local repo, and I will add a new file myfile inside the new folder and a file newfile at the root of the local repo. The hierarchy of the contents of the repo is as follows,

  • Repository
    • hello-world.txt
    • myfolder/
      • myfile
    • newfile

As we have already added and commited hello-world.txt previously, we only need to add other changes,

git add newfile myfolder/

We don't need to add myfolder/myfile seperately, as it is newly added. Adding the folder will automatically add all the contents of that folder. But if you are changing an existing file inside a folder, you may need to add it with the filename with its path. Let's commit these changes and look at an example for this case.

git commit -m "Added myfolder and newfile"

For example, let's say I have made few changes to myfile, so I need to add it first and then commit the changes.

git add myfolder/myfile
git commit -m "Updated myfile"

You can use git log to view all the commits made to a repo.

git log

This displays all the records of the commits made to a repo.

All the commits we made are still local and we need to push them to the remote repository. We will use push command for this,

git push -u origin master

Every repo has a default origin and a default branch named master. We will look at creating branches later. The above command uploads all the changes to the master branch.

When you are working on a group project, you will be encountering merge conflicts once in a while, you can overcome this problem by manually editing the files with merge conflicts. Merge conflicts can be a nightmare sometimes.

Let's look at some examples of branching and merging branches, we won't be going deep into the branching, and let's keep it simple. We can create a new branch using the git branch command,

git branch branch_name

To switch to a branch we will use the git checkout command,

git checkout branch_name

We can merge a branch to the master with the git merge command, to do that we need to first switch to the master branch and then use the command,

git checkout master
git merge branch_name

We can also make version releases using the git tag command, for example we can refer to a specific commit and mark it as a new version. By default the git tag uses the latest commit to mark the version, here is an example for git tag,

git tag tag_name

For example, git tag v1.0 will create a new tag with tag name v1.0 , you can use git tag to list all the tags you have created.

We will be ending our tutorial here, to learn more about git you can refer to the git documentation. If you found this article helpful, feel free to share it in your circle.

Tags