Git and GitHub

Commits In Detail

Commits are fundamental to Git’s version control functionality. This section delves into best practices for making commits, writing effective commit messages, fixing mistakes, and ignoring unnecessary files.

Keeping Your Commits Atomic

Atomic commits are small, self-contained changes that address a single issue or feature. This practice makes understanding and reviewing changes easier, rolling back specific changes if needed, and managing your project more effectively.

Best Practices for Atomic Commits:

  • Single Purpose: Each commit should address one specific issue or feature. For example, if you are adding a new feature and fixing a bug, these should be two separate commits.

  • Logical Grouping: Group-related changes together. For example, if a new feature requires changes in multiple files, commit them together if they are all part of the same logical change.

  • Frequent Commits: Commit changes frequently to avoid large, unwieldy commits.

Example:

# Add user authentication feature
git add app/controllers/user_controller.rb app/models/user.rb
git commit -m "Add user authentication feature with login and logout functionality"

# Fix a bug in user authentication
git add app/controllers/user_controller.rb
git commit -m "Fix bug in user authentication where session was not being cleared"

Commit Messages

Good commit messages are crucial for maintaining a readable and maintainable project history. They provide context for the changes and help collaborators understand the purpose and impact of each commit.

Best Practices for Commit Messages:

  • Use the Imperative Mood: Start with a verb in the imperative mood (e.g., "Add", "Fix", "Remove").

  • Keep it Short and Descriptive: The first line should concisely summarize the changes, ideally under 50 characters.

  • Provide Detailed Explanations: If necessary, follow the summary with a blank line and a more detailed description of the changes.

Example:

# Short, descriptive commit message
git commit -m "Add user authentication feature"

# Detailed commit message
git commit -m "Fix user authentication bug

Ensure that the user session is correctly cleared upon logout. This fixes a bug where users remained logged in despite clicking the logout button."

Some teams develop a custom way of writing commits to helping new and current staff understand and get up to speed with the shared codebase. Some use commits like these:

git commit -m "[feature] added new endpoint for fetching market data from CoinMarketCap API"

git commit -m "[test] added a test case for the new CMC endpoint to test the format of the output"

git commit -m "[chore] deleted unnecessary comments from 5 files for brevity"

git commit -m "[fixes] made use of Zap logging SDK and some unit tests to fix the currency parsing error"

Fixing Mistakes with Amend

Sometimes, you must modify the most recent commit, such as correcting a commit message or adding missed changes. The git commit --amend command allows you to do this without creating a new commit.

Using git commit --amend:

  • Correcting a Commit Message:

    git commit --amend -m "Corrected commit message"
    

    This replaces the message of the most recent commit.

  • Adding Missed Changes:

    # Stage the missed changes
    git add path/to/missed/file
    
    # Amend the commit
    git commit --amend --no-edit
    

    The --no-edit option keeps the original commit message unchanged.

Ignoring Files with .gitignore

The .gitignore file tells Git which files or directories to ignore. This is useful for excluding files that are not necessary for version control, such as build artifacts, temporary files, and personal settings.

Creating a .gitignore File:

  1. Create a .gitignore File: In the root of your repository, create a file named .gitignore.

  2. Specify Files and Directories to Ignore: Add patterns to the .gitignore file to specify what to ignore.

Example .gitignore for a backend project:

# Ignore build artifacts
/build/
/dist/

# Ignore temporary files
*.tmp
*.log

# Ignore OS-specific files
.DS_Store
Thumbs.db

# Ignore environment variables
.env

💡 NOTE: The package managers of some tools (like Cargo for Rust) are usually used to generate the project codebase and initialize it for Git. For these reasons, there is usually already a .gitignore file. Also, start by creating the project repository on GitHub before cloning it locally. In that case, you can initialize the .gitignore before clicking the Create new repository button.

Using .gitignore:

  • Add and Commit the .gitignore File:

    git add .gitignore
    git commit -m "Add .gitignore file to exclude unnecessary files"
    

Following these practices for commits, committing messages, fixing mistakes, and ignoring unnecessary files. You'll maintain a clean and manageable project history. This will facilitate easier collaboration and project maintenance.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board