Skip to main content

Git Commit

Introduction

The git commit command is one of the most fundamental operations in Git. A commit is like taking a snapshot of your project at a specific point in time. This snapshot records the changes you've made to your files since the last commit and stores them permanently in your Git repository's history.

Think of commits as checkpoints in your project's timeline. They allow you to:

  • Track the evolution of your project
  • Revert to previous states if needed
  • Collaborate effectively with others
  • Document the development process

Understanding the Commit Process

Before we dive into the commit command, let's understand the Git workflow:

  1. Working Directory: Where you make changes to your files
  2. Staging Area: Where you prepare changes to be committed
  3. Repository: Where Git permanently stores the changes as commits

Basic Commit Command

Creating Your First Commit

To create a commit, you first need to add your changes to the staging area using git add, then commit them:

bash
# Add changes to staging area
git add filename.txt

# Commit the changes
git commit -m "Add new feature"

The -m flag allows you to specify a commit message directly in the command line.

Viewing the Result

After committing, Git will display information about the commit:

bash
[main 3d7c8a3] Add new feature
1 file changed, 25 insertions(+), 4 deletions(-)

This output shows:

  • The branch name (main)
  • The commit hash (3d7c8a3)
  • The commit message ("Add new feature")
  • A summary of changes (1 file changed, with 25 lines added and 4 removed)

Writing Effective Commit Messages

Good commit messages are crucial for maintaining a useful history. They should:

  1. Be clear and concise
  2. Explain the "what" and "why" of changes
  3. Follow a consistent format

Single-Line vs. Multi-Line Messages

For simple changes, a single-line message is sufficient:

bash
git commit -m "Fix typo in login form"

For more complex changes, use multi-line messages:

bash
git commit -m "Add user authentication feature

- Add login form validation
- Create user session handling
- Implement password reset functionality"

Commit Options and Flags

Git commit provides several useful options:

Committing All Changes

To add and commit all modified files in one command:

bash
git commit -am "Update documentation"

The -a flag automatically adds all tracked, modified files to the staging area.

Amending Commits

To modify your most recent commit:

bash
git commit --amend -m "Updated commit message"

This is useful for:

  • Fixing a typo in your commit message
  • Adding forgotten changes to the previous commit

Viewing Commit History

To see your commit history:

bash
git log

Sample output:

commit 3d7c8a3e5fe4d3e1a578be1f7c7f242a7f5a7328 (HEAD -> main)
Author: Your Name <[email protected]>
Date: Mon Oct 10 15:23:47 2022 -0700

Add new feature

commit f8d2a1b0c8e5d4c3b2a1f0e9d8c7b6a5f4e3d2c1
Author: Your Name <[email protected]>
Date: Mon Oct 10 14:15:32 2022 -0700

Initial commit

For a more compact view:

bash
git log --oneline

Output:

3d7c8a3 Add new feature
f8d2a1b Initial commit

Best Practices for Committing

1. Commit Atomically

Make each commit represent a single logical change. This makes it easier to:

  • Understand the history
  • Find bugs
  • Revert specific changes

Example of good atomic commits:

- Add user login functionality
- Fix validation bug in signup form
- Update styling for buttons

2. Commit Frequently

Regular commits provide more granular history and make collaboration easier.

3. Follow a Consistent Message Format

A popular format is:

[type]: [summary]

[detailed description]

Types might include:

  • feat: - new feature
  • fix: - bug fix
  • docs: - documentation changes
  • style: - formatting changes
  • refactor: - code refactoring

Example:

feat: add password strength indicator

- Add visual indicator that updates in real-time
- Implement validation rules for password complexity
- Display helpful messages to guide users

Practical Examples

Example 1: Basic Web Development Workflow

bash
# Start a new project
mkdir my-website
cd my-website
git init

# Create some files
echo "<html><body><h1>My Website</h1></body></html>" > index.html
echo "body { font-family: Arial, sans-serif; }" > styles.css

# Add files to staging
git add index.html styles.css

# Make first commit
git commit -m "Initial project setup with basic HTML and CSS"

# Make changes to a file
echo "<p>Welcome to my website!</p>" >> index.html

# Commit the changes
git add index.html
git commit -m "Add welcome message to homepage"

Example 2: Fixing a Bug

bash
# Create a bugfix branch
git checkout -b fix-login-bug

# Fix the bug
# (Make your changes to the code)

# Add and commit the fix
git add auth.js
git commit -m "fix: resolve issue with login form validation

The form was not properly validating email addresses, allowing
invalid formats to be submitted. Added regex validation."

# Push the fix
git push origin fix-login-bug

Common Commit Problems and Solutions

Problem: Committed to the Wrong Branch

If you accidentally committed to the wrong branch:

bash
# Create a new branch with your changes
git branch correct-branch

# Reset your current branch to remove the commit
git reset --hard HEAD~1

# Switch to the new branch
git checkout correct-branch

Problem: Need to Undo a Commit

To undo your most recent commit but keep the changes:

bash
git reset --soft HEAD~1

To completely remove the commit and its changes:

bash
git reset --hard HEAD~1

Summary

The git commit command is essential for recording changes to your Git repository. Key points to remember:

  • Commits create snapshots of your project at specific points in time
  • Always add files to the staging area before committing
  • Write clear, descriptive commit messages
  • Commit atomically (one logical change per commit)
  • Use options like -a and --amend to streamline your workflow

By following these principles, you'll create a clean, useful history that makes collaboration easier and provides a valuable record of your project's development.

Exercises

  1. Create a new Git repository and make your first commit
  2. Practice making multiple commits with different types of changes
  3. Try using git commit --amend to modify your most recent commit
  4. Explore the git log command with different flags to view your commit history
  5. Write commit messages following the best practices outlined in this guide

Additional Resources



If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)