DEV Community

Cover image for Git for Beginners: Basics and Essential Commands
Anoop Rajoriya
Anoop Rajoriya

Posted on

Git for Beginners: Basics and Essential Commands

What is Git?

Git is an open-source Distributed Version Control System (DVCS). Created in 2005 by Linus Torvalds (the creator of Linux), it was built to handle massive projects with high speed and efficiency.

Why do you need it?

In simple terms, Git is a "time machine" for your project. Developers use it to:

  • Track Changes: See exactly what was changed, when, and by whom.
  • Collaborate: Work on the same codebase with multiple people simultaneously.
  • Manage History: Revert back to a previous version if something breaks.

Whether you are working alone or in a big team, Git ensures your project history is safe and organized.


Git Concepts & Terminologies

To master Git, you must understand how it organizes your work. Here is the breakdown of the main "zones" and key terms:

Diagram showing the main three zones of git

The Main Zones

  1. Working Directory: Your local folder where you actively edit files.
  2. Staging Area (Index): The "waiting room." Files go here when they are ready to be saved.
  3. Local Repository: A hidden .git folder on your machine that stores your project's history.
  4. Remote Repository: A version of your project hosted online (e.g., GitHub) for sharing and backup.

Essential Terms

  1. Commit: A "snapshot" of your project. Each commit has a unique ID and a message explaining the changes.

  2. Branch: A parallel workspace. It lets you develop features without breaking the main code.

  3. Merge: Combining changes from one branch into another (e.g., merging a "feature" branch into "main").

  4. HEAD: A pointer that marks your current location in the project history. Think of it as a "You Are Here" sign.


Essential Git Commands

I have grouped these commands logically so you can learn them step-by-step without feeling overwhelmed.

Diagram showing the essential terms of Git

1. Setup & Initialization

Use these commands to configure your identity and start new projects.

# Set your name for all your commits
git config --global user.name "Your Name"

# Set your email address
git config --global user.email "yourname@example.com"

# Turn a local folder into a Git repository
git init

# Download an existing project from a URL (like GitHub)
git clone [url]
Enter fullscreen mode Exit fullscreen mode

2. Staging & Snapshots

These are the commands you will use most often during your daily coding.

# show modified files in your working directory
# staged files for your next commit
git status

# add file in staging area for next commit
git add [file]
# you can use . to move all untraked files into staging area
git add .

# removing files from staging area 
# while retaining the changes in working area
git reset [file]

# shows the difference of what is changed but not staged
git diff

# shows the difference of what is staged but not yet commited
git diff --staged

# commit your staged content as a new commit snapshot
git commit -m "[descripted message]"

# a power user shortcut, it staged and commit the tracked files
git commit -am "[descripted message]"
Enter fullscreen mode Exit fullscreen mode

3. Branching & Merging

Branches allow you to work on new features safely without breaking the main project.

# List all local branches
git branch

# Create a new branch
git branch [branch-name]

# Switch to a specific branch
git checkout [branch-name]

# Combine another branch's history into your current one
git merge [branch-name]
Enter fullscreen mode Exit fullscreen mode

4. Inspecting History

Use these to look back at what has happened in your project.

# show the commit history of the current active branch
git log

# show the commits on branchA that are not on branchB
git log branchB..branchA

# show the commits that change file, even across renaming
git log --follow [file]

# show the differenc of what is in branchA that is not in branchB
git diff branchB...branchA

# show any object in Git in human-readable formate
git show [SHA]
Enter fullscreen mode Exit fullscreen mode

5. Sharing & Updating

These commands help you sync your code with remote platforms like GitHub.

# add git url as an alias
git remote add [alias] [url]

# fetch down all the branches from that Git remote
git fetch [alias]

# merge the remote branch to your current branch to bring it up to date
git merge [alias]/[branch]

# transmit local branch commits to the remote repository branch
git push [alias] [branch]

# fetch and merge any commit from the tracking remote branch
git pull
Enter fullscreen mode Exit fullscreen mode

Your First Git Workflow

Let’s put everything into practice. We will create a simple project, make some changes, and save our progress.

Diagram showing the workflow cycle with git

1. Create and Initialize Your Project

Open your terminal and create a new folder. This will be your Working Directory.

# Create the folder
mkdir my-first-git-project

# Enter the folder
cd my-first-git-project

# Initialize Git (You only do this once per project)
git init
Enter fullscreen mode Exit fullscreen mode

2. The "Stage and Commit" Cycle

Think of this as the heartbeat of your development process.

Step A: Create a file

echo "Hello Git!" > hello.txt
Enter fullscreen mode Exit fullscreen mode

Step B: Check the status

Run git status. You will see that hello.txt is "untracked." This means Git sees the file but isn't watching it yet.

Step C: Stage and Save

Move the file to the Staging Area and then save your snapshot.

# Stage the file
git add hello.txt

# Save the snapshot
git commit -m "Initial commit: Create hello.txt"
Enter fullscreen mode Exit fullscreen mode

3. Making and Reviewing Changes

What happens when you edit your code? Let’s add more text to our file.

# Update the file
echo "Learning Git is fun!" >> hello.txt

# See exactly what changed before saving
git diff
Enter fullscreen mode Exit fullscreen mode

If you are happy with the changes, repeat the cycle:

git add .
git commit -m "Update hello.txt with a new sentence"
Enter fullscreen mode Exit fullscreen mode

4. Reviewing Your Progress

To see the "time machine" in action, look at your project history.

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Each line represents a point in time you can return to if something goes wrong. You’ve just successfully tracked your first project!


Git might feel like a lot to learn at first, but you don't need to memorize every commands to be productive. Master the add - commit - push flow first and rest will comes with practice.

Remember: version control isn't just about saving code, its about the freedom to experiment without fear of breaking your project.

What’s Next?

In the next article, we’ll cover:

  • How Git work internally and how git tracks changes
  • Watching the objects/, refs/, HEAD inside .git folder
  • Learning concepts like Blobs, Tree, Commit

👉 Follow, Like & Share if this helped you learning Git Basics & Essential Commands


Top comments (0)