Version Control with GIT

M T C Lakshitha
Aeturnum
Published in
8 min readMay 20, 2024

Introduction
In today’s fast-paced world of software development, version control systems are indispensable tools for managing code changes, collaborating with team members, and ensuring the integrity of projects. Among the plethora of version control systems available, Git stands out as one of the most popular and powerful choices. In this comprehensive guide, we’ll take you through everything you need to know to master version control with Git, from the basics to advanced techniques.

Understanding Version Control
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. It allows multiple developers to work on a project simultaneously and keeps track of changes made by each contributor. There are two main types of version control systems.

  1. Centralized system: Like SVN, it has a single central repository.
  2. Distributed system: Like Git, allow every developer to have a full copy of the repository on their local machine.

Introduction to Git
Git, created by Linus Torvalds in 2005, revolutionized version control with its distributed architecture and lightning-fast performance. Unlike centralized systems, Git stores a complete copy of the repository, including its full history, on each developer’s machine. This makes operations like branching and merging incredibly fast and efficient. Git’s core concepts include repositories (stores all versions of a project), commits (snapshots of changes), branches (independent lines of development), and merges (combining changes from different branches).

Pros & Cons in similar solutions

Git Platforms
Git platforms are online services that provide hosting for Git repositories, enabling teams to collaborate on software development projects. These platforms offer a range of features to facilitate code management, collaboration, and project tracking.

  • GitHub
  • GitLab
  • Bitbucket

Getting Started with Git

Installing Git
Installing Git is straightforward and varies depending on your operating system. Visit the official Git website (https://git-scm.com/) for detailed installation instructions for Windows, macOS, and Linux.

Configuring Git
After Installing Git, the first step is to configure it with your name and email address using following commands. You can also set other configurations like default text editor and line endings.

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

Initializing a Git Repository
To start version controlling a project with Git, navigate to the project’s directory in your terminal and run the command.

git init

This creates a new Git repository in the current directory. Alternatively, you can clone an existing repository from a remote source using the command.

git clone <repository URL>

Basic Git Commands
git init
The `git init` command initialize a new Git repository in the current directory.

git clone
The `git clone` command creates a local copy of a remote repository on your machine.

git add
The `git add` command stages changes for the next commit. You can add specific files or directories to the staging area using:

git add <file>

or add all changes using:

git add .

git commit
The `git commit` command creates a new commit with the changes staged in the index. Always accompany commits with meaningful commit messages using the `-m` flag.

git commit -m "Add feature XYZ"

git status
The `git status` command displays the current state of the working directory and staging area. It shows which files are modified, staged, or untracked.

git log
The `git log` command shows the commit history of the repository. You can use options like ` — oneline` for a compact view or ` — graph` for a graphical representation of branches.

Branching and Merging

Introduction to Git Branches
In Git, a branch is a parallel version of a repository’s codebase. It allows you to work on new features, bug fixes, or experiments without affecting the main codebase.

  • Local Branch: A local branch is a branch that exists only on your local machine. When you create a new branch in Git, it’s created locally by default. You can switch between branches, make changes, commit them, and merge them locally without affecting the main repository until you push your changes to a remote repository.
  • Remote Branch: A remote branch is a branch that exists on a remote repository, such as GitHub, GitLab or Bitbucket. Remote branches are shared among team members collaborating on a project. When you push a local branch to a remote repository, it creates a corresponding remote branch.

Creating a Branch
Branching in Git is lightweight and allows you to work on new features or fixes without affecting the main codebase. To create a new branch, use the command:

git branch <branch-name>

This creates a new branch but doesn’t switch to it. To switch to the new branch, use:

git checkout <branch-name>

or combine both steps using:

git checkout -b <branch-name>

Switching Branches
To switch between branches, use the `git checkout` command followed by the branch name.

git checkout <branch-name>

Merging Branches
Merging combines the changes from different branches into one. To merge a branch into the current branch, use:

git merge <branch-name>

Git will automatically perform a fast-forward merge if the branches have diverged or create a new merge commit if necessary.

Resolving Merge Conflicts
Merge conflicts occur when Git cannot automatically merge changes from different branches. Git marks the conflicting areas in the affected files. You need to manually resolve the conflicts by editing the files, marking them as resolved, and committing the changes.

Collaborating with Git
Working with Remote Repositories
Git allows you to collaborate with others by sharing your changes with remote repositories hosted on platforms like GitHub, GitLab, or Bitbucket. You can add a remote repository using:

git remote add <name> <URL>

and push your changes to the remote using:

git push <remote-name> <branch-name>

or pull changes from the remote using:

git pull <remote-name> <branch-name>

Collaborating with Others
Git facilitates collaboration through features like branches and pull requests. Branches allow multiple developers to work on different features simultaneously without interfering with each other. Pull requests are a way to propose and initiate code review before merging them into the main branch.

Pull Request: A Pull Request (PR) is a feature commonly used in Git-based version control system to propose changes to a codebase. It allows contributors to suggest changes, fixes, or new features and request them to be reviewed and merged into the main codebase.

Pull Request Flow

Advanced Git Topics

  • Rebasing
    Rebasing is an alternative to merging that allows you to integrate changes from one branch into another by applying each commit on top of the destination branch. This results in a cleaner commit history compared to merging.
  • Git Workflows
    Git workflows are conventions or patterns for using Git effectively in a development team. Common workflows include GitFlow, GitHub Flow, and GitLab Flow, each with its own set of rules and best practices.
  • Git Hooks
    Git hooks are scripts that Git execute before or after certain actions like committing, merging, or pushing. They allow you to automate tasks or enforce policies in your repository.
  • Git Bisect
    Git bisect is a powerful tool for finding the commit that introduced a bug by performing a binary search through the commit history. It automates the process of checking out different commits and testing for the presence of a bug until the culprit is found.

Recommended GUI tools for Git

Best Practices with Git
Using Git effectively requires following best practices:

Write meaningful commit messages:

  • Commit messages should be descriptive, concise, and meaningful.
  • Examples of bad commit messages: “fixed stuff”, “changes”, “update”, etc.
  • Examples of good commit messages: “Fix bug causing crash when user clicks submit button”, “Add feature to allow users to reset passwords”, etc.

Keep commits small and focuses:

  • Each commit should represent a single logical change. Avoid bundling unrelated changes in one commit.
  • Small commits make it easier to review, understand, and revert changes when needed.
  • If you find yourself making too many changes, consider breaking them into smaller, more focused commits.

Use branches for feature development and bug fixes:

  • Branches allow you to work on new features or bug fixes without affecting the main codebase.
  • Create a new branch for each feature or bug fix. This isolates your changes and makes it easier to manage and review them.
  • Use meaningful branch names that reflect the purpose of the changes (e.g., ‘feature/user-authentication’, ‘bugfix/fix-login-error’).

Regularly fetch and pull changes:

  • Fetching and pulling changes from remote repositories ensures you stay up to date with the latest changes made by your team.
  • Before starting work, always fetch the latest changes from the remote repository (‘git fetch’) and incorporate them into your local branch (‘git pull’).
  • Regularly pulling changes reduces the risk of merge conflicts and keeps your local repository in sync with the remote.

Use Git rebase to keep a clean history:

  • Rebase allows you to rewrite commit history, making it cleaner and more linear.
  • Resolve any conflicts that arise during the rebase process. This ensures a smoother and cleaner merge later.

Use Git hooks for automation:

  • Git hooks are scripts that Git executes before or after events such as commits, pushes, and merges.
  • By automating repetitive tasks with Git hooks, you can ensure consistency and improve the quality of your codebase.

Use descriptive branch names:

  • Branch name should be descriptive and reflect the purpose of the changes.
  • Avoid generic names like ‘feature1’ or ‘fix-bug’. Instead, use names that clearly indicate the feature or bug being addressed. (e.g., ‘user-authentication’, ‘fix-login-issue’).

Use Gitignore to exclude unnecessary files:

  • Create a ‘.gitignore’ file in your repository to specify files or directories that should be ignored by Git.
  • This prevents irrelevant files (such as complied binaries, temporary files, or IDE-specific files) from being tracked in your repository.

Communicate with your team:

  • Keep your team informed about the changes you’re making and any challenges you encounter.
  • Use pull request descriptions, comments, and chat platform to communicate effectively with team members.
  • Collaboration and communication are essential for a smooth and efficient Git workflow

Conclusion
I’ve covered everything from the basic commands to advanced topics like rebasing and Git workflows. By mastering Git, you’ve gained a powerful tool that will streamline your development workflow, enable seamless collaboration, and ensure the integrity of your projects. Keep practicing, exploring, and learning, and you’ll become a Git expert in no time!

Additional Resources

Thank you for taking the time to read this article.

--

--