Skip to main content

Synopsis

git commit [-a | --interactive | --patch] [-s] [-v] [-u[<mode>]] [--amend]
	   [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>]
	   [-F <file> | -m <msg>] [--reset-author] [--allow-empty]
	   [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
	   [--date=<date>] [--cleanup=<mode>] [--[no-]status]
	   [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
	   [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]
	   [--] [<pathspec>...]

Description

Create a new commit containing the current contents of the index and the given log message describing the changes. The new commit is a direct child of HEAD, usually the tip of the current branch, and the branch is updated to point to it. The content to be committed can be specified in several ways:
  1. By using git add to incrementally “add” changes to the index before using the commit command
  2. By using git rm to remove files from the working tree and the index
  3. By listing files as arguments to the commit command, in which case the commit will ignore changes staged in the index
  4. By using the -a switch to automatically “add” changes from all known files and “rm” files that have been removed
  5. By using the --interactive or --patch switches to decide one by one which files or hunks should be part of the commit
If you make a commit and then find a mistake immediately after that, you can recover from it with git reset.

Common Usage

1

Stage your changes

First, add files to the staging area:
git add file.c
2

Commit with a message

Create a commit with a descriptive message:
git commit -m "Fix bug in user authentication"
3

Commit all tracked changes

Automatically stage all modified tracked files:
git commit -a -m "Update documentation"
4

Amend the last commit

Modify the most recent commit:
git commit --amend

Options

-m <msg>, --message=<msg>Use the given message as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.-a, --allAutomatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.-p, --patchUse the interactive patch selection interface to choose which changes to commit.-v, --verboseShow unified diff between the HEAD commit and what would be committed at the bottom of the commit message template.
-F <file>, --file=<file>Take the commit message from the given file. Use - to read the message from the standard input.-t <file>, --template=<file>When editing the commit message, start the editor with the contents in the given file.-e, --editLet the user further edit the message taken from -F, -m, or -C.--no-editUse the selected commit message without launching an editor. For example, git commit --amend --no-edit amends a commit without changing its commit message.-C <commit>, --reuse-message=<commit>Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.-c <commit>, --reedit-message=<commit>Like -C, but with -c the editor is invoked, so that the user can further edit the commit message.
--amendReplace the tip of the current branch by creating a new commit. The recorded tree is prepared as usual, and the message from the original commit is used as the starting point.--fixup=[(amend|reword):]<commit>Create a new commit which “fixes up” the given commit when applied with git rebase --autosquash. Plain --fixup=<commit> creates a “fixup!” commit which changes the content but leaves its log message untouched.--squash=<commit>Construct a commit message for use with git rebase --autosquash. The commit message title is taken from the specified commit with a prefix of “squash! ”.--reset-authorWhen used with -C/-c/--amend options, or when committing after a conflicting cherry-pick, declare that the authorship of the resulting commit now belongs to the committer.
--dry-runDo not create a commit, but show a list of paths that are to be committed, paths with local changes that will be left uncommitted and paths that are untracked.-n, --no-verifyBypass the pre-commit and commit-msg hooks.--allow-emptyUsually recording a commit that has the exact same tree as its sole parent commit is a mistake. This option bypasses the safety.--allow-empty-messageCreate a commit with an empty commit message without using plumbing commands.-i, --includeBefore making a commit out of staged contents so far, stage the contents of paths given on the command line as well.-o, --onlyMake a commit by taking the updated working tree contents of the paths specified on the command line, disregarding any contents that have been staged for other paths.
--author=<author>Override the commit author. Specify an explicit author using the standard A U Thor <[email protected]> format.--date=<date>Override the author date used in the commit.-s, --signoffAdd a Signed-off-by trailer by the committer at the end of the commit log message.-S[<keyid>], --gpg-sign[=<keyid>]GPG-sign commits. The keyid is optional and defaults to the committer identity.--no-gpg-signCountermand both commit.gpgSign configuration variable and earlier --gpg-sign.
--cleanup=<mode>Determine how the supplied commit message should be cleaned up before committing. The mode can be:
  • strip - Strip leading and trailing empty lines, trailing whitespace, commentary and collapse consecutive empty lines
  • whitespace - Same as strip except commentary is not removed
  • verbatim - Do not change the message at all
  • scissors - Same as whitespace except everything from the scissors line is truncated
  • default - Same as strip if the message is to be edited, otherwise whitespace

Examples

Basic commit workflow

$ edit hello.c
$ git rm goodbye.c
$ git add hello.c
$ git commit

Commit with automatic staging

$ edit hello.c
$ rm goodbye.c
$ git commit -a
The command git commit -a first looks at your working tree, notices that you have modified hello.c and removed goodbye.c, and performs necessary git add and git rm for you.

Commit with inline message

$ git commit -m "Fix typo in documentation"

Commit specific files

$ edit hello.c hello.h
$ git add hello.c hello.h
$ edit Makefile
$ git commit Makefile
This makes a commit that records the modification to Makefile. The changes staged for hello.c and hello.h are not included in the resulting commit.

Amend the last commit

$ git commit --amend
This opens the editor to modify the commit message and includes any currently staged changes.

Commit after resolving merge conflicts

$ git status | grep unmerged
unmerged: hello.c
$ edit hello.c
$ git add hello.c
$ git commit

Commit Message Best Practices

Though not required, it’s a good idea to begin the commit message with a single short (no more than 50 characters) line summarizing the change, followed by a blank line and then a more thorough description. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git. For example, git format-patch turns a commit into email, and it uses the title on the Subject line and the rest of the commit in the body.
  • git add - Add file contents to the index
  • git rm - Remove files from the working tree and index
  • git mv - Move or rename files
  • git merge - Join two or more development histories together
  • git commit-tree - Create a new commit object

Build docs developers (and LLMs) love