HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Create a Git commit

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
creategitcommit

Problem

The building block of version control in Git is the commit. A commit represents a unit of work that you want to save in your repository. It includes changes to files, a commit message, and metadata like the author and timestamp.
Using git commit -m <message>, you can create a new commit with the staged changes and the specified <message>. If you omit the -m option, Git will open the default text editor to enter the commit message.
> [!TIP]
>
> If you want to learn how to configure Git's default text editor before using this command, check the linked article.

Solution

# Syntax: git commit [-m <message>]

git add .
git commit -m "Fix the network bug"
# Creates a commit with the message "Fix the network bug"

git add .
git commit
# Opens the default text editor to enter the commit message


> [!TIP]
>
> If you want to learn how to configure Git's default text editor before using this command, check the linked article.
If, for whatever reason, you want to skip the pre-commit and commit-msg hooks, you can use the --no-verify option with the git commit command. Running git commit --no-verify -m <message> will commit the staged changes without running the hooks.
> [!NOTE]
>

Code Snippets

# Syntax: git commit [-m <message>]

git add .
git commit -m "Fix the network bug"
# Creates a commit with the message "Fix the network bug"

git add .
git commit
# Opens the default text editor to enter the commit message
# Syntax: git commit --no-verify -m <message>

# Make some changes to files, ones that your precommit hook might not allow
git add .
git commit --no-verify -m "Unsafe commit"
# Creates a commit with the message "Unsafe commit", without running git hooks
# Syntax: git commit --allow-empty -m <message>

git commit --allow-empty -m "Empty commit"
# Creates an empty commit with the message "Empty commit"

Context

From 30-seconds-of-code: create-commit

Revisions (0)

No revisions yet.