Skip to main content

Command Palette

Search for a command to run...

Git Introduction

Updated
6 min read
Git Introduction
C

Cloud | AWS | DevOps | AI 📍 Toronto 🇨🇦 🚀 Cloud Architect @ AWS 👨🏽‍🏫 Professor

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 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 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:

sudo apt update

Install Git

Install Git using the following command:

sudo apt install git

Verify the Installation

Check if Git is installed correctly by running:

git --version

You should see something like:

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.

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

To confirm your configuration, run:

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

     cd ~
    
  2. Create a new folder for your project and move into it:

     mkdir my-resume-site 
     cd my-resume site
    

Create the index.html File

  1. Create a simple index.html file:

     vi index.html
    

    Copy paste the index.html file contents from this blog to this file and save it.

  2. Confirm file exists

     cat index.html
    

Initialize Git

Initialize Git in your project directory:

git init

Stage Your File

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

git add index.html

Commit Your Changes

Save your changes to Git with a meaningful message:

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

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

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:

git branch -M main  
git push -u origin main

Recap on what we did so far

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.

CommandDescriptionExample Usage
git initInitialize a new Git repositorygit init
git clone <repo_url>Clone a repositorygit clone https://github.com/user/repo.git
git statusShow the status of your working directorygit status
git add <file>Stage changes to be committedgit add file.txt
git add .Stage all changes in the current directorygit add .
git commit -m "<message>"Commit staged changes with a messagegit commit -m "Added new feature"
git pushPush commits to the remote repositorygit push origin main
git pullFetch and merge changes from the remotegit pull origin main
git branchList branchesgit branch
git branch <branch_name>Create a new branchgit branch feature-branch
git checkout <branch>Switch to a branchgit checkout feature-branch
git checkout -b <branch>Create and switch to a new branchgit checkout -b feature-branch
git merge <branch>Merge a branch into the current branchgit merge feature-branch
git logShow commit historygit log
git log --onelineShow concise commit historygit log --oneline
git diffShow changes in tracked filesgit diff
git diff <branch1> <branch2>Compare two branchesgit diff main feature-branch
git stashTemporarily save changes without committinggit stash
git stash popApply the last stashed changesgit stash pop
git reset <file>Unstage a filegit reset file.txt
git reset --hard <commit>Reset working directory to a specific commitgit reset --hard abc1234
git remote -vShow remote repositoriesgit remote -v
git remote add <name> <url>Add a remote repositorygit remote add origin https://github.com/user/repo.git
git tag <tag_name>Create a tag for a specific commitgit tag v1.0.0
git fetchFetch changes from remote without merginggit fetch origin
git rebase <branch>Reapply commits on top of another branchgit rebase main
git cherry-pick <commit>Apply a specific commit to the current branchgit cherry-pick abc1234

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

Happy coding!

More from this blog

C

Cerulean Cloud Blog

27 posts

Cerulean Cloud Blog aims to share cloud engineering concepts that work for people of all levels, from beginners to advanced engineers.