DEV Community

Cover image for Understanding Git
Adil Shahzad
Adil Shahzad

Posted on

Understanding Git

Git overview

Git is a Free and Open-Source Distributed Version Control System. Git’s purpose is to keep track of projects and files as they change over time from different users.
A repository is committed to the project, and the HEAD refers to the current commit viewing in the repository.

All this information related to the project is stored in the .git/ folder, usually known as a git repository, which is mainly hidden by default in the project directory. So basically, Git keeps track of changes made by the developers on the project they are working on. Also, developers can work on the different branches, which helps them add more features to the project to resolve bugs, and when they are done with the changes, they can merge the changes to the default branch, which is the main branch. These techniques help the developers to track down bugs or software failures with the help of commits made to the project.

What is HEAD?

HEΑD is a pointer, which αlwαys points to the latest commit in the branch. Whenever a user commits to the project repository, HEAD is updated with the latest commit. The heads of the branches are stored in the .git/refs/heads/ directory.

What is a git repository?

A Git repository is a .git/ directory inside a project which tracks and saves all the history and changes made to the files or folders in a git repository.

If you delete the .git/ folder, you delete your project history.

Git uses a Distributed Version Control System to track all changes made to the project and save them into the repository. Users can then delete or copy the existing repository or create a new one for the current projects.

Types of git repository

Bare repository

The software Development team uses Bare Repositories to share changes. Individual users cannot modify or create new versions of the repository.

non-Bare repository

In non-Bare repositories, users can easily modify the existing repository and create a new version. By default, the cloning process creates a non-bare repository.

Git workflow

These are the Git stages (workflow)

  1. modified
  2. staged
  3. committed

modified

Whenever users make changes to the files, git cannot track those changes. We must move the file to the staged area using the git add command to track the changes.

staged

On the other hand, in the stage, files modifications are tracked by git, part of the snapshot.

committed

The changes made into the file successfully become a part of the latest snapshot.

Git Workflow

First, when we add the file to the working directory, the file is usually untracked, and to track the file, we need to stage the file; for this, we use the git add command, and when the file followed, we need to commit on the file with the following command git commit. When we edit the file, the file will be again in the untracked stage, and we need to stage and commit to the file. We can also reset the staged area to untracked the file that we tracked accidentally. Let us get started with git by installing it first.

Git installation

To get started with Git, Download the Git concerning your operating system. Once installation is complete, you can check the git version with the following command after installation is completed.

git --version

Enter fullscreen mode Exit fullscreen mode

Git configuration

The purpose of the git configuration is to configure user information used across all local repositories. The username and email are associated whenever the user commits changes to the repository.

git config --global user.name "[firstname lastname]"

Enter fullscreen mode Exit fullscreen mode

After the name configuration on git CLI, configure the email with the following command.

git config --global user.email "youremail@yourdomain.com"

Enter fullscreen mode Exit fullscreen mode

Once done, you can confirm that the information is set by running:

git config --list

Enter fullscreen mode Exit fullscreen mode

Using git for a new project

Let’s see how we can set up a new project using Git.

Initializing the repository

First, we need to create a new directory.


mkdir <git_basic>

Enter fullscreen mode Exit fullscreen mode

Now, we need to navigate to the directory which we created earlier.


cd <git_basic>
Enter fullscreen mode Exit fullscreen mode

Run the following command to initialize an empty git repository inside the working directory.


git init

Enter fullscreen mode Exit fullscreen mode

After this command, a .git/ folder will be initialized to the working directory; you can use the following command to list the hidden files and folders.

ls -A
Enter fullscreen mode Exit fullscreen mode

Git status

Git status is used to show the level of file changes as untracked, modified, or staged.
Let us create the first file that we will be tracking using git. Make Sure still in the git_basic directory terminal. To create new files in the current directory from the terminal, run the following command:


touch README.md
Enter fullscreen mode Exit fullscreen mode

.md extension is used for the Markdown Language. Markdown is a lightweight markup language with plain-text-formatting syntax.

Use the following command to open the file in the vim editor.


vim README.md
Enter fullscreen mode Exit fullscreen mode

Once in the Vim editor, press I from the keyboard to start editing. When done with editing, press the ESC button. The command to save a file in Vim and quit the editor is :wq. As soon as the file is saved, the state of the repository will be changed. The status of the file can be easily tracked by running the following command:

git status

Enter fullscreen mode Exit fullscreen mode

The git status command does not change or update anything. Instead, it prints out which files are modified, staged, or untracked. To stage the file, we need to learn about staging and committing.

staging and committing

staging

Before committing, we must tell git what files we want to (untracked, modified, or deleted) to the project directory. To track the git working directory files, we use the git add command. Why do we need to do this? Why can't we commit directly? Let us work on two files simultaneously but want to commit only one completed file, so in this situation, the "git add" command helps stage the file. Even deletion of files can also be tracked in the git history, so deleted files must be staged and committed.

Run the following command on Git Bash to stage the files or folders.


git add . 
Enter fullscreen mode Exit fullscreen mode

in the git add . dot can be used to simultaneously track all the project changes.

Alternatively, sometimes only we need to stage one file.


git add <filename>

Enter fullscreen mode Exit fullscreen mode

The folder can also be staged using the following command.


git add <myfolder>

Enter fullscreen mode Exit fullscreen mode

After the add command, you can check the status of the file, whether it's staged or not, using the git status command.

unstaged Files

Sometimes you accidentally staged the files, so with this command, you can unstaged the files from the git staging area.


git reset [file]

Enter fullscreen mode Exit fullscreen mode

commit

Commits work as a snapshot or milestone along the timeline of git progress. Commits are created directly with the git commit command after the staging of the files. Git snapshots are always committed to the local directory.

Use the following command to commit changes.


git commit -m "Message that describes what this change does."

Enter fullscreen mode Exit fullscreen mode

TIP: For commit messages, do not use the past tense, such as "I made a change in the file " use language like "making navbar blue" as you are giving orders to the git.

Fixing your last commit

If you made a mistake in your last commit message, run this command.


git commit --amend -m "Put your correct message here."
Enter fullscreen mode Exit fullscreen mode

View a List of Commits

You can view the latest and previous commit details using the following command

git log

Enter fullscreen mode Exit fullscreen mode

You can also use the following command to see a simplified list of commits.

git log --online
Enter fullscreen mode Exit fullscreen mode

To see a list of commits with even more detail (including which files changed), run this command.

git log --stat
Enter fullscreen mode Exit fullscreen mode

Note: if the commit list is long, you can use the UP/ Down Arrow key and press the q button from the keyboard to exit from the terminal.

What have we learned so far?

  • To Initialize Git Repository, we use git init
  • To Stage all the files, we use git add .
  • To Commit the changes, we use git commit -m "You Message"

Every time you modify, delete or add the files, you have to use the git add command and then commit your changes to the git repository using the git commit command.

Summary

This lesson is essential for the basic understanding of Git. The main takeaways are the three states that a file can be:

Modified: To modify a file on the Working Directory.

Staged: To add the file to the Staging Area, so it could be snapshotted.

Committed: Snapshot of the entire project (the unmodified and staged files).

If a file was part of the previous commit and did not modify it, it will automatically be part of the next commit. A modified but unstaged file is considered unmodified. Git will not automatically track the changes, as you have to ask git to follow them by staging those files.
After staging the files, we also learned about committing to the changes.
In the next lesson, we will explore more about advanced concepts of Git such as branches, merge, tags etc.

If you have any question, please feel free to connect with me on LinkedIn.

Top comments (0)