git commit Command


git commit Command

The git commit command is used to save changes to the local repository. It creates a snapshot of the staged changes and records it in the version history of the repository. Each commit has a unique identifier and includes a commit message describing the changes made.


What Does git commit Do?

When you run git commit, Git performs the following actions:

  1. Records a Snapshot: It takes a snapshot of the current state of the staged changes (from the staging area) and saves it in the repository.
  2. Creates a Commit Object: This includes metadata such as the commit message, author information, and a unique commit hash.
  3. Updates the Branch: The branch you're working on is updated to point to the new commit.

Basic Syntax

git commit -m "Commit message"
  • -m "Commit message": Provides a message describing the changes made in this commit.

Examples

Creating a Commit with a Message

To create a commit with a message describing the changes:

git commit -m "Add new feature for user authentication"

This command commits all staged changes with the message "Add new feature for user authentication".

Committing Changes with a Text Editor

If you omit the -m option, Git will open your default text editor to allow you to write a more detailed commit message:

git commit
  • The editor opens with a template for your commit message.
  • You can provide a detailed explanation of the changes in the editor and then save and close the editor to complete the commit.

Committing Changes and Skipping the Staging Area

You can commit changes directly without staging them first by using the -a option. This will automatically stage all modified and deleted files:

git commit -a -m "Fix bug in user login"
  • This command stages all tracked files that have been modified or deleted and creates a commit with the provided message.

Committing with a Different Author

You can specify a different author for the commit using the --author option:

git commit -m "Update README" --author="Jane Doe <jane.doe@example.com>"
  • This is useful if you're committing changes on behalf of someone else or if the commit should reflect a different author.

Commit Message Guidelines

  1. Keep It Brief: The subject line (first line) should be a brief summary of the changes, ideally under 50 characters.
  2. Provide Context: Use the body of the commit message to explain what changes were made and why they were necessary. Separate the subject from the body with a blank line.
  3. Use Imperative Mood: Write commit messages in the imperative mood (e.g., "Fix bug" instead of "Fixed bug").

Example:

git commit -m "Fix issue with login authentication Corrected the validation logic to handle edge cases where the user input was not being processed correctly."

Viewing Commit History

To view the commit history, use:

git log
  • This command shows a list of commits, including commit hashes, author information, dates, and commit messages.

Undoing a Commit

  • Amend Last Commit: If you need to change the last commit (e.g., modify the commit message or add more changes), use --amend:

    git commit --amend -m "Updated commit message"
    • This replaces the last commit with a new commit that contains the updated message and staged changes.
  • Undo Commit (Keep Changes): If you want to undo the last commit but keep the changes staged:

    git reset --soft HEAD~1
  • Undo Commit (Discard Changes): To undo the last commit and discard changes:

    git reset --hard HEAD~1

Summary

  • Purpose: git commit saves the staged changes to the local repository, creating a new commit with a unique identifier and a commit message.
  • Syntax: git commit -m "Commit message"
  • Usage: Use git commit after staging changes with git add to record those changes in the repository. Optionally, you can provide a detailed commit message and use various options to customize the commit process.

Committing is a crucial step in version control, as it ensures that changes are saved with descriptive messages, allowing you and others to track and understand the evolution of your project.