The git commit -m
command saves changes to your local repository with a message describing the modifications. This guide provides an in-depth explanation of how to use git commit -m
and its variations to efficiently manage changes in your codebase.
Understanding the git commit command
The git commit
command captures a snapshot of the staged changes in your project. This snapshot is then stored in your local repository, which allows you to track the history of your project over time. The -m
flag is used to provide a commit message directly from the command line without opening a text editor.
Syntax of git commit -m
The basic syntax of the git commit -m
command is:
git commit -m "Your commit message here"
The message should be concise and explain what changes the commit introduces to the repository. Good commit messages are crucial for maintaining a readable and navigable history.
For more information on these messages see best practices for writing commit messages.
How to use git commit -m
Stage your changes: Before you can commit, you need to stage the changes you want to include in the commit. You can do this using the
git add
command:Terminalgit add <file1> <file2>Commit the changes: Once your changes are staged, use the
git commit -m
command to commit them:Terminalgit commit -m "feat(web-app): add login functionality to the app"View the commit: You can view the commit you just made using the
git log
command:Terminalgit log --oneline
This will show a list of recent commits along with their commit messages.
Using git commit -a -m
The git commit -a -m
command (or git commit -am
) is a shortcut that allows you to skip the staging step (using git add
) for modified files (it does not affect new or deleted files). It stages changes to modified files and commits them in one step:
git commit -a -m "Update existing files with bug fixes"
This command is particularly useful when you have made changes to existing files and want to quickly commit those changes without individually staging each one.
The git commit -m
command helps you cleanly organize and manage changes to your projects. By understanding how to use this command, along with best practices for commit messages, you can enhance your productivity and maintain a clear history of your project.
Using the Graphite CLI
The Graphite CLI is designed to enhance Git workflows, particularly around managing stacked changes and simplifying complex operations like rebasing. While it doesn't have a direct equivalent to git commit -m
, it offers commands that integrate committing with branch management.
Creating a new branch with a commit
To create a new branch and commit changes simultaneously, you can use the gt create
command with the -m
flag:
gt create -m "Implement user authentication"
This command creates a new branch off the current one, stages all changes, commits them with the provided message, and sets up the proper parent-child relationship in the stack.
Modifying the last commit
If you need to amend the most recent commit on the current branch, the gt modify
command can be used:
gt modify -m "Refactor authentication module"
This command amends the last commit with the new message "Refactor authentication module".
Note: The gt modify
command is equivalent to using git commit --amend
in standard Git.
If you'd like to know more about the Graphite CLI, you can check out our documentation.