# Creating Git and Github.

## Introduction.

Git and GitHub are essential tools for modern software development. Git is a version control system that helps developers track changes, collaborate with others, and maintain the history of their projects. GitHub is a web-based platform that uses Git for version control and provides additional features like collaboration, issue tracking, and project management. This guide will walk you through setting up Git, creating a repository, making commits, and performing basic Git operations like pushing and pulling.

## Git terminology.

**Repository (repo):** The directory, located at the top level of a working tree, where Git keeps all the history and metadata for a project. Repositories are almost always referred to as repos. A bare repository is one that isn't part of a working tree; it's used for sharing or backup. A bare repo is usually a directory with a name that ends in .git for example, project.git.

**Commit:** When used as a verb, commit means to make a commit object. This action takes its name from commits to a database. It means you are committing the changes you have made so that others can eventually see them, too.

**Branch:** A branch is a named series of linked commits. The most recent commit on a branch is called the head. The default branch, which is created when you initialize a repository, is called main, often named master in Git. The head of the current branch is named HEAD. Branches are an incredibly useful feature of Git because they allow developers to work independently (or together) in branches and later merge their changes into the default branch.

**Remote:** A remote is a named reference to another Git repository. When you create a repo, Git creates a remote named origin that is the default remote for push and pull operations.

**Commands, subcommands, and options:** Git operations are performed by using commands like `git push` and `git pull`. `git` is the command, and `push` or `pull` is the subcommand. The subcommand specifies the operation you want Git to perform. Commands frequently are accompanied by options, which use hyphens (-) or double hyphens (--). For example, `git reset --hard`.

## Setting Up Git.

**Installing Git** To start using Git, you need to install it on your system. Here are the installation steps for different operating systems:

**Windows**: Download the installer from [Git](https://git-scm.com/downloads) for Windows, run it, and follow the installation instructions.

**macOS**: Install Git using Homebrew by running the command `brew install git` in your terminal.

**Linux**: Install Git using your distribution’s package manager. For example, on Ubuntu, you can run `sudo apt-get install git`.

Once installed, open your terminal or Git Bash (if you’re on Windows) and run this code: `git --version`

You should see the installed Git version. Congrats, you’re ready to roll!

![git version](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468523070/ff856699-17e5-4dfb-8458-206f8d6cd331.png align="left")

**Configuring Git**

If you do not have a Github account, you will need to create an account. Click [here](https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F&source=header-home) to create a Github account.

Follow the prompts to create an account with your email address.

![Git hub account](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468524236/b597c938-8407-4e71-9088-dafdcd0eb2d7.png align="left")

You need to configure Git with your user information. Open your terminal and run the following commands:

`git config --global user.name "Your Name" git config --global user.email "you@example.com"` These commands set your name and email, which will appear in your commits.

## Creating Your First Repository.

**Initializing a New Repository**

To create a new Git repository, navigate to your project directory in the terminal and run:

`git init`

This command initialises a new Git repository in the current directory, creating a .git folder that contains all the necessary files for version control. PS: Note that the .git folder is hidden.

![git init](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468525666/8bcac883-9252-45f8-b7d1-91ff95222216.png align="left")

Great! You’ve just created a Git repository.

## Adding Files to the Repository:

Create some files (e.g. `index.html`, `style.css`, `script.js` etc)

You can use a Linux command such as `touch` to create a file or use Visual Studio code (VSC) editor. In this article we will use VSC.

* From the terminal (Git Bash), create a directory named *GitLab* using the command `mkdir GitLab`
    

![mkdir](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468527146/9e95b04d-7947-4528-862c-92b882d5b4b6.png align="left")

In Git bash, type `code .` to open Visual studio code editor.

* Create your files `index.html`, `style.css`, `script.js`.
    
* Add the block of codes below to the *index.html* file you just created.
    

**HTML code**

```bash
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Website</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header>
        <h1>Welcome to My Simple Website</h1>
        <nav>
            <ul>
                <li><a href="#home">Home</a></li>
                <li><a href="#about">About</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul>
        </nav>
    </header>
    <main>
        <section id="home">
            <h2>Home</h2>
            <p>This is the home section of the website.</p>
        </section>
        <section id="about">
            <h2>About</h2>
            <p>This is the about section of the website. Here you can find information about this website.</p>
        </section>
        <section id="contact">
            <h2>Contact</h2>
            <p>This is the contact section of the website. Feel free to get in touch!</p>
            <button onclick="showAlert()">Click Me</button>
        </section>
    </main>
    <footer>
        <p>&copy; 2024 Simple Website. All rights reserved.</p>
    </footer>
    <script src="scripts.js"></script>
</body>
</html>
```

**CSS (styles.css)**

```bash
body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

header {
    background-color: #4CAF50;
    color: white;
    padding: 15px 20px;
    text-align: center;
}

header nav ul {
    list-style-type: none;
    padding: 0;
}

header nav ul li {
    display: inline;
    margin: 0 10px;
}

header nav ul li a {
    color: white;
    text-decoration: none;
}

main {
    padding: 20px;
}

main section {
    margin-bottom: 20px;
}

footer {
    background-color: #333;
    color: white;
    text-align: center;
    padding: 10px 0;
    position: fixed;
    width: 100%;
    bottom: 0;
}
```

**scripts.js**

```bash
function showAlert() {
    alert('Button clicked!');
}
```

![VSC](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468528575/744886c7-2b87-41b9-90b4-903814499ace.png align="left")

## Adding the created files to the repository

To add the created files to the repository *index.html, style.css, script.js*, run the command `git add .` This can be done using the Git Bash console or from VsCode

![git add .](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468530006/e8f25509-f3d5-4251-90a6-dae7b107dadd.png align="left")

`git add .`command adds all the files and changes in the current directory to the repository. If you want to add specific files you use `git add "filename"`'

To check the status of your staged files you use `git status`.

![git status](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468531029/6ccff7ef-47f6-4cd4-a36b-94fbe28e3a7d.png align="left")

**Committing Changes** Once you have staged your changes, you can commit them to the repository with a descriptive message: `git commit -m "Your commit message"`

The `-m` flag in this command tells Git that you're providing a commit message.

This command creates a snapshot of the staged changes and records it in the repository’s history.

![git commit](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468532241/2a7f1d12-a3cf-4db5-8558-18fd1a7dc215.png align="left")

**Pushing and Pulling**

To push your local commits to a remote repository on GitHub, you first need to add the remote repository: You can create the reposistory first in Github or use the command below `git remote add origin https://github.com/username/Gitlab.git`

Log in to your git account and click **New** to create a new repository

![new repo](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468533862/6abd9994-4b60-49fd-8b8d-80deffd2a517.png align="left")

Enter the *Repository name* and give a description (optional).

Click **Create a repository**

![create repo](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468535051/def960b2-37f7-4d5b-8352-c3730de4238d.png align="left")

![create repo](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468536648/b531a6d7-4e5e-4854-825f-110ade6caecc.png align="left")

Then, push your commits to the remote repository: `git push origin main` Replace *main* with your branch name if you are working on a different branch.

![git push](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468538224/20d00740-41d8-4045-99c9-c1cde3be8ff4.png align="left")

Open Github to view the added files:

![created files](https://cdn.hashnode.com/res/hashnode/image/upload/v1723468539378/5b542e3e-d850-4284-ad7a-eb0788bb8463.png align="left")

**Pulling Changes from GitHub** To fetch and integrate changes from a remote repository, use the following command: `git pull origin main`

This command updates your local repository with the latest changes from the remote repository.

## Additional Git Operations

**Branching** Branches allow you to work on different features or bug fixes independently. To create a new branch, use: `git checkout -b new-branch`

To switch to an existing branch, use: `git checkout existing-branch`

**Merging** To merge changes from one branch into another, first switch to the branch you want to merge into: `git checkout main`

Then, merge the other branch: `git merge new-branch`

**Viewing Commit History**

To view the commit history of your repository, use: `git log`

You can also use `git log --oneline` for a simplified view.

**Resolving Conflicts**

Conflicts may arise when multiple changes are made to the same part of a file. Git will mark the conflicts in the affected files, and you will need to resolve them manually. After resolving conflicts, stage the resolved files and commit the changes.

```bash
git add resolved-file
git commit -m "Resolved merge conflict in resolved-file"
```

**Conclusion**

Git and GitHub are powerful tools that streamline the development process, facilitate collaboration, and ensure that your code is version-controlled and backed up. By following this guide, you should be able to set up Git, create and manage repositories, make commits, and handle basic Git operations.
