Back to Git & Dev Tools
2026-04-205 min read

commit (Git & Dev Tools)

Learn commit (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Git Commits: A full guide for Developers

Why This Matters

In software development, version control is vital to manage changes in your codebase efficiently. Git, a popular distributed version control system, enables developers to track changes, collaborate effectively, and recover lost work easily. One essential aspect of using Git is understanding how to make commits correctly. This guide will walk you through the process of creating commits, common mistakes to avoid, practice questions, and frequently asked questions.

Prerequisites

Before diving into the core concept, ensure you have a basic understanding of:

  1. Git installation and setup (Git installation guide)
  2. Basic Git commands such as git init, git add, git status, git commit, git push, and git pull (Git Basics)
  3. Understanding the Git workflow, including branches and merges (Git Branching)
  4. Familiarity with text editors like Vim, Emacs, or nano for writing commit messages

Core Concept

A commit in Git represents a single instance of saving changes to your repository. Each commit contains:

  1. A unique SHA-1 identifier (hash)
  2. The author and committer details
  3. The commit message describing the changes made
  4. The snapshot of the project's current state at that point in time

Making a Commit

To create a new commit, follow these steps:

  1. Navigate to your repository directory using the command line.
  2. Stage the files you want to include in the commit using git add or git add . for all changes.
  3. Write a concise and descriptive commit message using git commit -m "Your commit message". You can use a text editor like Vim, Emacs, or nano by omitting the -m flag to open an editor and write your commit message.
  4. Verify your commit by running git log.
$ cd my-project
$ git add .
$ git commit

Write your commit message in the text editor that opens

$ git log


### Commit Messages Best Practices

1. Use the imperative mood in your commit message (e.g., "Add", "Fix", "Update").
2. Keep it concise and descriptive, limiting each line to 72 characters or fewer.
3. Include any relevant issue tracking numbers if applicable.
4. Use the present tense for completed work and future tense for ongoing work (e.g., "Add support for new feature" vs. "Adding support for new feature").
5. Write a clear subject line followed by a blank line and a detailed description of the changes made.

Worked Example

Let's walk through an example of creating a commit:

  1. Create a simple text file named example.txt with the content "Hello, World!".
  2. Stage and commit the changes using the following commands:
$ echo "Hello, World!" > example.txt
$ git add example.txt
$ git commit -m "Added new example file"

Common Mistakes

  1. Incorrect or vague commit messages: Avoid using generic phrases like "Fixed an issue" or "Updated code". Instead, provide specific details about the changes made.
  2. Committing unstaged changes: Always stage your changes before committing to ensure you're only saving intended modifications.
  3. Ignoring .gitignore file: Make sure to follow your project's .gitignore file to prevent unnecessary files from being included in commits.
  4. Not using descriptive branch names: Use meaningful branch names that clearly indicate the purpose of the branch (e.g., feature/new-login-page, bugfix/login-error-message).
  5. Committing large files: Large files can slow down your repository and make it difficult to work with. Consider splitting large files into smaller ones or using compression tools like Git LFS.
  6. ### Common Mistakes (subheadings)
  • Incorrect commit messages
  • Committing unstaged changes
  • Ignoring .gitignore file
  • Not using descriptive branch names
  • Committing large files
  1. Not squashing commits: Merging multiple small commits can create a noisy and difficult-to-follow history. Squash commits together using the git rebase command to maintain a clean, well-structured codebase.
  2. ### Common Mistakes (subheadings)
  • Not squashing commits
  1. Not using feature branches: Working directly on the master branch can lead to conflicts and instability. Always create a new feature branch for your development work.
  2. ### Common Mistakes (subheadings)
  • Not using feature branches

Practice Questions

  1. What is the purpose of a commit in Git?
  2. How do you stage files for committing?
  3. Why is it important to write descriptive commit messages?
  4. What happens if you forget to add a file to your .gitignore file before committing?
  5. What are some best practices for naming branches in Git?
  6. How can you squash multiple commits into one using Git?
  7. Why is it important to use feature branches when working on new features or bug fixes?
  8. What is the difference between git commit and git commit -a?
  9. How do you view the history of changes in your repository?
  10. What command can you use to revert a specific commit in your repository?

FAQ

  1. What should I do if I accidentally commit unstaged changes?
  • You can amend the most recent commit using git commit --amend. Stage your changes, write a new commit message, and save the changes.
  1. How can I revert a specific commit in my repository?
  • Use the git revert command followed by the commit hash to undo the changes made in that commit.
  1. What is Git's default branch name?
  • The default branch name is master. However, it's recommended to use main instead for new projects.
  1. How can I view the history of changes in my repository?
  • Run the git log command to see a list of all commits, along with their SHA-1 identifiers, authors, committers, and messages.
  1. What is Git's staging area, and why is it important?
  • The staging area (also known as the index) is an intermediate step between your working directory and the repository. It allows you to select which changes will be included in the next commit. Staging is essential for organizing your commits effectively and maintaining a clean, well-structured codebase.
  1. What command can I use to see the differences between my working directory and the last committed version of a file?
  • Use git diff to view the differences between your working directory and the last committed version of a file.
  1. How can I view the status of my files in the repository?
  • Run the git status command to see which files have been modified, staged, or untracked.
  1. What is Git's .gitignore file and why should I use it?
  • The .gitignore file lists the files and directories that Git should ignore when committing changes. Using a .gitignore file helps keep your repository clean and efficient by excluding unnecessary files from being tracked.
commit (Git & Dev Tools) | Git & Dev Tools | XQA Learn