DEV Community

Cover image for Git for Beginners: Basics and Essential Commands
Kunal
Kunal

Posted on • Edited on

Git for Beginners: Basics and Essential Commands

I am sure you've heard a lot about the the term Git, so in this blog we discuss

Table of Contents

  1. What is Git
  2. Why Git is Used
  3. Git Basics and Core Terminologies
  4. Common Git Commands

So first before we hopped into what is Git we have to discuss Why Git and Why it is so popular?

Why git?

So earlier when we Dont have git we have to share our code via pen drive , Zip file or FTP(File Transfer Protocol) servers.

Diagram Representation

git

As you can see from the above diagram, that problems like

  1. Easy to overwrite someone else's code
  2. Hard to know who changed what
  3. File corruption
  4. No proper version of history
  5. We can't easily go back to previous versions

so, in order to overcome all these issues git arrived.

Git Popularity

Git become so popular because it solved many programmer's problems that faced earlier
First, Git is distributed among all the developers which means every developer has a full copy of project.

Second,Git makes tracking changes very easy, Developers can see who changed the code and what was changed and when it was changed.

Third, Platforms like GitHub made Git more popular by allowing developers to store code online and collaborate from anywhere .

What is Git?

Git is a version control system that helps developers track, manage and save changes in their code. As you can see in the *Diagram *

git

Whenever you edit your code, Git keeps a record of what was changed ( we'll discuss it how it is done).This means you can go back to an older version anytime if something breaks or something goes wrong.

Git also makes it easy for multiple people to work on the same projects. Each person can work on their own copy of the code and later combine their changes safely.

Git is like a history book for your code.

Git Basics and Core Terminologies

In order to learn how Git works and its commands we have to first look into Diagram and some core terminology with real-life examples

git

Repository : A repository is just a folder where your project lives and Git keeps track of everything inside it.

It's like a notebook where every time you edit a page, a copy of the old page is safely saved.

Commit : A commit is like a checkpoint. It is like saving your recent work with a note.

it's like clicking save in a game-you can always come back to the saved point with a note

Branch : A branch is like creating a separate path to work on your code without disturbing the main work.

It's like making a photocopy of your notebook to experiment on, while the original stays unchanged.

Head : Head is like a pointer that tells you where you are right now. Head shows the current place you are working on Git.

It's like a bookmark in a book showing the page you are currently reading.

Common Git Commands

1. git init

This command is used to initialize Git in a project.

git init
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Turns a normal folder into Git repository
  • Git starts tracking files in this folder

2. git status

This command shows the current state of your project.

git status
Enter fullscreen mode Exit fullscreen mode

What it tells you:

  • which files have changed
  • which files are staged
  • what is ready to be committed

3. git add

This command is used to add changes to the staging area.

//for a specific file 
git add <filename> 

// for everything 
git add .
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Selects files you want to save
  • Prepares them for a commit.

4. git commit

This command saves your changes permanently.

git commit -m "initial commit"
Enter fullscreen mode Exit fullscreen mode

What it does:

  • Creates a snapshot of your project.

  • Stores it in Git history with a message.

5. git log

This command shows the history of commits.

git log
Enter fullscreen mode Exit fullscreen mode

What it does :

  • Displays all commits.

  • Shows who changed what and when.

6. git diff

This command shows what exactly changed in your files.

git diff
Enter fullscreen mode Exit fullscreen mode

What it does :

  • Shows the difference between the last commit and the current commit.

  • What you changed but haven't committed yet

7. Git checkout

Used to switch branches or go back to an old commit

Git checkout branch_name
Enter fullscreen mode Exit fullscreen mode

What it does :

  • Moves you to another branch

  • Changes your working files to match that branch

8. git pull

Used to get the latest code from a remote repository.

git pull
Enter fullscreen mode Exit fullscreen mode

What it does :

  • Downloads new changes

  • Updates your local project

9. git push

Used to send your code to remote repository.

git push
Enter fullscreen mode Exit fullscreen mode

What it does

  • Uploads your commits

  • Makes your changes visible to others

Workflow of pushing code to a remote repository

workflow

Thanks for reading ! if enjoyed this blog , you can read more on this πŸ‘‡

Top comments (0)