The git commit
command is used to save your changes to the local repository. This command takes the staged changes in your working directory and records them in the repository along with a descriptive message.
Initial Example
To commit staged changes with a message, use:
git commit -m "Your commit message here"
COMMIT Options
Option | Description |
---|---|
-a, --all | Stage all modified and deleted files before committing. |
-m <msg>, --message <msg> | Use the specified message as the commit message. |
-F <file>, --file <file> | Take the commit message from the specified file. |
--author <author> | Override the author for this commit. |
--date <date> | Override the date for this commit. |
-e, --edit | Edit the commit message before committing. |
--amend | Modify the last commit, including updating its message. |
-C <commit>, --reuse-message <commit> | Reuse the commit message from the specified commit. |
-c <commit>, --reedit-message <commit> | Reuse and edit the commit message from the specified commit. |
--fixup <commit> | Create a commit to fix up a previous commit. |
--squash <commit> | Create a commit to squash a previous commit. |
--reset-author | Use the current user as the author for the commit. |
--signoff | Add a Signed-off-by line at the end of the commit message. |
-S, --gpg-sign[=<keyid>] | GPG-sign the commit using the specified key. |
--no-gpg-sign | Do not GPG-sign the commit, even if commit.gpgSign is set. |
--template <file> | Use the specified file as a template for the commit message. |
-u <key-id>, --untracked=<key-id> | Set the GPG key for signing commits if not already configured. |
--cleanup <mode> | How to strip spaces and comments from the message. |
--status | Include the status of staged files in the commit message template. |
--no-status | Do not include the status of staged files in the commit message template. |
-q, --quiet | Suppress commit summary message. |
-v, --verbose | Show unified diff between HEAD and the commit in the message template editor. |
--[no-]verify | Bypass the pre-commit and commit-msg hooks. |
--allow-empty | Create a commit, even if there are no changes staged. |
--allow-empty-message | Create a commit with an empty commit message. |
--no-edit | Use the existing commit message without launching an editor. |
--amend | Replace the tip of the current branch by creating a new commit with the same parent as the current tip. |
--no-post-rewrite | Bypass the post-rewrite hook. |
Examples
1. Committing with a Message
To commit staged changes with a specific message:
git commit -m "Added new feature to the project"
This records your changes in the repository with the provided message.
2. Committing All Changes
To commit all changes (both staged and unstaged) in the working directory, use the -a
option:
git commit -a -m "Fixed bugs and updated documentation"
This stages and commits all changes in tracked files.
3. Amending the Last Commit
To modify the most recent commit, including its message, use the --amend
option:
git commit --amend -m "Updated commit message with more details"
This allows you to edit the previous commit, combining any staged changes with it.
4. Using an Editor to Write the Commit Message
To open your default text editor for writing a detailed commit message, simply use:
git commit
This will open your editor where you can type a multi-line commit message.