Getting started with Git on your machine

Getting started with Git on your machine


How to configure Git

Before git can be configured on your machine, you need to create a GitHub account if you don’t have one.

Creating a GitHub account is pretty easy, just get to the GitHub official website and click the signup button, fill the necessary details, submit and hurray!!! you get an account with GitHub.

To configure git, type the commands below.

git config --global user.name "your-GitHub-username"
git config --local user.name "your full name"
git config --global user.email "your-email-address"
git config --local user.email "your email id"

Basic commands for the command prompt

touch.
mkdir.
cd.
cd ../

What is Github

Github is an online platform that allows developers to synchronise their local repositories into the web so that it can be accessed remotely.

It can also be used to browse through other developers repositories and download source code or document without using git.

The most beautiful feature of git is that it allows multiple developers to work remotely on the same project.

What is a repository

A repository just is a name for a folder that is initialized with git.in order words, it a folder in which git keeps track of its data, newly added files or folders and changes made to the files.

A repository is usually a parent folder containing other folders, nested folders and files.

It is commonly called a Repo by most developers.

How to create a repository

For each project you work on, you need to create a repository to track changes in it. There are two ways to create a GitHub repository

  1. using the command-line interface (CLI).
  2. using the GitHub official website.
  3. In this tutorial, I will show you both methods

How to create a Repository via CLI

For each of these commands press the enter key after typing.cd Desktopmkdir project1cd project1 git init

Using the Github official website

Step 1

Create a GitHub account or sign in if you already have an account.

Step 2

Click on the plus icon on the top right corner of the website after logging in.

step 3

Click on the New repository option.

step 4

 Enter the information required in the create a new repository field and click the Create repository button.

Wooooooooo we have just created our first repository. That was easy right?

Adding files and folders to our repository

In the project1 directory, type the following commands:

touch countries.txt

mkdir hello-world

cd hello-world

touch helloWorld.html

We just created a folder and two files in our repo. Go to your desktop and open project1 folder with your favourite text editor. countries.txt should contain the following:

Nigeria

India

Cameroon

Ghana

helloWorld.html file should contain the following:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <div class="container">
      <h1>Hi can you say hello World in any language?</h1>
    </div>
  </body>
</html>

Now, let’s add these files and folder to git and commit them for tracking of changes.

cd ../
git add .
git commit -m "I added a folder and two files to this project"

N/B Your commit message should describe the changes you have made to the repository to help project maintainers. I will elaborate more on this when we get to open source.

What is happening in this repository

Sometimes we don’t just know what is going on in our repo but we don’t have to worry because git got us covered with these commands

git status
git log

Undoing changes in a repository

we can undo changes either before or after a commit.

Undoing changes before a commit

Now let’s add the data below to our countries.txt

Africa 
America
Europe
Asia

Ooops we just made a mistake these are names of continents and not countries. let’s check what is happening in our repo.

git status

Let’s undo this mistake with git.

git checkout -- countries.txt
cat countries.txt

that was for a specified file. If more than one file is changed, to undo all the change files we will use the command below

git reset --hard

Undoing changes after a commit

Suppose however that you have committed a change and now want to undo that change and revert to the previous version. Let’s repeat the same mistake we made earlier but this time we will get it committed. Add the data below to our countries.txt

Africa 
America
Europe
Asia
git add .
git commit -m "I mistakenly added names of continents to countries.txt"

To see all our committed changes, type the command below

git log

The git log command shows lines saying commit with some apparently random set of letters and numbers.

This is an identifier for a particular commit. We can use git revert with one of these identifiers to remove the change that happened in that commit. Have you seen the random set of letters and numbers in the commits? In practice, you only need about the first 6 or 7 characters of the commit identifier to identify a particular commit uniquely.

git revert can be used to undo any commit

git revert abgd875

Conclusion

At the end of this lesson, we have been able to configure git on our local machine, create our own repository, make changes to our repository and also undo some changes.


Share on social media

//