Basic Git Commands and Online Git Repositories

Basic overview of Git and GitHub

Ishara De Silva
LinkIT

--

Photo by Clément H on Unsplash

From this article, I will discuss some basic Git commands and at the end of this small exercise, you will be able to set up a folder as a Git repository and perform basic Git operations on your Git repository.

Also, you will learn about how to set up and use an online Git repository and synchronize your local Git repository with your online repository. From the exercise I have shared with you, you will be able to set up the online repository as a remote repository for your local Git repository, push your commits to the online repository and clone an online Git repository to your computer.

Basic Git Commands

At a preferred location on your computer, create a folder named git-basics

Open this git-basics folder in your favorite editor.

Add a file named index.html to this folder, and add the following HTML code to this file.

<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>Welcome to the git basics</h1>
</body>
</html>

Initializing the folder as a Git repository

Go to the git-basics folder in your cmd window/terminal and type the following at the prompt to initialize the folder as a Git repository:

git init

Checking your Git repository status

Type the following at the prompt to check your Git repository’s status:

git status

Adding files to the staging area

To add files to the staging area of your Git repository, type:

git add .

Committing to the Git repository

To commit the current working area to your Git repository, type:

git commit -m “this is the first commit”

Checking the log of Git commits

To check the log of the commits to your Git repository, type;

git log — oneline

Now, modify the index.html file as follows:

<!DOCTYPE html>
<html>
<head></head>
<body>
<h1> Welcome to the git basics </h1>
<p>This is a paragraph</p>
</body>
</html>

Add a sub-folder named login to your git-basics folder, and then add a file named login.html to the login folder. Then set the contents of this file to be the same as the index.html file above.

Then check the status and add all the files to the staging area.

Then do the second commit to your repository

Checking out a file from an earlier commit

To check out the index.html from the second commit, find the number of the second commit using the git log, and then type the following at the prompt:

git checkout <second commit’s number> index.html

Resetting the Git repository

To discard the effect of the previous operation and restore index.html to its state at the end of the second commit, type:

git reset HEAD index.html

Then type the following at the prompt:

git checkout — index.html

You can also use git reset to reset the staging area to the last commit without disturbing the working directory.

Setting up an Online Git repository

Sign up for an account at GitHub (https://github.com).

Let’s create a new repository and see how the steps to be done. Set up an online Git repository named git-test. Note the URL of your online Git repository.

Set the local Git repository to set its remote origin

At the prompt, type the following to set up your local repository to link to your online Git repository:

git remote add origin <repository URL>

Pushing your commits to the online repository

At the prompt, type the following to push the commits to the online repository:

git push -u origin master

Cloning an online repository

To clone an online repository to your computer, type the following at the prompt:

git clone <repository URL>

Conclusion

In this article, you have learned basic Git commands and how to set up an online Git repository, synchronize your local repository with the remote repository, and clone an online repository. Hope you enjoyed my article and think this would be helpful when working on Git projects with your teammates.

--

--

Ishara De Silva
LinkIT
Writer for

Former Software Engineering Intern @Aspitio(pvt)Ltd. | IT Undergraduate| Faculty of Information Technology | University of Moratuwa