Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basic and Essential Commands

Updated
4 min read
Git for Beginners: Basic
 and Essential Commands

Introduction

In modern software development, version control is an essential skill. One of the most widely used tools for version control is Git. It helps developers track changes in their code, collaborate with other developers, and maintain a history of their project.

Git was created in 2005 by Linus Torvalds, the creator of the Linux kernel. He developed Git to manage the development of the Linux kernel more efficiently. Today, Git is used by millions of developers and organizations worldwide.

Working Directory (files)
        |
     git add
        v
Staging Area (index)
        |
    git commit
        v
Local Repository (commits & branches)
   /           \
 feature      main
   \           /
    git merge
        |
    git push <--> git pull / fetch
        |
Remote Repository (origin)

What is Git?

Git is an open-source distributed version control system that allows developers to track and manage changes in their source code. Unlike traditional version control systems, Git allows every developer to have a complete copy of the repository on their local machine.

This makes collaboration easier and allows developers to work on different features without interfering with each other's work.


Why Developers Use Git

Developers use Git for many reasons:

  • To track changes in source code

  • To collaborate with other developers

  • To manage different versions of a project

  • To revert to previous versions if something goes wrong

  • To maintain an organized workflow in development projects


## Git Basic Working Workflow

Understanding the basic workflow of Git helps developers manage their code efficiently.

The Git workflow mainly consists of three stages:

1.  **Working Directory**  
    This is the area where you create or modify files in your project.
    
2.  **Staging Area**  
    Files are added to the staging area using the `git add` command. This prepares the files to be committed.
    
3.  **Repository**  
    The repository stores all the committed changes and maintains the complete history of the project.
    

Basic workflow example:
Working Directory → Staging Area → Git Repository
      |                 |                 |
   Edit files       git add          git commit

When working with remote platforms like GitHub, developers also use:

git push → Upload changes to remote repository
git pull → Download latest changes from remote repository

Essential Git Commands

1. git init

The git init command is used to create a new Git repository in a project folder. After running this command, Git starts tracking changes in that directory.

Example:

git init

2. git status

The git status command shows the current state of the repository. It displays modified files, untracked files, and files that are ready to be committed.

Example:

git status

3. git add

The git add command adds files to the staging area before committing them to the repository.

Example:

git add

This command stages all modified files.


4. git commit

The git commit command records changes to the repository along with a message describing the update.

Example:
git commit -m "Initial commit"

git commit -m "Short description"  # create the commit

5. git push

The git push command uploads local commits to a remote repository such as GitHub.

Example:
git push origin main

git push -u origin main

6. git pull

The git pull command fetches and merges changes from a remote repository to your local repository.

Example:
git pull origin main

git pull origin main      # fetch and merge origin/main into current branch
git pull                      # fetch and merge the default upstream branch
git pull --rebase origin main # fetch and rebase current branch on origin/main
git pull --ff-only origin main# only update if fast-forward is possible

7. git clone

The git clone command creates a copy of an existing repository from a remote source.

Example:
git clone repository-url

git clone <repo-url> my-dir

Conclusion

Git is a powerful and essential tool for developers. By learning the basic and essential Git commands, beginners can manage their projects more efficiently and collaborate with other developers with ease.

With regular practice, Git becomes an important part of a developer’s workflow and helps maintain an organized and reliable code history.