Git Introduction

Git Introduction

What is Git?

Git is a distributed Version Control System (VCS) for managing files in folders (Repository/Repo). The file change history is saved using a series of commits.

By using Git we can make several changed versions of a file without duplicating the existing file or Save As. For example, in programming, we create a login feature. Usually, we will create the program file in the 📁 login folder and the folder is in the repository:

  • 📁 applications (repositories)
    • ⬇🔵: “feat: initialize project” (commit)
    • ⬇🔵: “feat: added view for login and register” (commit)
    • ⬇🔵: “feat: create controller user” (commit)
    • ⬇🔵: “fix: fix error in email input format” (commit)
    • ⬇🔴⟼ Peek at saved information:
      • Commit: 6bebc5658521d98f3eeadb42362e43bc072f0273
      • Author: User
      • Date: Tue Jun 28 2022 18:53:48 GMT+0700 (Western Indonesia Time)
      • Messages: feat: create authentication and authorization models
    • ⬇🔵: …
    • 📁 login

It can be seen that the login folder has been monitored and changes are saved as a series of commits (commit) by Git as changes/revisions are made. A series of commits is a version of each change that occurred (snapshot). If we open one of the commit points, we will see some important information when we make changes to each version, namely:

  • Commit: hash/ID as a marker for each commit so that it can be tracked.
  • Author: the person who did the commit (this will be useful when we work with a team).
  • Date: time of commit
  • Messages: messages from each commit, this is very important because it will tell you what has been done in each commit

Here are some things that differentiate Git from other VCS:

Snapshots, Not Differences

Conceptually, most VCS save a set of changes file-based or what is usually referred to as delta-based version control

Deltas

Deltas (Source: git-scm.com)

However, Git does not store its data this way. Git treats its data as a series of snapshots. So, every time you commit a project, Git will take a snapshot of all files and save it. If the file has not changed, Git will not save the file again, it will only link to a file that is identical to the previous file it saved. This will create a snapshot stream. So doing one commit means we create one snapshot

Snapshots

Snapshots (Source: git-scm.com)

This is an important difference between Git and other VCS. This makes Git more like a mini file system with some tools rather than just a regular VCS.

Nearly Every Operation Is Local

Git has the entire project history on local disk, this makes most operations in Git very fast. How did it happen? 😲

For example, to browse the history (history) of changes from a project, Git does not need to go to the server to get the history and display it, Git simply reads it directly from the local database. This means seeing the history of the project almost instantly.

The interesting thing is that a lot can be done offline. For example, when we get on a plane or train and want to do a little work, we can commit (commit) easily until we get an internet connection and upload it to the server.

Git Has Integrity

We will not lose any information during transit or get file corruption without being detected by Git because the mechanism Git uses for checksumming is hash SHA-1. This is a 40-character string consisting of hexadecimal characters (0–9 and a–f) and is calculated based on the contents of a file or directory structure in Git. Hash example:

24b9da6552252987aa493b52f8696cd6d3b00373

We’ll see this hash value all over the place in Git. Git stores everything in its database based on its hash value.

Git Generally Only Adds Data

When doing activities in Git, almost everything is just adding data to the Git database. Like any VCS, Git can lose or break uncommitted changes, but once you commit them to Git, it’s very difficult to lose them, especially if you regularly upload them to a repository on a server, such as Github. or Gitlab.

The Three States

The main thing to remember is that Git has 3 states where files are: modified, staged, and committed:

  • Modified means we change (add, edit, delete) file, but it has not been saved permanently to the repository
  • Staged means that we mark the modifications we make to file to be saved permanently to the repository
  • Committed means the data is safe and stored in the repository

This brings us to the three main parts of a Git project: Working tree, Staging area, and Git directory.

Areas Areas (Source: git-scm.com)

Install Git

Mac And Linux

For Mac and various Linux distros, Git is usually installed automatically. You can check this by running git --version in the terminal.

If you don’t find Git on your Linux distro, please check [this] link (https://git-scm.com/download/linux) for the installation guide according to the distro you are using.

Windows

For Windows, you can install Git by downloading the installation here.

After installing you can use Git Bash or Command Line Window to use Git.

Basic Config

First-Time Git Setup

After the Git installation is complete, we can do several things to adjust the environment in Git. This tool is called git config which allows us to set variable configurations (setting) for all aspects of the appearance and operation of Git. Among them:

  • ~/.gitconfig file: This is specific only to the user in question. We can make Git read and write to this file specifically by providing the --global option.

  • config file in the git directory (.git/config) or whatever repository is being used: This is specific to that repository only. Each config at the level closest to the code (source code) will always override the config that has been set at the upper level.

We can see all the settings and where they come from using the command:

[user@localhost]$ git config --list --show-origin

Git global setup

The first thing to do after installing Git is to set up a username and email address. This is important because every commit with Git will use this information.

[user@localhost]$ git config --global user.name "Aditira Jamhuri"
[user@localhost]$ git config --global user.email "[email protected]"

Editors

Now that the identity is set, we can configure the default text editor to use when Git needs it. If not configured, Git will use the default editor. If we want to use a different text editor, such as VSCode, we can do the command:

[user@localhost]$ git config --global core.editor "code --wait"

More details for settings text editor are here

Default branch name

By default, Git will create a branch named master when we create a new repository with git init. From Git version 2.28 onwards, we can set a different initial branch name. For example, we can change the initial branch name to main:

[user@localhost]$ git config --global init.defaultBranch main

Checking Your Settings

If we want to check settings, we can use the git config --list command to see a list of all existing settings:

[user@localhost]$ git config --list
user.name=Aditira
[email protected]
color. status=auto
color. branch=auto
color.interactive=auto
color. diff=auto
...

We can also see value with certain keywords, by typing git config <key>, for example:

[user@localhost]$ git config user.name
Aditya
Back to top