DEV Community

Cover image for Git for Beginners: Basics and Essential Commands
Abhimanyu Kumar
Abhimanyu Kumar

Posted on

Git for Beginners: Basics and Essential Commands

Introduction

If you are learning web development, later or sooner you will be listen this sentences
"Push your code to GitHub"

But before GitHub there is Git

Git is not just another tool, it is the foundation of software development.
Almost every company, open-source project, and developer uses Git daily.

In this, here we will see all about the Git and their command, which comes to use in the day to day life.

What is Git?

Git is the distributed version control system. It gives your code a memory.

  • Track changes in your code
  • Save different version of your project
  • work with other developers without overwriting each other's work
  • Go back in Time if somethings break

Use of Git

Before git, developer used:

  • Pendrive
  • Email attachments
  • Shared folder
  • Files like final_1, final_1, last_final

This makes overwhelming codes, it become causes of

  • Lost code
  • Overwritten changes
  • No history
  • No collaboration

This all of the problems solved by the version control system, That is GIT

Git Provides you to : -

  • Keeps your full history of changes
  • Shows who changes and what changed
  • Allow collaboration with team
  • Makes mistake reversible

That's why everyone loves Git

Git Basic and some core Technologies

Before using the Git command, let's have some understand the important terms.

1. Repository(Repo)

This is nothing just a folder, where Git is tracking.
It contains:

  • Your project files
  • Git history
  • Git configuration

Once you init a git in your project create on file inside your project, and this files tracks your all the changes and history that you have done.

2. Commit

A Commit is a snapshot of your code at a specific time

Think like this:

  • A unique ID
  • Author name
  • Date & time
  • Message describing the change

3. Branch

A Branch is an independent line of development

  • Main branch - Stable code
  • Other branch - experiments, feature, fixes

4. Head

Head is a pointer to point your current commit.
Head shows you are right now in project history.

5. Git workflows

Three stages are there in Git works

  • Working directory
  • Staging Area
  • Repository

Installing Git

  • Windows: Download from git-scm.com
  • macOS : brew install git
  • Linus : sudo apt install git
  • after installation check version

git --v or git --version

Common Git commands

1. git-init
initilized a new Git repo

git init

2. git status
show your current status of your project

git status

  • Modified files
  • staged files
  • untracked files

3. git add
Adds file to the staging area

git add or git add .

staging means : :I want to include these changes in the next commit.

4. git commit
save changes permanently to the git history

git commit -m "Message"

Always write meaningful commit message

5. git log
shows commit history

git log

you can see:

  • commit history
  • Author
  • Date
  • Message

6. git branch
show all branch

git branch

Create a new branch

git create "branch name"

7. git checkout
switch between branches

git checkout branch-name

8. git checkout -b
create + switch branch in one command

git checkout -b branch-name

Why need to learn version control for every developer

  • Work confidently in teams
  • Debug faster
  • Build professional habits
  • Prepare for Jobs & internship

Conclusion

Git is not just a tool.
it is :

  • A safety net
  • A collaboration System
  • A time machine of your code

Top comments (0)