LinuxCommandLibrary

git-commit

Record changes to the repository

TLDR

Commit staged files to the repository with a message

$ git commit [[-m|--message]] "[message]"
copy

Commit staged files with a message read from a file
$ git commit [[-F|--file]] [path/to/commit_message_file]
copy

Auto stage all modified and deleted files and commit with a message
$ git commit [[-a|--all]] [[-m|--message]] "[message]"
copy

Commit staged files and sign them with the specified GPG key (or the one defined in the configuration file if no argument is specified)
$ git commit [[-S|--gpg-sign]] [key_id] [[-m|--message]] "[message]"
copy

Update the last commit by adding the currently staged changes, changing the commit's hash
$ git commit --amend
copy

Commit only specific (already staged) files
$ git commit [path/to/file1 path/to/file2 ...]
copy

Create a commit, even if there are no staged files
$ git commit [[-m|--message]] "[message]" --allow-empty
copy

SYNOPSIS

git commit [options] [--] [paths...]

PARAMETERS

-a, --all
    Stage all modified/deleted tracked files automatically

-m , --message=
    Use given message for commit

--amend
    Amend last commit (reset author/date/message)

-F , --file=
    Read message from file

--author=
    Override author name/email

--date=
    Override author date

-c , --reuse-message=
    Reuse message from specified commit

-C , --reedit-message=
    Reuse/edit message from commit

--squash=
    Merge message with commit

--fixup=
    Create fixup commit for autosquash

-s, --signoff
    Add Signed-off-by line

-S[], --gpg-sign[=]
    GPG-sign commit

--no-gpg-sign
    Do not GPG-sign

-u, --untracked-files[=]
    Include untracked files (all|normal|no)

--allow-empty
    Allow empty commit

--allow-empty-message
    Allow empty commit message

--dry-run
    Simulate commit without creating

-v, --verbose
    Show diff in commit message template

--cleanup=
    Clean message (default|verbatim|strip|whitespace)

-i, --interactive
    Interactive commit (patch mode)

-p, --patch
    Patch mode for selective hunks

--no-verify
    Bypass pre-commit hooks

-b , --branch=
    Create new branch if needed

--edit
    Prompt for commit message editor

--no-edit
    Use latest message without edit

--short
    Short status in message template

--status
    Include status in message template

-q, --quiet
    Suppress commit summary

--trailers [(=|:)]
    Add trailers to message

DESCRIPTION

The git commit command captures a snapshot of the project's current state, based on the tracked files and staged changes, and saves it as a commit object in the local Git repository. Each commit has a unique identifier (SHA-1 hash), a message describing the changes, an author, and a timestamp.

Before committing, use git add to stage files. Unstaged changes are ignored. The commit message is crucial for tracking history; it should be concise yet informative, often following conventional formats like starting with a verb and limiting the first line to 50 characters.

Commits create a permanent record in the repository's history. They enable branching, merging, and reverting. Without a message, Git opens an editor (default Vim or configured via core.editor). Use -m for inline messages. Amend last commit with --amend to fix mistakes. Empty commits are possible with --allow-empty, useful for tags or CI.

Integrates with hooks for pre-commit checks. Supports signing with -S for GPG verification. Essential for version control workflow.

CAVEATS

Commits are immutable; use git reset or git commit --amend carefully to avoid losing history. Does not push changes remotely—use git push. Ignores unstaged files unless -a. Hooks may block commits.

COMMON USAGE

git commit -m "Fix bug"
git commit -am "Update docs"
git commit --amend --no-edit

MESSAGE GUIDELINES

First line: 50 chars summary.
Body: Explain 'what' and 'why', not 'how'. Use imperative mood.

HISTORY

Introduced in Git 1.0.0 (2005) by Linus Torvalds as core plumbing command. Evolved with options like --amend (v1.5.2, 2007), -S signing (v1.7.9, 2011), and trailers (v2.13, 2017). Central to Git's distributed model.

SEE ALSO

Copied to clipboard