Blogs

Git Basics: Learn the Most Used Commands in Minutes!

Introduction

Git is a version control system that lets users track projects changes. Those projects (also called repositories) can be complicated, such as applications source codes, or just as simple as a bunch of text files.

Git is the tool for managing your changes, while GitHub is the web-based hosting service for repositories.

In this blog, we will mimic working on a real-life project to learn how to interact with git. I've already created a repo that you can use to follow along with this tutorial.

The below are some terms you should be familiar with

  1. Branch: You can think of a branch as a copy of your repository (also referred to as a repo).
  2. Upstream: In simple terms, it refers to the repo from which you downloaded your code.

That's enough for an introduction; let's start learning!

Commands and their usages

  • git clone

    This command lets you clone/downlad the repo files locally.

  • git fetch

    This command will fetch all the available branches in a repo.

  • git branch

    This command will list all the available branches in the repo (run this after your do the fetch).

  • git checkout -b {branch-name}

    This command creates a new branch for you; it creates a copy of the current branch before executing the command.

  • git add

    This command will let you specify which files you want to commit/push to your repo. If you want to push all your changes, you can just do git add .

  • git commit

    This command creates a new commit containing the changes you specified in the previous command (git add). One good practice here is to always include a message with each commit. git commit -m "my message goes here"

  • git push {upstream} {branch-name}

    This command is the final step when making your changes; once you push, your changes will be available in the repo. By default, the upstream name is origin.

These are the most used commands and it will be super benificial for you to master them!

Let's now put them into action!

Quick demo

  1. Let's clone a repo
    git clone https://github.com/MumenTayyem/github-basics.git
  2. Navigate to the repo
    cd github-basics
  3. Fetch all available branches
    git fetch
  4. List all available branches
    git branch
  5. Let's now create a new branch
    git checkout -b {change-this-to-your-name} # your branch name will be your name
  6. Make any change to the file called mumen.txt
  7. Specify which changes you want to include in your commit
    git add mumen.txt
  8. Create a new commit
    git commit -m "your message goes here"
  9. Push your changes to the upstream
    git push origin {your-name} # your branch name which you specified in step#5

Summary

In this quick blog, you learned the basic commands to start using git. You then created your branch with your changes and pushed them to the upstream. Hope this was helpful for you!