git simplified – part 1

Post Written by
Marko Marjanović
Last modified on July 1st, 2020 at 2:39 pm

Doing any coding without some kind of versioning control system ( VCS ) is nearly impossible today. Thanks to his creator, Linus Torvalds, we have a great distributed system that is pretty easy to start with, however very hard to master. Think of GIT as a tool that you will be constantly learning as you go, but don’t get discouraged, we will going to learn the most basic parts of git that will help you in you everyday work. In essence, GIT is a tool for tracking changes in the given directory, allowing saving state (snapshots) of the files as you work on your project.

Repository

Every versioning control system is based around the repositories. Think of repository as a project management tool for tracking changes of your files. Each repository contains commit objects and references to them called heads. Git is smart, however not smart enough to do all the work for you so you need to tell it where to look for the changes and save them when you are satisfied with them. Creating new repository is initiated with command git init from the directory where your project files resides. This command will create hidden directory called .git which contains all configuration files and objects that GIT interact with for the specific repository. In contrast to centralized versioning systems like Subversion or CVS, you can have repository created on your local machine. Let’s create new project and initialize repository:

$ mkdir git-simplified && cd git-simplified
$ git init

States

There are three major stages prior to committing, e.g. saving a snapshot of the current project. When you add a new files to repository, they are still not added to the repository, therefore they are not tracked. New files are marked as untracked. To see the details about working repository we use git status. In order to tell the git to track our file we need issue git add <file name>, or if we want to add all files to repository we can use a dot instead of file name.

$ git add file.txt ( or git add . )

When we add new files to a repository, we are moving them to a staging phase. Only if the files are staged, we can commit the changes, or in other words, save their current state. Once the tracked files are changed, git moves them to modified state.

Changes to be committed:
  (use "git rm --cached ..." to unstage)
        new file:   test.txt
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)
        modified:   test.txt

The important part to understand is that If the files are not staged, they cannot be committed so we need to add them to staging phase once more and then commit them.

$ git add test.txt
$ git commit

When you run git commit, git will automatically open default text editor and ask you to provide a short message. Each commit should contain a message that describe changes made at that time. Saving the file, commit process is completed. We can use a shorthand by providing -m flag to commit command and type a message without opening the text editor.

$ git commit -m “add test file”

Good practise is to check the working repository with git status before and after each git command. Also, if we want to see the changes made to a single file we can use git diff <file name> command, or we can use --staged flag to check the changes made to a staged file. Commits can be inspected running git log command.

commit 9d14fdf930254dc4862012f8c2636d33127dc0a9
Author: m4r35 
Date:   Tue Wed 7 03:31:26 2017 +0100

    add test file

Committed changes are stored in local database in the .git directory as a reference to a state of the files in repository at that moment and each commit is represented with SHA-1 hash value. That being said, the mechanism the git is using provides integrity of the saved snapshots, meaning that they cannot be changed without git being able to detect that. The most important part to understand about states is that if you discard changes to your files by not committing them, they will be lost forever. This is basically the only way you can lose changes in git, however if you regularly commit changes you made, you can be sure that they cannot be lost.

Summary

Distributed versioning system like git stores references to current snapshot of the files in the git directory called repository. There are three major phases involved in the tracking process: modified, staged and committed. Each stage represents the state of the files. Only staged files are ready to be committed. In the next article we are going to see how can we utilize commits to undo changes that we’ve made and some of the best practices of using git more efficiently.

Contact Us

Fill out the enquiry form and we'll get back to you as soon as possible.