# Git Introduction

Git is the most popular version control system used by developers around the world. It helps track changes in your code, collaborate with others, and manage project versions efficiently. Whether you're working solo or as part of a team, Git ensures your work is organized and never accidentally lost.

In this blog, we'll walk you through the following:

* **Understanding Git basics.**
    
* **Installing Git on Ubuntu.**
    
* **Creating a GitHub repository.**
    
* **Pushing the** `index.html` **file** [**that was shown in this blog**](https://blog.ceruleancloud.ca/deploy-static-resume-on-an-apache-server) **to a GitHub repository.**
    

### **Introduction to Git**

### **What is Git?**

Git is a distributed version control system designed to manage changes in files, especially code, efficiently. It allows developers to:

* Track changes over time.
    
* Collaborate with multiple people without conflicts.
    
* Revert to previous versions if needed.
    

### **Why Use Git?**

* **Versioning:** Always have access to previous versions of your code.
    
* **Collaboration:** Multiple developers can work on the same project simultaneously.
    
* **Backup:** Your codebase is stored safely in remote repositories like GitHub.
    

### **Installing Git on Ubuntu**

To use Git, you need to install and configure it on your machine.

To follow along with the rest of this blog, I am assuming the following:

* **You have a** [**Github**](https://github.com/) **account**
    
* **You have a working Ubuntu environment**
    

**Update the Package Manager**

Open your terminal and update the package manager to ensure you have the latest package information:

```plaintext
sudo apt update
```

**Install Git**

Install Git using the following command:

```plaintext
sudo apt install git
```

**Verify the Installation**

Check if Git is installed correctly by running:

```plaintext
git --version
```

You should see something like:

```plaintext
git version 2.x.x
```

**Configure Git**

Set up your identity, which will be attached to your commits. This will be your github password and email.

```plaintext
git config --global user.name "Your Name"  
git config --global user.email "youremail@example.com"
```

To confirm your configuration, run:

```plaintext
git config --list
```

### **Creating a GitHub Repository**

**GitHub is a cloud-based service that hosts Git repositories. Here’s how to create one:**

**Log in to GitHub**

Go to Github and log in or sign up for an account.

**Create a New Repository**

Click on the **+** icon in the top-right corner and select **New Repository**.

1. Provide a repository name (e.g., `my-resume-site`).
    
2. Choose visibility:
    
    * **Public:** Anyone can view it.
        
    * **Private:** Only you and your collaborators can view it.  
        ***PS: I recommend using Private***
        
3. Check “Initialize this repository with a README” option.
    
4. Click **Create repository**.**4\. Adding and Pushing Your** `index.html` File
    

Now that your GitHub repository is ready, let’s add and push your `index.html` file to it.

### **Create a Local Project Directory**

1. Move to your home directory
    
    ```plaintext
    cd ~
    ```
    
2. Create a new folder for your project and move into it:
    
    ```plaintext
    mkdir my-resume-site 
    cd my-resume site
    ```
    

### **Create the** `index.html` File

1. Create a simple `index.html` file:
    
    ```plaintext
    vi index.html
    ```
    
    Copy paste the index.html file contents from [this blog](https://blog.ceruleancloud.ca/deploy-static-resume-on-an-apache-server) to this file and save it.
    
2. Confirm file exists
    
    ```plaintext
    cat index.html
    ```
    

### **Initialize Git**

Initialize Git in your project directory:

```plaintext
git init
```

### **Stage Your File**

Add the `index.html` file to Git’s staging area:

```plaintext
git add index.html
```

### **Commit Your Changes**

Save your changes to Git with a meaningful message:

```plaintext
git commit -m "Initial commit: Add index.html"
```

### **Link to Your GitHub Repository**

Link your local repository to the GitHub repository you created earlier. Replace `<yourusername>` and `<reponame>` with your GitHub username and repository name:

```plaintext
git remote add origin https://github.com/<yourusername>/my-resume-site.git
```

### **Push Your Code to GitHub**

Push your code to the main branch of your GitHub repository:

```plaintext
git branch -M main  
git push -u origin main
```

### Recap on what we did so far

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1734801058605/5785230a-2b03-4db8-885d-32abc627cf17.png align="center")

### **Verifying Your Upload**

1. Open your GitHub repository in a browser.
    
2. You should see your `index.html` file.
    
3. Click on the file to view its contents.
    

### **Going further**

Now that your code is on GitHub, you can:

* Update the file and push changes using `git add`, `git commit`, and `git push`.
    
* Clone the repository to other machines using `git clone`.
    
* Collaborate with others by inviting them to your repository.
    

### Git Cheatsheet

The below table shows most commands that a developer would use in development while using Git.

| **Command** | **Description** | **Example Usage** |
| --- | --- | --- |
| `git init` | Initialize a new Git repository | `git init` |
| `git clone <repo_url>` | Clone a repository | `git clone` [`https://github.com/user/repo.git`](https://github.com/user/repo.git) |
| `git status` | Show the status of your working directory | `git status` |
| `git add <file>` | Stage changes to be committed | `git add file.txt` |
| `git add .` | Stage all changes in the current directory | `git add .` |
| `git commit -m "<message>"` | Commit staged changes with a message | `git commit -m "Added new feature"` |
| `git push` | Push commits to the remote repository | `git push origin main` |
| `git pull` | Fetch and merge changes from the remote | `git pull origin main` |
| `git branch` | List branches | `git branch` |
| `git branch <branch_name>` | Create a new branch | `git branch feature-branch` |
| `git checkout <branch>` | Switch to a branch | `git checkout feature-branch` |
| `git checkout -b <branch>` | Create and switch to a new branch | `git checkout -b feature-branch` |
| `git merge <branch>` | Merge a branch into the current branch | `git merge feature-branch` |
| `git log` | Show commit history | `git log` |
| `git log --oneline` | Show concise commit history | `git log --oneline` |
| `git diff` | Show changes in tracked files | `git diff` |
| `git diff <branch1> <branch2>` | Compare two branches | `git diff main feature-branch` |
| `git stash` | Temporarily save changes without committing | `git stash` |
| `git stash pop` | Apply the last stashed changes | `git stash pop` |
| `git reset <file>` | Unstage a file | `git reset file.txt` |
| `git reset --hard <commit>` | Reset working directory to a specific commit | `git reset --hard abc1234` |
| `git remote -v` | Show remote repositories | `git remote -v` |
| `git remote add <name> <url>` | Add a remote repository | `git remote add origin` [`https://github.com/user/repo.git`](https://github.com/user/repo.git) |
| `git tag <tag_name>` | Create a tag for a specific commit | `git tag v1.0.0` |
| `git fetch` | Fetch changes from remote without merging | `git fetch origin` |
| `git rebase <branch>` | Reapply commits on top of another branch | `git rebase main` |
| `git cherry-pick <commit>` | Apply a specific commit to the current branch | `git cherry-pick abc1234` |

By mastering Git, you’ll streamline your development workflow and enhance collaboration on any project.

Happy coding!
