DEV Community

loading...
Cover image for Getting Started with Git

Getting Started with Git

Christopher Glikpo
A learner and a Youtuber who likes to write Technical Blog. https://www.youtube.com/c/ChristopherGlikpo
・5 min read

Git is an Open Source Distributed Version Control System.
Let me break it down and explain the wording:

  • Control System: This basically means that Git is a content tracker. So Git can be used to store content — it is mostly used to store code due to the other features it provides.
  • Version Control System: The code which is stored in Git keeps changing as more code is added. Also, many developers can add code in parallel. So Version Control System helps in handling this by maintaining a history of what changes have happened. Also, Git provides features like branches and merges, which I will be covering later.
  • Distributed Version Control System: Git has a remote repository which is stored in a server and a local repository which is stored in the computer of each developer. This means that the code is not just stored in a central server, but the full copy of the code is present in all the developers’ computers. Git is a Distributed Version Control System since the code is present in every developer’s computer.

Installation of Git

Installation of Git is straightforward using the installer package available at Git official web site.

  • Download the installer from Git WebSite.
  • Run the installer.
  • Follow the installer steps, agree the license agreement and click the next button.
  • Restart your system/machine

NB:Mac users can install it with brew: brew install git

Now, test Git by printing its version using the following command in Command Prompt:

git --version
# git version 2.31.0.windows.1

Enter fullscreen mode Exit fullscreen mode

Configure Git

Now let Git know who you are. This is important for version control systems, as each Git commit uses this information:

git config --global user.name "James Brown"
git config --global user.email "jamesbrown@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Change the user name and e-mail address to your own. You will probably also want to use this when registering to GitHub later on.

Note: Use global to set the username and e-mail for every repository on your computer.If you want to set the username/e-mail for just the current repo, you can remove global

You can see current global configuration with:

git config --global --list
Enter fullscreen mode Exit fullscreen mode

Creating Git Folder

Now, let's create a new folder for our project:

mkdir myproject
cd myproject
Enter fullscreen mode Exit fullscreen mode

mkdir makes a new directory.

cd changes the current working directory.

Now that we are in the correct directory. We can start by initializing Git!

Note: If you already have a folder/directory you would like to use for Git: Navigate to it in command line, or open it in your file explorer, right-click and select "Git Bash here"

Initialize Git

Once you have navigated to the correct folder, you can initialize Git on that folder:

git init 
Initialized empty Git repository in /Users/user/myproject/.git/
Enter fullscreen mode Exit fullscreen mode

You just created your first Git Repository!

Note: Git now knows that it should watch the folder you initiated it on.Git creates a hidden folder to keep track of changes.

Git Adding New Files

You just created your first local Git repo. But it is empty.

So let's add some files, or create a new file using your favourite text editor. Then save or move it to the folder you just created.

For this example, I am going to use a simple HTML file like this:

<!DOCTYPE html>
<html>
<head>
<title>Getting Started with Git!</title>
</head>
<body>

<h1>Hello world!</h1>
<p>This is the first file in my new Git Repo.</p>

</body>
</html>

Enter fullscreen mode Exit fullscreen mode

And save it to our new folder as index.html.

Let's go back to the terminal and list the files in our current working directory:

ls
index.html
Enter fullscreen mode Exit fullscreen mode

ls will list the files in the directory. We can see that index.html is there.

Then we check the Git status and see if it is a part of our repo:

git status
On branch master

No commits yet

Untracked files:
  (use "git add ..." to include in what will be committed)
    index.html

nothing added to commit but untracked files present (use "git add" to track)
Enter fullscreen mode Exit fullscreen mode

Now Git is aware of the file, but has not added it to our repository!

Files in your Git repository folder can be in one of 2 states:

Tracked - files that Git knows about and are added to the repository
Untracked - files that are in your working directory, but not added to the repository

Git Staging Environment

The ideas of the Staging Environment and the Commit are two of Git's most important features.

You may be adding, modifying, and deleting files while working. However, you should add the files to a Staging Environment whenever you reach a milestone or complete a section of the project.

Staged files are files that are ready to be committed to the repository you are working on. You will learn more about commit shortly.

For now, we are done working with index.html. So we can add it to the Staging Environment:

git add index.html

Enter fullscreen mode Exit fullscreen mode

The file should be Staged. Let's check the status:

git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
    new file: index.html
Enter fullscreen mode Exit fullscreen mode

Now the file has been added to the Staging Environment.We are ready to do our first commit.

Git Commit

We are ready to go from stage to commit for our repo(folder) now that we have completed our work.

As we work, adding commits allows us to keep track of our progress and modifications. Each commit is treated as a "save point" by Git. It's a moment in the project where you can go back and fix an issue or make a modification.

We should always provide a message when we commit.

By adding clear messages to each commit, it is easy for yourself (and others) to see what has changed and when

git commit -m "My First git commit"
[master (root-commit) c7da65d] My first git commit
 1 file changed, 12 insertions(+)
 create mode 100644 index.html
Enter fullscreen mode Exit fullscreen mode

The commit command performs a commit, and the -m "message" adds a message.

The Staging Environment has been committed to our repo, with the message:
"My first git commit"

Git Commit Log

To view the history of commits for a repository, you can use the log command:

git log
commit c7da65d981ce205dfadbeedec4e36a5e1625c558 (HEAD -> master)
Author: jamesbrown <jamesbrown@gmail.com>
Date:   Mon Jul 12 17:34:54 2021 -0700

    My first git commit
Enter fullscreen mode Exit fullscreen mode

If you've reached this point, thank you very much. I hope that this tutorial has been helpful for you and I'll see you all in the next.

If you like my work, please consider
Buy me a coffee
so that I can bring more projects, more articles for you

If you want to learn more about Web Development don't forget to to follow me on Youtube!

Discussion (2)

Collapse
pjotre86 profile image
pjotre86

Hey! Honest question:
Why did you write this up?

Collapse
buraker37215814 profile image
Burak Ertürk

Why u askin? For me it was helpful tbh