DEV Community

Cover image for Git and GitHub Essentials: Understanding the Basics
John
John

Posted on

Git and GitHub Essentials: Understanding the Basics

Git is a version control system used to manage and keep track of changes in software code. The changes can be hosted on a web platform i.e. GitHub.

These two tools are essential for developers as changes are made in code daily. A basic introduction to git is well outlined here. To perform actions in git, commands are run in a command-line interface known as Git Bash.

By the end of this article, a beginner should be able to make a commit and upload it onto a remote repository i.e. GitHub.

These are the basic terminologies used in git:

  1. Repository(repo) — a central location where Git stores all the files, folders, and version history of a project.
  2. Remote repository — a repository hosted on a server or online(web-based) like GitHub where the version history can be saved and interacted with.
  3. Local repository — a copy of the git repository that resides on the local machine. It contains a git directory(hidden file), commit histories, files, and folders.

There are also some basic commands in git that can be used to make a commit and upload it onto a remote repository — where colleagues can interact with the project. Interaction can be done by analyzing the project and probably making changes and saving the changes made.

Here is a beginner-friendly walkthrough to learn the process of making commits and pushing a project onto a remote repository i.e. GitHub:

Step 1: Create a folder and save it

We assume this folder will contain files of our project and our intention is to track the changes we will make to the project and host them on GitHub — where other colleagues can interact with it.

I will name my folder “First_project”. You can add a file of choice including a .txt file.

Close the folder and navigate to Git Bash.

Step 2: Create a git directory(hidden file) in the saved folder

To do this, navigate into the folder you have created through the command line by;

cd "the_file_path_to_the_folder"
Enter fullscreen mode Exit fullscreen mode

In my Git Bash environment, it will resemble this;

Image description

To confirm you are working in the directory, you can use the command;

pwd
Enter fullscreen mode Exit fullscreen mode

The command prints the current directory.

Image description

In the current directory, create a git repository(hidden file) in the folder. This is done by initializing using a command;

git init
Enter fullscreen mode Exit fullscreen mode

In my command line;

Image description

Step 3: Add the files from the folder that you wish to track

This is done through a command;

git add "name_of_the_file"
Enter fullscreen mode Exit fullscreen mode

In my command line;

Image description

Step 4: Check whether the file(s) have been added & ready for commit

Run a command;

git status
Enter fullscreen mode Exit fullscreen mode

In my command line;

Image description

To do this, use the command;

git commit -m "include_a_message"
Enter fullscreen mode Exit fullscreen mode

The message is a short description of why the commit is made.
Image description

Step 6: Create an empty remote repository on GitHub

On your GitHub account, create a new repository, where the files will be hosted on the remote repository.

You will be directed to this window after creating the repo successfully;

Image description

Copy the link displayed, that will be used to create a link between git and the remote repository we created.

Step 7: Create a connection to the remote repo

Use the command:

git remote add origin <the_link_copied_from_github_repo>
Enter fullscreen mode Exit fullscreen mode

On Git Bash:

Image description

This creates a connection to the remote repository we created on GitHub.

Step 8: Push the files to GitHub

After creating the connection, push the files to the remote repository by;

git push -u origin
Enter fullscreen mode Exit fullscreen mode

In Git Bash, a successful push command will appear as;

Image description

The files are now reflected on the remote repository in GitHub. To confirm this, you can open the remote repository on GitHub and view the files here

They will appear as:

Image description

Follow the steps above to track and manage changes in files through git and GitHub.

Top comments (0)