DEV Community

Cover image for Git: The Tool Every Developer Needs to Know
Nadeem Rashid
Nadeem Rashid

Posted on

1

Git: The Tool Every Developer Needs to Know

Introduction

In software development, keeping track of code changes and managing different versions of a project is crucial. Git is a powerful tool that helps developers:

  • Save progress and revisit previous changes.
  • Collaborate efficiently without overwriting each other’s work.
  • Manage different versions of a project easily.

With Git, multiple contributors can work on the same project simultaneously. Additionally, platforms like GitHub allow developers to store their projects online, making remote collaboration seamless.

This guide covers Git fundamentals, essential commands, and how to use Git with remote repositories. By the end, you will have a solid foundation for managing code effectively.

Things We Will Learn in This Blog

In this blog, we will dive into the key concepts and commands of Git to help you manage your code and collaborate with others. Here’s what you’ll learn:

  • What Git is and why it’s important for version control and collaboration.
  • How to check if Git is installed and how to install it if needed.
  • How to configure Git with your name and email to track commits.
  • Key Git terms such as repositories, commits, branches, merges, and remotes.
  • Git workflow and how changes flow through different stages (working directory, staging area, and local repository).
  • Basic Git commands for initializing repositories, staging, committing, and pushing changes.
  • How to work with remote repositories (e.g., GitHub) for collaboration and managing versions.
  • How to undo changes and fix mistakes using Git’s powerful commands.
  • How to work with branches to create new features or fixes without affecting the main project.

By the end of this blog, you'll have a comprehensive understanding of Git and how to use it to manage your projects effectively.

What is Git and Why Use It?

Git is a distributed version control system (DVCS) that helps developers manage code efficiently. It allows you to track changes, collaborate with others, and maintain different versions of a project without losing progress.

Key Benefits of Git:

  • Track Changes – Keep a complete history of modifications.
  • Collaboration – Work smoothly with multiple developers.
  • Branching & Merging – Experiment with new features without affecting the main project.
  • Backup & Recovery – Revert to previous versions if needed.
  • Industry Standard – Used by companies like Google, Microsoft, and major open-source projects.

By using Git, teams can work together efficiently while keeping their code organized and secure.

Checking if Git is Installed

Before using Git, check if it's already installed on your system. Open a terminal or command prompt and run:

git --version
Enter fullscreen mode Exit fullscreen mode

If Git is installed, it will display something like below

git version 2.45.0.windows.1
Enter fullscreen mode Exit fullscreen mode

If the command does not work, Git is not installed on your system

Installing Git

You can download and install Git from the official website:
Download Git

Configuring Git

After installing Git, you need to set up your username and email. This information is used to track your commits and associate them with you.

Set Your Name and Email

Run the following commands in your terminal or command prompt:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Enter fullscreen mode Exit fullscreen mode

Verifying Your Git Configuration

To check if your details were saved correctly, run:

git config --list
Enter fullscreen mode Exit fullscreen mode

This will display a list of all configured settings. You should see your name and email in the output, like this:

user.name=Your Name  
user.email=your.email@example.com
Enter fullscreen mode Exit fullscreen mode

Basic Git Terms

  • Repository (Repo) – A folder where Git is initialized to track changes in a project.
  • Commit – A saved version of your project at a specific point in time.
  • Remote Repository – A version of your repository stored online (e.g., GitHub, GitLab).
  • Staging Area – An intermediate area where changes are gathered before they are committed to the repository. It allows you to prepare and review changes before finalizing them in a commit.
  • Branch – A separate line of development that allows you to work on changes without affecting the main project. Useful for adding new features or fixing bugs.

Braching

  • Merge – Combining changes from two different branches into one. For example, adding new features from a feature branch to the main project branch.
  • Clone – Copying a repository from a remote source to your local machine.
  • Pull – Getting the latest changes from a remote repository to update your local copy.
  • Push – Sending your local changes to a remote repository to share with others.

Understanding Git Workflow

Git follows a three-stage process to track and manage code changes efficiently. This workflow ensures that every change is tracked, reviewed, and stored safely before being shared.

The main stages of Git workflow are:

  • Working Directory – The folder where your project exists and where you modify files.
  • Staging Area - A temporary space where changes are prepared before committing. This allows you to review modifications before permanently saving them in the local repository.
  • Local Repository – A hidden .git directory on your computer where committed versions are stored.
  • Remote Repository – A version of your repository hosted on platforms like GitHub or GitLab, used for collaboration.

Git Workflow

Basic Git Workflow

  1. Initialize a repository: git init

  2. Clone an existing repository: git clone <repo-url>

  3. Stage changes:

    • git add <file-name>
    • git add . (to add all files)
  4. Commit changes: git commit -m "Your message"

  5. Push changes to remote: git push origin <branch-name>

  6. Pull the latest changes: git pull origin <branch-name>

Essential Git Commands

1. Setup & Initialization

Action Command Description
Configure Git git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Set up Git with your name and email.
Initialize a repo git init Create a new local repository.
Link repo to GitHub git remote add origin <repo-url> Connect local repo to GitHub.

2. Tracking & Committing Changes

Action Command Description
Check status git status See modified/untracked files.
Stage all changes git add . Add all modified files to staging.
Stage a specific file git add <filename> Add a specific file.
Commit changes git commit -m "Your message" Save changes locally.

3. Pushing Code to GitHub

Action Command Description
Push to GitHub git push -u origin main Upload commits to GitHub.
Push to a different branch git push origin <branch-name> Push changes to a specific branch.

4. Pulling & Updating Code

Action Command Description
Fetch latest changes git pull origin main Update local repo from GitHub.
Clone a repository git clone <repo-url> Download a copy of an existing GitHub repo.

5. Undo & Fix Mistakes

Action Command Description
Undo last commit (keep changes) git reset --soft HEAD~1 Remove the last commit but keep changes.
Revert to last committed state git checkout -- <filename> Undo changes in a specific file.
Reset all uncommitted changes git reset --hard Remove all local changes. ⚠ Be careful!

6. Branching & Merging

Action Command Description
Create a new branch git branch <branch-name> Create a new branch.
Switch branches git checkout <branch-name> Move to another branch.
Create & switch to a new branch git checkout -b <branch-name> Create and switch to a new branch.
Merge branches git merge <branch-name> Merge a branch into the current branch.

Conclusion

Git is an essential tool for every developer, enabling efficient version control and seamless collaboration. By mastering its core concepts and commands, you can streamline your workflow, keep your projects organized, and ensure smooth collaboration.

This blog covered Git fundamentals—from installation to essential commands for tracking, committing, and pushing changes. We also explored working with remote repositories, managing branches, and fixing mistakes. With these skills, you’re ready to manage your codebase and collaborate effectively.

Start using Git today to level up your development process!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay