Discuss your project

Coding Smart with Git and GitFlow: A Tutorial for Better Code Management

/* by Tirth Bodawala - June 14, 2023 */

In today’s digital age, software development has become a powerful engine driving innovation and growth. And to power this engine efficiently, we need robust tools that facilitate smooth collaboration, track changes, and provide secure backup. Two such potent tools are Git and GitFlow. This blog post will demystify these tools and highlight their vital role in our coding routine.

Git: Your Coding Time Machine

Git is like a time machine for your code. It keeps track of all the changes made in the code over time. If you stumble upon a bug, you can easily go back and see what caused it.

Git also allows multiple developers to work on the same project without stepping on each other’s toes. This is done using ‘branches’ – separate spaces where you can play around with the code without messing up the main project.

What’s more? Git serves as your safety net in case of hardware failures. Since the entire code is stored on your local machine, you can easily restore your work even if your hardware crashes.

The Understated Brilliance of Git

Git shines as a distributed version control system that touts speed and efficiency. But its importance goes beyond just its headline features. Here are the crucial roles Git plays in everyday coding:

Historical Tracking: Git enables developers to track code alterations over time. Unraveling the origins of a bug becomes a simplified task with the capability to trace back changes.

Unified Collaboration: Git empowers multiple developers to work harmoniously on a shared project. It accomplishes this through isolated environments known as branches, enabling developers to work independently on their code snippets.

Reliable Backup and Restoration: An underappreciated aspect of Git is its robustness against hardware failures. With Git, every piece of your code lives on your local machine, providing a reliable backup. In the event of a hardware failure, your work remains secure and unaltered.

Commit Often, Push Daily: A Recommended Mantra

Committing your work and pushing it to a shared repository on a daily basis is a practice that cultivates security, integration, and ongoing collaboration. Regular commits generate a comprehensive log of the project’s evolution, keeping the entire team updated with recent changes. Moreover, it circumvents complications arising from large, intricate merges in the future.

Navigating the GitFlow Maze

Designed by Vincent Driessen, GitFlow has found popularity for its compatibility with collaborative and scalable development teams. Here’s a concise breakdown of its structure:

Master Branch: Home to the source code that exists in production.

Development Branch: A branch stemming from the master, where code is developed and tested.

Feature Branches: Individual branches springing from the development branch for each feature or bug fix. Each addition is thus isolated, preventing interference with the main code.

Release Branches: These branches host features from the development branch that have reached stability and are ready for release. Any bugs discovered are rectified here before the final merge with the master and development branches.

Hotfix Branches: Created from the master branch, these are intended for urgent bug fixes in the production code. Following testing, they are merged back with the master and development branches.

Improved Naming Conventions for Branches

When working on large projects, you might be using a ticketing system like Jira or GitHub issues to track tasks and bugs. In such cases, it can be beneficial to include the ticket number in the branch name. This creates an immediate, clear connection between the task and the code.

Here’s how you can adjust the previous naming convention to include ticket numbers:

  • For features: feature/TICKET_NUMBER-short-feature-description. For example, if you’re adding a login feature corresponding to ticket number 1234, the branch could be named feature/1234-login.
  • For bug fixes: bugfix/TICKET_NUMBER-short-bug-description. For example, if you’re fixing a bug in the login system that corresponds to ticket number 1235, the branch could be named bugfix/1235-login-issue.
  • For hotfixes: hotfix/TICKET_NUMBER-short-fix-description. For example, if you need to fix an urgent issue in the payment system related to ticket number 1236, the branch could be named hotfix/1236-payment-issue.

This naming convention makes it straightforward to track the progress of individual tickets, as the ticket number and branch name are directly connected.

Benefits of Including Ticket Numbers in Branch Names

Including ticket numbers in branch names helps to:

  • Improve traceability: You can easily trace the code changes back to the specific task or bug, facilitating better tracking and accountability.
  • Enhance communication: When discussing a particular feature or bug fix, referring to the ticket number and branch name gives everyone a clear context.
  • Boost efficiency in code reviews: During pull requests, reviewers can immediately understand the purpose of the changes, and cross-verify with the details mentioned in the corresponding ticket.
  • Adopting a structured naming convention for your branches that incorporates ticket numbers is a step towards more efficient and traceable development practices. These little refinements, over time, can lead to significant improvements in your team’s productivity and project management efficiency.

Guidelines for Writing Commit Messages

  • Keep it Concise: The first line of your commit message should be a brief summary of the changes, ideally no more than 50 characters.
  • Detail in the Body: If the commit is complex and requires an explanation, add a detailed description in the body of the commit message, which should be separated from the summary by a blank line.
  • Use the Imperative Mood: Write your commit message as if you are giving a command. For example, use “Fix bug” instead of “Fixed bug” or “Fixes bug.”
  • Reference Ticket Numbers: If you’re using a ticketing system, reference the ticket number in your commit message. This improves traceability and helps link your commits to the tasks they’re related to.

Patterns for Writing Commit Messages

A common pattern that is used for commit messages is:

Type(scope): Subject

Here’s a breakdown:

  • Type: This signifies the type of change made. Common types include feat (for a new feature), fix (for a bug fix), style (for changes in code formatting that do not affect functionality), refactor (for code changes that neither fix a bug nor add a feature), and others.
  • Scope: This is an optional part where you can mention what part of the codebase the commit modifies.
  • Subject: This is a brief summary of the changes, written in the imperative mood.

For example: feat(auth): Add login via Facebook

This commit message communicates that it’s a new feature (feat) related to authentication (auth) and that it adds Facebook login functionality.

For commits related to a specific ticket or issue, you can include the ticket number in the subject like so:

  • fix(123): Resolve issue with password reset email

This tells us that it’s a bug fix (fix) related to ticket number 123 and that it resolves an issue with the password reset email.

Crafting clear, concise, and informative commit messages is an essential skill in software development. Not only do they improve personal productivity, but they also enhance team collaboration and project maintainability. By adhering to the guidelines and patterns shared above, your commit history will become a valuable resource in understanding the evolution of your project.

Exploring Pull/Merge Requests

Suppose you’re working on a new feature in your application, say, ‘user authentication’. You have created a separate branch (let’s call it ‘feature/651-user-auth’) and have implemented the new feature. After testing it in your local environment, you decide it’s ready for review. You push your changes to the remote repository and then create a Pull Request.

In the Pull Request, you explain what you’ve done, why you’ve done it, and how it has been tested. This is your opportunity to give context to your changes, making it easier for reviewers. After creating the Pull Request, your teammates can now review your code and provide feedback. They might suggest improvements, ask questions, or approve the changes.

Once the changes have been approved by your team, and all checks (like automated tests) have passed, your code can be merged into the main branch (e.g., ‘development’ or ‘master’, depending on your workflow).

In conclusion, the effective usage of Git, GitFlow, and consistent adoption of branch naming and commit conventions play a pivotal role in structuring, securing, and managing your codebase. Embracing these tools and practices will lead to significant improvements in team productivity, collaboration, and overall project maintainability.