Getting Started with Git

Ali Hamed
Tech Blog
Published in
6 min readDec 25, 2022

What is Git? Is it important? Should I start using it? And many other questions came to mind everyone heard the word “Git”. This article tries to answer many of your questions.

Reference: marwanwahbi.com

Git is a version control system widely used for software development and other version control tasks. It allows developers to easily manage and track changes to their source code and collaborate with other team members on a project.

Git uses a distributed version control system, meaning every developer has a local copy of the entire repository on their computer. This allows developers to work independently and make changes to their local copy of the code without affecting other team members. When a developer is ready to share their changes, they can “commit” them to their local repository and then “push” them to the remote repository, where other team members can access them.

Git also provides powerful tools for managing and merging multiple codebase versions. Developers can create separate “branches” of the code to work on different features or bug fixes and then merge their changes into the main codebase when they are ready. This allows teams to work efficiently and avoid conflicts on large, complex projects.

In short, Git is an essential tool for any software development team and is a valuable skill for any developer to have in their toolkit.

git logo

How to use Git?

The following are the steps to start using Git:

Step 1 — Install Git on your computer.

You can download the latest version from the official Git website (https://git-scm.com/).

Git home page

Step 2 — Create a local repository

This is done by running the git init command in the terminal or command prompt.

git init

This will initialize an empty Git repository in the current directory.

Usually, this is done inside the folder containing your project.

Opening bash
Executing git init command

The only thing you will notice when you do this is the word master appears next to the file name, as shown in the previous image. However, if you enable showing hidden files, you will see a new folder appear in your directory named git with a dot at the beginning.

Git folder created

The .git folder is hidden to prevent accidental deletion or modification of the folder. The version history of the code base will be lost if this folder is deleted. This means we will not be able to roll back changes made to the code in the future.

Step 3 — Add some files to your local repository

You can add files and check their status by using the git status command.

git status
Executing git status command

You can notice that the file names are in red. This indicates one of two things, the first is the addition of a new file that did not previously exist, and the other is a modification to a file that was present, but in this case, there is the word “modified” before the file name.

Step 4 — Add the files to your local repository

This could be done by using the git add command.

git add .

This command stages the files for the next commit. It will also add all the files. In case you need to add a specific one only you can use this command:

git add <filename>

Now if we run the git status we can see that the color of the file names has changed from red to green.

Executing git add command

Step 5 — Commit files

Now, you should commit the files to your local repository by using the git commit command:

git commit -m "your message"

This saves a snapshot of the files in your local repo, with a message describing your changes.

The message here is just to add some notes for you or for others to know in brief what changes were added to the project.

Note that only the files in the green color will be committed and here is the purpose of the “git add .” or “git add <filename>”

Step 6 — Connect to a remote repository

To connect your local repository to a remote one (i.e., GitHub) you can use the git remote add command:

git remote add <remote-name> <remote-url>

Creating a repository on GitHub

Creating a repository on GitHub requires three steps:

First, open your GitHub and create a new repository.

GitHub profile

Next, Configure your project.

Creating a new repository on GitHub

Finally, copy the <remote-url> and you are ready to go.

Coping the repository remote URL

Step 7 — Pushing changes to the remote repository

Finally, the all changes should be synchronized with the remote repository. This is done using the git push command.

git push -u origin master

After doing that refresh the GitHub page and you can see your files and folders.

Files synchronized with GitHub

Other Git commands

Now, I want to talk about some of the most important commands as well, all of which are used permanently, but with different scenarios.

git clone

git clone <remote-url>

This command is used if we want to take and save project files related to someone else, regardless of the reason for doing so. Or withdraw our files after we, regardless of the reasons, lost these files on our computer.

All you have to do is choose where to download and save these files and then execute the command.

You can find the <remote-url> here:

git clone

Note: When you use this command, you will not have to add the <remote-url> because this is done automatically and is placed on the same <url> that you copied. But if you want to change that, you can use the command and do so.

git pull

This command is for:

git pull <remote> <branch>

Pull the latest changes from the specified remote repository and branch.

git log

To see the commit history for the repository you can use this command:

git log

Note: If you are stuck in an endless cycle of repeating the same commit history, this usually happens to Windows users. All you need to do is press the letter Q.

git branch

To see the list of local branches in the repository.

git branch

To create a new branch:

git branch <branch_name>

git checkout

To switch to the specified branch.

git checkout <branch_name>

Summary

In this article, I illustrated how to install and get started with Git, how to synchronize local and remote repositories, and then mentioned some essential git commands.

If you want to learn more you can find all Git documentation on this website.

--

--