DEV Community

Cover image for About Git #01
Chathumi Kumarapeli
Chathumi Kumarapeli

Posted on

About Git #01

Let's begin this tutorial with a scenario. Assume that you are a developer who is working on a project with another bunch of developers as a team. One of the developers in your team adds a new feature to the project. Now some functions which were functioning perfectly well before, gets crashed. What can you do now? This is where version controlling comes to play. They help to keep track of various versions of your code segments and also helps to recover any break downs that happens.

Version Control Systems (VCS)

Version Control Systems keep track of the changes that we make to our files. We can edit multiple files and treat those files/ collection of edits as a single change. This is commonly known as a commit.

There are two categories of version control systems;

  1. Centralized VCS

    Here all team members are connected to a central server to get the latest copy of the code and share the changes with others. The issue with this category is the availability of a central point of failure. If server goes offline team members cannot collaborate with each other.
    Ex: Subversion, Team foundation server

  2. Distributed VCS

    In this category every team member has a copy of the project in the history of his machine. Hence, the snapshots of the project is saved locally to the machine. Even though the central server is offline, synchronization can be done with others.
    Ex: Git, Material

Git

Among various version control systems Git is the most famous one. That is because it is free, open-source, fast, and scalable. It also provides cheap branching and merging facilities. It helps a group of developers to do coding collaboratively without facing issues as it keeps tracks of different versions of the code.

You can use Git via,

  • The command line
  • Code editors and IDEs
  • Graphical User Interfaces

Installing Git

To check whether you already have git installed in your machine, simply open the command prompt and type the command git --version. It will show the git version in your machine, and if it is higher than 2.20, you are good to proceed in this tutorial. If you get an error when you run the above command or if your version is less than 2.20 then you need to install the current version of Git. You can do so by going to this link.

Starting with Git

Setting Global Configurations

As I have mentioned above, Git keeps track of the things like who made changes and what changes he made. For this we need to add few configurations.
First let git knows who the global user is.

If you are using Windows you can use Git Bash
git config --global user.mail "yourmailaddress@example.com"
git config --global user.name "your name"

You can also configure an editor or an IDE. Since I am using Visual Studio, I'm going to set it by following code,
git config --global core.editor "code --wait"

You can check all the above configuration information by opening the VScode using following command.
git config --global -e

Creating Repositories

First let's create a new folder and navigate into it using following commands.
mkdir newFolder
cd newFolder

Now we are to make a git repository. You might be wondering what does Repository mean in Git. It is where all of your software packages and code files are stored.
You should use the following command to create a git repository inside your current folder.
git init
This command initializes a new empty git repository in your current directory.

Inside your current folder now you have a sub folder named .git which is hidden for the moment. If you type the command ls you cannot see anything. But if you use ls -a you can see the hidden .git repository. So why this sub folder is hidden? Actually this folder includes information about your project history. It includes directories like branches, info, objects, references, etc. Hence this is not our business but git's to look into it. As we need not to touch this it is hidden from us.

As you created a repository let's add/move any file of your choice into the folder to which you created the git repository before (I added a text file name file1.txt to my current folder 'newFolder'). Now your folder has changed. Previously it was empty, but now it has a file in it. Hence, you need to make git trace this change. To do that you should add the file using the command,
git add test.py

Now the file which was only in your local machine is added to the Staging area(index).

Index is a file maintained by Git that contains all of the information about what files and changes are going to go into your next commit.

If you want to get information on current working tree, you can use git status.
And to make a commit which means save the changes you have done, all you need to do is,
git commit -m 'Commit Message'
Here add a meaningful commit message stating what you have done related to the new changes.

Tracking Files

Any Git project consists of three sections;

  1. Git Directory: contains the history of all the files and changes that were made
  2. Working Tree: contains the current state of the project
  3. Staging Area: contains the changes that are marked to be included in the next commit.

Each time you make a commit Git record a new snapshot of the state of your project at that moment. Combination of these snapshots make the history of your project. Files of a project can either be tracked or untracked. Tracked files are part of the snapshots. Untracked are not.
Each track file can be in one of these three stages:

  1. Modified: file has changes (adding, modifying, or deleting) which have not yet committed.
  2. Staged: files which has changes that are ready to be committed
  3. Committed: files whose changes are safely stored in snapshots in git directory.

To get the current configuration you can use git config -l.

git log command will give the commit history.

That's pretty much on starting with Git. For you to know more about staging area, removing files and other things feel free to continue on to the next tutorial. ๐Ÿ˜ƒ

Top comments (2)

Collapse
 
ashwin1729 profile image
Ashwin Dhuriya

It was the best explaination and everything is clear to me . Thank you for this article๐Ÿ˜‡

Collapse
 
chathurashmini profile image
Chathumi Kumarapeli

Glad that it helped you๐Ÿ˜ƒ