Synopsis
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:- By using
git addto incrementally “add” changes to the index before using the commit command - By using
git rmto remove files from the working tree and the index - By listing files as arguments to the commit command, in which case the commit will ignore changes staged in the index
- By using the
-aswitch to automatically “add” changes from all known files and “rm” files that have been removed - By using the
--interactiveor--patchswitches to decide one by one which files or hunks should be part of the commit
git reset.
Common Usage
Options
Basic Options
Basic 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.Commit Message Options
Commit Message Options
-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.Amending and Fixing
Amending and Fixing
--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.Commit Behavior Options
Commit Behavior Options
--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 and Signing Options
Author and Signing Options
Cleanup Options
Cleanup Options
--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 lineswhitespace- Same as strip except commentary is not removedverbatim- Do not change the message at allscissors- Same as whitespace except everything from the scissors line is truncateddefault- Same as strip if the message is to be edited, otherwise whitespace
Examples
Basic commit workflow
Commit with automatic staging
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
Commit specific files
Makefile. The changes staged for hello.c and hello.h are not included in the resulting commit.
Amend the last commit
Commit after resolving merge conflicts
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.
Related Commands
- 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
