Chuck's Academy

GIT

Best Practices in Git

In this chapter, we will learn some of the best practices for working with Git, especially when collaborating on large projects or with distributed teams. These recommendations will help you maintain a clean commit history, improve collaboration with other developers, and avoid common mistakes.

Writing Clear and Descriptive Commit Messages

One of the most important aspects of working with Git is writing good commit messages. A good commit message should be clear and descriptive, providing enough information about the changes made. Avoid generic messages like "fix" or "changes," and instead, write messages that explain the why and what of the change.

Example of a bad commit message:

bash
"A commit message like 'fixes' offers no useful information about what was changed or why."

Example of a good commit message:

bash
"A commit message like 'Fixes bug in tax calculation when total is negative' clearly describes what was changed and why it matters."

Making Small and Frequent Commits

Making small and frequent commits is a good practice that facilitates tracking changes and simplifies troubleshooting. Each commit should focus on a single task or functionality, making the history more readable and easier to review.

When commits are too large, it becomes more difficult to identify what introduced an error or problem. On the other hand, small and specific commits allow changes to be reverted or adjusted more precisely.

Using Branches Efficiently

Git is incredibly powerful when working with branches. Here are some recommendations to use branches effectively:

  • Use branches for new features: Create a new branch for each new feature, bug fix, or experiment. This allows you to work in isolation without affecting the main branch (main or master).

    bash
  • Keep the main branch clean: The main branch should reflect a stable state of the project. Avoid making changes directly in main unless you are sure they are production-ready changes.

  • Delete branches that are no longer used: Once a branch has been merged, you can delete it to keep the repository organized.

    bash

Rebase vs Merge

When working with multiple branches, you might need to integrate changes from one branch into another. Git offers you two ways to do this: merge and rebase.

  • Merge: Creates a merge commit that combines the changes from both branches.

    bash
  • Rebase: Moves the commits of one branch over another, creating a cleaner and more linear history.

    bash

Both approaches are valid, but rebase is preferred in projects where a linear and easy-to-follow commit history is desired. However, be careful when using rebase on branches shared with other developers, as it can rewrite history and cause conflicts.

Reviewing Code and Making Pull Requests

When working in a team, it is essential to conduct code reviews before merging changes into the main branch. Pull requests are a useful tool for this. Before merging a branch, open a pull request so other developers can review your changes and suggest improvements or corrections.

GitHub and GitLab offer an easy-to-use interface for opening and reviewing pull requests, facilitating collaboration among distributed teams.

Protecting the Main Branch

A common practice in collaborative projects is to protect the main branch (main or master). This means that no one can make changes directly to the main branch without first going through a review process (usually via a pull request).

To protect a branch on GitHub, follow these steps:

  1. Go to Settings in your repository.
  2. Select Branches and then choose the branch you want to protect.
  3. Enable branch protection and set rules like requiring reviews before merging or ensuring tests pass.

This ensures that only reviewed and tested code reaches the main branch.

Keeping the History Clean

The git rebase command can be very useful for maintaining a clean commit history. Using interactive rebase, you can reorder, squash, or delete unnecessary commits before they are merged into the main branch.

To start an interactive rebase on the last 5 commits, use:

bash
"With the command 'git rebase -i followed by HEAD tilde 5', you can reorder, squash, or delete commits in the last five changes."

Keeping a clean history makes the project easier to read and debug issues in the future.

Conclusion

In this chapter, we covered some of the best practices for working with Git, including how to write good commit messages, the importance of making small and frequent commits, and how to work with branches efficiently. We also discussed the difference between merge and rebase, and how to review code using pull requests. By following these best practices, you will be able to maintain a clean and organized workflow in your projects.

In the next chapter, we will look at how to manage repositories long-term, and how to handle branches and tags in projects that grow over time.


Ask me anything