git commit -m "<message>"

Estimated reading: 2 minutes 16 views

The git commit -m "<message>" command is used to save changes staged in the Git repository along with a message describing the changes. This creates a new commit in the repository’s history.

Optional Commands with git commit

You can enhance the functionality of git commit by using the following optional flags:

OptionDescriptionExample
git commit -m "<message>"Commits staged changes with a single-line message.git commit -m "Initial commit" creates a commit with the message “Initial commit.”
git commit -a -m "<message>"Stages all tracked file changes and commits them in one step.git commit -a -m "Updated configuration" stages and commits changes to all tracked files.
git commit --amend -m "<new_message>"Updates the message of the most recent commit.git commit --amend -m "Corrected typo in previous commit" updates the last commit message.

Syntax and Example

				
					# Commit staged changes with a message
git commit -m "Added login functionality"
# Example output:
# [main 1a2b3c4] Added login functionality
# 1 file changed, 10 insertions(+)

# Stage and commit changes to all tracked files in one step
git commit -a -m "Refactored login module"
# Example output:
# [main 5d6e7f8] Refactored login module
# 2 files changed, 20 insertions(+), 5 deletions(-)

# Update the last commit message
git commit --amend -m "Fixed typo in commit message"
# Example output:
# [main 5d6e7f8] Fixed typo in commit message

				
			

Why Use git commit -m "<message>"?

  • Track Changes: Each commit represents a snapshot of your project, allowing you to track and revert changes as needed.
  • Descriptive History: Adding a meaningful message helps document the purpose of each commit for future reference.
  • Amend Commit: The --amend option allows you to correct mistakes in the last commit without creating a new one.

Conclusion

The git commit -m "<message>" command is a fundamental part of the Git workflow, enabling you to save changes with clear descriptions. By using optional flags, you can streamline your workflow and correct mistakes when necessary.

Leave a Comment

Share this Doc

git commit -m "<message>"

Or copy link

CONTENTS