Lesson 4
Commits in Detail: Making Them Atomic, Crafting Messages, and Using Amendments
Introduction

Welcome to the final lesson of the Git Basics course! In previous lessons, you've learned the basics of Git and how to use essential commands to manage changes in your projects. Now, we'll focus on mastering the art of creating highly effective commits. This lesson will help you enhance the readability and usability of your commit history, making collaboration smoother and more productive. Let's get started!

Keeping Commits Atomic

An atomic commit refers to the practice of ensuring each commit represents a single logical change. This means that when you make a commit, it should address one specific task or update. For example, if you're fixing a bug, adding a new feature, or refactoring code, each of these actions should have its own separate commit.

The idea behind atomic commits is to simplify code reviews by making it easier for reviewers to understand the scope of each change. By keeping commits atomic, bug tracking becomes more straightforward, as isolating changes allows developers to pinpoint specific issues quickly.

Furthermore, atomic commits result in a cleaner, more understandable commit history. This enables anyone reviewing the project to follow its evolution more easily, as each commit tells a specific part of the project's story.

Let’s revisit our initial example of Bob and Alice preparing for the surprise party. In this context, we can see how they broke their work into clear, logical, atomic commits.

  1. Create guest list: Alice focused solely on building the guest list and made a commit with the message "Create guest list."

  2. Plan decorations: After completing her guest list, Alice made a new, separate commit to plan the decorations. Notice that she didn’t combine these changes into one commit because the guest list and decorations are two distinct tasks. This was committed with the message "Plan decorations."

  3. Update menu for vegetarians: After initially planning the menu and creating the music playlist, Bob realized he needed to update the menu to accommodate vegetarians. Rather than combining this change with his previous work, he made a separate commit for the update with the message "Update menu for vegetarians."

By keeping each commit focused on one specific task, Alice and Bob maintained a clean, understandable commit history that makes it easy to navigate and understand the changes.

Writing Effective Commit Messages

A good commit message provides clear context about why a change was made and what it accomplishes. Using the present imperative style (e.g., "Add", "Fix", "Remove") is a best practice, as it makes your commit history read like a clear list of tasks that have been done. An effective commit message is typically a brief, concise summary of the change, ideally no longer than 50 characters.

Additionally, avoid using the first person or any personal references in commit messages to ensure that the message remains objective and focused on the tasks rather than the individual actions of a contributor.

To better understand what makes a commit message effective, let’s compare good and bad examples:

AspectGoodBad
Use of Present ImperativeFix search bar alignment issueFixed the search bar
ClarityAdd login functionalityStuff added
SpecificityUpdate header image to new designUpdated things
Separation of Featurescommit 1: Add search bar, commit 2: Plan decorations for eventAdd search and plan event
Avoid First PersonOptimize loading speed for homepageI improved loading speed

Using clear, concise, and focused commit messages makes it easier for others (and your future self) to navigate the project’s history. The good examples above show how following these simple practices can make your commit history meaningful and organized.

Including a Commit Message Body

For larger or more complex commits, it's beneficial to include a commit message body. The body provides additional detail beyond the subject line, explaining the reasoning behind the changes or offering any necessary context. It is separated from the subject line by a blank line and can span multiple lines if needed. This practice is especially useful when revisiting the commit in the future or when multiple team members are involved.

Here’s a step-by-step guide to adding a commit message body:

  1. Start the commit: Use git commit without the -m flag.
  2. Enter the message: In the text editor that opens (like VIM), write your subject line.
  3. Add a blank line: Press Enter to leave a blank line after the subject.
  4. Write the body: Provide additional details or context about the commit.

Example:

Plain text
1Update payment system logic 2 3Simplified redundant code paths to improve performance 4and ensure that the transaction process is efficient.
Using "git commit --amend" to Fix Mistakes

Mistakes happen, and sometimes you might realize a commit message is unclear or a small change was left out of your last commit. This is where git commit --amend becomes useful.

With git commit --amend, you can rewrite the most recent commit, either to edit the commit message or to include additional changes that you've staged. This command will open your default text editor, allowing you to modify the commit message or add any staged changes to the commit.

When using git commit --amend, it's important to apply it in the right context: use it when the commit is still local and hasn't been shared with a remote repository. This ensures that your history remains clean without causing potential conflicts with others’ work. However, avoid using it after you've shared the commit, as it can rewrite history and create confusion.

Let’s take an example to better understand this command. Imagine you’ve created three menu options: vegetarian, gluten-free, and keto. For each of them, you have a text file, and you want to add them to your repository. You added two files—vegetarian_menu.txt and gluten_free_menu.txt—and committed them with the message:

Bash
1git add vegetarian_menu.txt gluten_free_menu.txt 2git commit -m "Add vegetarian and gluten-free menus"

After this commit, you realize you forgot to include the file for the keto menu. To fix this, start by staging the missing file:

Bash
1git add keto_menu.txt

Now, instead of creating a new commit, you can amend the previous one to include the keto menu and update the message. Use:

Bash
1git commit --amend

This will open the commit message editor, where you can update the message to reflect all three menu options:

Plain text
1Add vegetarian, gluten-free, and keto menus

By using git commit --amend, you incorporate the missing file and update the commit message in one step, ensuring that the commit history accurately reflects all the changes in a single, logical commit.

For more information on using git commit, you can refer to the official Git documentation: Git Commit Documentation.

VIM editor

When using git commit --amend, it’s important to know how to navigate and edit in the default text editor, which, in our case, is VIM. Although VIM might seem a bit intimidating at first, here’s a quick guide to help you modify commit messages efficiently.

  1. Insert Mode: To begin typing or editing the commit message, press i to enter Insert mode.
  2. Editing: Once in Insert mode, make any necessary changes to your commit message.
  3. Exiting Insert Mode: After editing, press Esc to return to Command mode.
  4. Saving and Exiting: To save your changes and exit VIM, type :wq and press Enter.
  5. Exiting Without Saving: If you want to exit without saving your changes, type :q! and press Enter.

Example in Context:

Imagine you’ve run git commit --amend, and VIM opens the last commit message for editing. You might see something like this:

Plain text
1Add vegetarian, gluten-free, and keto menus 2 3# Please enter the commit message for your changes. Lines starting 4# with '#' will be ignored, and an empty message aborts the commit. 5# 6# Date: Thu Oct 24 13:55:16 2022 +0000 7# 8# On branch main 9# Changes to be committed: 10# modified: vegetarian_menu.txt 11# modified: gluten_free_menu.txt 12# added: keto_menu.txt 13#

To update the message (for example, adding the keto menu), follow these steps:

  1. Press i to enter Insert mode.
  2. Edit the commit message. For instance, change:
    Plain text
    1Add vegetarian and gluten-free menus
    to:
    Plain text
    1Add vegetarian, gluten-free, and keto menus
  3. Once done, press Esc to exit Insert mode.
  4. Type :wq and press Enter to save the changes and close VIM.

By learning these simple commands, you can easily modify commit messages using VIM when working with git commit --amend.

Summary

In this lesson, we delved into creating effective commits by keeping them atomic and writing clear commit messages. We also discussed how to fix mistakes using git commit --amend. These practices are essential for maintaining a clean, comprehensible commit history, which is vital for collaboration and project management.

Next, you'll apply what you've learned in practical exercises. As you work on your code, remember to focus on crafting precise commits and messages—skills that will greatly benefit your development workflow and teamwork. Happy committing!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.