Getting Started

In this blog, We’ll try to learn about Version Control and why it is so important before you start working on bigger projects. So, Version Control is a way of keeping track of changes that are made to a file or a set of files over a while.

Especially, if you’re collaborating on a project with other programmers, Version Control can be very helpful. It ensures that everyone is working on the same version of the code and It helps you to see a history of changes, who made them, and when they were made. It can easily revert to a previous version if needed. This also prevents people from working on the same issue, which saves more time.

There are various Version Control Systems (VCS) available, but Git is the most popular and is most widely used by programmers around the globe.

Now, we’ll directly jump into What is Git?

What is Git?

Git Logo

Git - fast, scalable, Distributed Revision Control System

Git is a free and open-source tool that keeps track of changes that you make to your code. It’s like having a time machine for your code!

Distributed Revision Control System (DRCS) is a type of VCS that enables developers to work on the same codebase independently and without relying on a central server.

Every time you make changes to a code, Git takes a snapshot of your code and saves it locally, which can be reverted if you needed it.

Git, GitHub - two different things

Git is a version control system that helps you manage your code and track changes locally on your computer. Whereas GitHub, on the other hand, is a web-based platform that provides hosting services for Git repositories, making it easier for developers to collaborate, share their work, and contribute to open-source projects.

Installing Git

Installing Git on Debian-based Linux

sudo apt install git-all

Installing Git on Arch-based Linux

sudo pacman -S git

Installing Git on Windows

1. Download the Git installer from their website.

Installer

2. Or if you have winget tool installed in your PC, just run this command

winget install --id Git.Git -e --source winget

3. Download the latest version (2.40.0) which is suitable for the configuration of your PC.

4. Open the setup wizard after downloading.

Setup

5. Specify the destination folder of your choice, where you would like to install Git.

Setup

6. Let the default or recommended settings be for other options. At last, select Install to install Git on your PC.

Setup

7. After the completion of the installation process, open a git bash and type the following command to check whether Git is installed properly.

git --version

Staging, Committing, Pushing & Pulling

Staging

To understand these concepts in a better way imagine you are shopping online

  1. Staging: Staging is like preparing a shopping cart before checkout. You add items to your cart to decide what you want to buy. Like ways in git, staging is similar; you select specific changes or files that you want to include in your next “purchase” (commit). It’s like deciding which changes you want to save in your history.

  2. Committing: Committing is like completing your purchase. It’s like checking out your shopping cart and finalizing your purchase. Your changes are now saved in the Git history with a message explaining what you changed. It’s a snapshot of your work at that specific moment in time.

  3. Pushing: Pushing is like sharing your shopping list with others. When you push in Git, you are sending your committed changes to a central server (like GitHub) so that others can see and access your work. It’s like sharing your shopping list with friends or family, so they know what you bought.

  4. Pulling: Pulling is like updating your shopping list with new items. When you pull in Git, you’re fetching changes made by others (like your friends or family adding new items to the shopping list) and bringing those changes into your local copy of the project. It helps you keep your work up-to-date with what others have done.

I hope this example made you understand these concepts well.

Basic Git commands

  1. Setup git: Sets the global configuration for the user’s name in Git
    git config --global user.name “<firstname lastname>”
    
    git config --global user.email “<valid-email>”
    
  2. For Initializing a new Git repository in current directory
    git init
    
  3. Cloning a existing git repository in your local machine
    git clone <url>
    
  4. Stages a specific file
    git add <file_name>
    
  5. Stages all the changes in the current directory
    git add .
    
  6. Committing all new changes which are previously staged

    git commit -m "<commit message>"
    
  7. Displays list of commits that are made
    git log
    
  8. Shows the current state of the files in the repo whether they are committed, staged or untracked

    git status
    
  9. Pushes the committed changes to the remote repository named origin in main branch

    git push origin main
    
  10. Linking your local repository to a remote repository for pushing and pulling changes to your Github account

    git remote add origin <repo_link>
    
  11. Pushes the committed changes to the remote repository named origin in main branch
    git push -u origin main