Git Introduction with real-time example

Priya Salvi
4 min readSep 11, 2023

--

Git is a distributed version control system (DVCS) widely used in software development to track and manage changes to source code and other text-based files. It was created by Linus Torvalds in 2005 and has since become one of the most popular version control systems in the world. Git provides a structured and efficient way for multiple people to collaborate on a software project, track changes, and maintain a history of code modifications.

Here are some key concepts and features of Git:

  1. Version Control: Git allows developers to track changes in their codebase over time. It maintains a history of every change made to the project files, making it possible to roll back to previous versions if needed.
  2. Distributed: Git is a distributed version control system, which means that every developer has a complete copy of the repository on their local machine. This allows for offline work and faster access to history.
  3. Branching: Git’s branching model is lightweight and efficient. Developers can create branches to work on new features or bug fixes independently of the main codebase. Branches can be merged back into the main branch when the work is complete.
  4. Collaboration: Git enables collaboration among multiple developers. They can work on different branches and merge their changes into a central repository, allowing for concurrent development without conflicts.
  5. Commit: In Git, a commit represents a specific set of changes to the codebase. Developers create commits to save their work and document what changes were made. Each commit has a unique identifier (SHA-1 hash) and a commit message explaining the changes.
  6. Remote Repositories: Git supports remote repositories hosted on servers, such as GitHub, GitLab, and Bitbucket. Developers can push their local changes to a remote repository, allowing for centralized collaboration and backup.
  7. Pull Requests and Code Reviews: Git hosting platforms like GitHub and GitLab offer features like pull requests and code reviews. These allow developers to propose changes, discuss them, and review each other’s code before merging.
  8. Merge and Rebase: Git provides options for integrating changes from one branch into another, such as merging and rebasing. These mechanisms help maintain a clean and linear history.
  9. Conflict Resolution: When multiple developers make conflicting changes to the same file, Git helps identify and resolve these conflicts, ensuring that code modifications do not conflict with each other.
  10. History and Log: Developers can use Git commands to view the history of changes, inspect commit messages, and track the evolution of the codebase over time.
  11. Tagging: Git allows developers to create tags to mark specific points in the project’s history, such as releases or important milestones.

Git is a fundamental tool in modern software development, enabling teams to work collaboratively, manage code changes effectively, and maintain a detailed history of their projects. It is widely used across various industries and is an essential skill for software developers and teams.

Let’s walk through a real-time example of how Git is used in a typical software development scenario:

Scenario: A team of software developers is working on a web application called “TaskManager.” They use Git to manage the source code for the project collaboratively.

Step 1: Setting Up the Repository

  1. Creating the Repository: One team member creates a new Git repository on a Git hosting platform like GitHub, GitLab, or Bitbucket. Let’s say the repository is named “TaskManager.”
  2. Cloning the Repository: Other team members clone the repository to their local machines using the git clone command. This creates a local copy of the project.

Step 2: Development Workflow

  1. Creating a New Feature: Developer A wants to add a new feature to the TaskManager application — let’s say it’s a “Task Priority” feature. They create a new branch for this feature using git checkout -b feature/task-priority.
  2. Working on the Feature: Developer A makes changes to the codebase, adding functionality related to task priorities.
  3. Committing Changes: As they work, Developer A creates several commits using git commit to save their progress. Each commit has a meaningful message describing the changes made.
  4. Pushing to the Remote Repository: Once the feature is complete, Developer A pushes their branch to the remote repository using git push origin feature/task-priority. This makes the branch and its changes available to other team members.
  5. Creating a Pull Request (PR): Developer A creates a pull request on the Git hosting platform to request that their changes be reviewed and merged into the main branch (usually master or main).

Step 3: Code Review and Collaboration

  1. Code Review: Other team members review the code changes in the pull request. They can leave comments, suggest improvements, and discuss the code.
  2. Continuous Integration (CI): The project may be configured with a CI system (e.g., Jenkins, Travis CI) that automatically builds and tests the code when changes are pushed. This ensures that the new feature does not introduce errors.

Step 4: Merging the Feature

  1. Merging the PR: Once the code is reviewed and approved, a team member with merge permissions merges the pull request into the main branch.
  2. Closing the Branch: After the merge, the feature branch is no longer needed and can be deleted locally and remotely.

Step 5: Keeping the Repository Up to Date

  1. Regularly Updating: Team members regularly update their local repositories using git pull to fetch the latest changes from the remote repository.

Step 6: Handling Conflicts

  1. Conflict Resolution: Occasionally, two developers may make conflicting changes to the same part of the codebase. Git helps identify these conflicts, and developers work together to resolve them.

Step 7: Tagging Releases

  1. Tagging Releases: When a new version of the TaskManager application is ready for release, the team can create a Git tag (e.g., v1.0) to mark the specific commit associated with that release.

This example illustrates a simplified software development workflow using Git. Git enables collaboration, version control, code review, and a structured approach to managing changes in a team setting. Each step ensures that code changes are tracked, reviewed, and integrated into the project systematically, helping maintain code quality and reliability.

--

--

Priya Salvi

Software Engineer | Oracle Certified Java Associate | Neophile | Bibliophile