Back to Git & Dev Tools
2026-03-307 min read

Version control with Git (Git & Dev Tools)

Learn Version control with Git (Git & Dev Tools) step by step with clear examples and exercises.

Title: Version Control with Git (Git & Dev Tools)

Why This Matters

Version control is an essential tool for developers as it enables them to manage changes made to their codebase, collaborate effectively, and maintain a history of modifications. Git, an open-source distributed version control system, has become the industry standard due to its robust features and ease of use. In this lesson, we will delve into understanding Git, setting up a local repository, basic commands, common mistakes to avoid, and best practices for effective collaboration.

The Importance of Version Control

Version control allows developers to:

  • Track changes made to their codebase over time
  • Collaborate effectively with other developers
  • Revert back to previous versions if errors occur
  • Maintain a history of modifications for accountability and auditing purposes

Prerequisites

Before diving into Git, it's essential to have some familiarity with the command line interface (CLI) and basic programming concepts such as files, directories, and text editors.

Command Line Interface (CLI) Basics

The CLI is a text-based user interface that allows users to interact with their operating system by entering commands. Familiarity with the CLI will help you navigate your system more efficiently and perform various tasks related to Git.

Navigating Your File System

Understanding how to navigate your file system using commands like cd (change directory), ls (list files), and touch (create a new file) is crucial for working with Git effectively.

Core Concept

Git is a distributed version control system that allows developers to track changes made to their codebase, collaborate, and manage multiple branches and repositories. It operates on three main areas: the working directory, the staging area, and the local repository.

  1. Working Directory: This is where you create, modify, and delete files. All your changes are made here.
  2. Staging Area (Index): After making changes in the working directory, you can select specific changes to be committed by adding them to the staging area.
  3. Local Repository: Once you stage changes, they are added to the local repository, which stores all the commits made to the project.

Git also allows for collaboration by pushing and pulling changes from remote repositories hosted on platforms like GitHub, Bitbucket, or GitLab.

Distributed Version Control Systems (DVCS)

Unlike centralized version control systems where all changes are stored in a single repository, Git is a distributed version control system (DVCS). This means that every developer has a complete copy of the repository on their machine, allowing for offline work and faster collaboration.

Worked Example

Let's create a simple Git repository and perform some basic operations:

  1. Initialize a new Git repository in your project directory using the command git init.
$ cd my_project
$ git init
Initialized empty Git repository in /home/user/my_project/.git/
  1. Create a new file called readme.md and make some changes to it.
$ touch readme.md
$ nano readme.md
  1. Stage the changes made to readme.md using the command git add readme.md.
$ git add readme.md
  1. Commit the staged changes with a commit message using the command git commit -m "Initial commit".
$ git commit -m "Initial commit"
[master (root-commit) 123abc] Initial commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.md
  1. Make more changes to the readme.md file and stage them using git add.
$ nano readme.md
$ git add readme.md
  1. Commit the staged changes with a new commit message using git commit.
$ git commit -m "Updated readme"
[master 456def] Updated readme
1 file changed, 1 insertion(+), 1 deletion(-)
  1. To view the commit history, use git log.
$ git log
commit 456def (HEAD -> master)
Author: Your Name <your.email@example.com>
Date: Thu Jan 13 12:00:00 2022 +0530

Updated readme

commit 123abc (initial-commit)
Author: Your Name <your.email@example.com>
Date: Wed Jan 12 11:00:00 2022 +0530

Initial commit
  1. To view the differences between commits, use git diff.
$ git diff HEAD~1 HEAD
diff --git a/readme.md b/readme.md
index 6e54a2c..3970d3b 100644
--- a/readme.md
+++ b/readme.md
@@ -1,2 +1,3 @@

My Project

-This is my project's README file.

+This is my project's README file with updates.

+

+More updates to the README file.


9. To view the status of your files and staging area, use `git status`.

$ git status

On branch master

Changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git restore ..." to discard changes in working directory)

modified: readme.md

no changes added to commit (use "git add" and/or "git commit -a")


10. To stage all changes, use `git add .`.

$ git add .


11. To view the status after staging, use `git status`.

$ git status

On branch master

Nothing to commit, working tree clean

Common Mistakes

  1. Forgetting to add changes: Failing to stage changes before committing can lead to unintentionally discarding modifications. Always remember to use git add before git commit.
  2. Ignoring merge conflicts: When merging branches, Git may encounter conflicts that require manual resolution. Ignoring these conflicts can result in inconsistencies and errors in the codebase.
  3. Not using branching effectively: Branching is crucial for maintaining a clean and organized codebase during collaboration. Failing to create and manage branches appropriately can lead to confusion and merge conflicts.
  4. Misunderstanding Git's distributed nature: Understanding that every developer has a complete copy of the repository can help prevent common mistakes like overwriting changes or losing commits when collaborating.
  5. Not setting up proper access controls: Failing to set up appropriate access controls on remote repositories can lead to unauthorized modifications and data loss.

Subheadings under Common Mistakes:

  • Forgetting to commit staged changes
  • Ignoring merge conflicts during merging branches
  • Misusing branching strategies (e.g., using master for development)
  • Failing to handle conflicts properly when they arise
  • Neglecting access controls on remote repositories

Practice Questions

  1. How do you initialize a new Git repository in your project directory?
  2. What happens when you stage changes using git add?
  3. Explain the difference between the working directory, staging area, and local repository in Git.
  4. How can you view the commit history of your project?
  5. What should be done to resolve merge conflicts when merging branches in Git?
  6. What is a distributed version control system (DVCS), and how does it differ from centralized version control systems?
  7. What are some common mistakes developers might make when using Git, and how can they avoid them?
  8. Why is it important to set up proper access controls on remote repositories?
  9. What command do you use to view the status of your files and staging area in Git?
  10. How do you stage all changes in a directory at once using Git?

FAQ

  1. Why is version control important for developers?

Version control allows developers to manage changes made to their codebase, collaborate effectively, and maintain a history of modifications.

  1. What are the benefits of using Git compared to other version control systems?

Git's distributed nature, robust features, and ease of use make it an industry standard for version control. It also allows for efficient collaboration and branching.

  1. How can I collaborate with others using Git?

To collaborate, you can push your local repository to a remote repository hosted on platforms like GitHub, Bitbucket, or GitLab, and then pull changes from other developers' repositories.

  1. What happens when I run git init in my project directory?

Running git init initializes a new Git repository in the specified directory, creating a hidden .git folder that stores all the necessary files for version control.

  1. Why is it important to stage changes before committing them?

Staging changes allows developers to selectively choose which modifications to commit, ensuring that only intended changes are recorded in the commit history.

  1. What is a merge conflict, and how can I resolve it?

A merge conflict occurs when Git encounters conflicting changes in the same file or lines during a merge operation. To resolve a merge conflict, you must manually edit the affected files and mark the resolution using Git's merge tools.

  1. What is branching in Git, and why is it important?

Branching allows developers to work on separate versions of a codebase without affecting the main development line (usually called master). This enables parallel development, feature isolation, and easier collaboration.

  1. Why is it important to set up proper access controls on remote repositories?

Setting up appropriate access controls helps prevent unauthorized modifications, data loss, and potential security breaches when collaborating with others on a shared repository.

  1. What command do you use to view the status of your files and staging area in Git?

The git status command displays the current status of your files and staging area in Git.

  1. How do you stage all changes in a directory at once using Git?

To stage all changes in a directory at once, use the command git add .. This will stage all modified, deleted, and new files in the current directory and its subdirectories.

Version control with Git (Git & Dev Tools) | Git & Dev Tools | XQA Learn