mirror of
https://github.com/mintlify/docs.git
synced 2026-09-14 13:35:46 +08:00
169 lines
3.1 KiB
Plaintext
169 lines
3.1 KiB
Plaintext
---
|
|
title: git commit
|
|
description: Record changes to the repository
|
|
---
|
|
|
|
Save your staged changes to the repository with a descriptive message.
|
|
|
|
## Syntax
|
|
|
|
```bash
|
|
git commit [options]
|
|
```
|
|
|
|
## Description
|
|
|
|
`git commit` creates a snapshot of your staged changes and adds it to the project history. Each commit has a unique ID and includes a message describing what changed.
|
|
|
|
## Common usage
|
|
|
|
Commit with a message:
|
|
|
|
```bash
|
|
git commit -m "Add user authentication"
|
|
```
|
|
|
|
Commit with a detailed message:
|
|
|
|
```bash
|
|
git commit -m "Add user authentication" -m "Implement JWT-based auth system with password hashing"
|
|
```
|
|
|
|
## Options
|
|
|
|
<ParamField path="-m, --message" type="string">
|
|
Specify commit message inline
|
|
|
|
```bash
|
|
git commit -m "Your commit message"
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="-a, --all" type="flag">
|
|
Automatically stage modified and deleted files before committing
|
|
|
|
```bash
|
|
git commit -am "Update documentation"
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--amend" type="flag">
|
|
Modify the most recent commit
|
|
|
|
```bash
|
|
git commit --amend -m "Corrected message"
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--no-verify" type="flag">
|
|
Skip pre-commit and commit-msg hooks
|
|
</ParamField>
|
|
|
|
## Examples
|
|
|
|
### Basic commit
|
|
|
|
```bash
|
|
git add .
|
|
git commit -m "Fix navigation bug"
|
|
```
|
|
|
|
### Commit all tracked changes
|
|
|
|
Skip `git add` for tracked files:
|
|
|
|
```bash
|
|
git commit -am "Update API documentation"
|
|
```
|
|
|
|
### Multi-line commit message
|
|
|
|
```bash
|
|
git commit -m "Add payment processing" -m "- Integrate Stripe API
|
|
- Add payment form validation
|
|
- Create payment history page"
|
|
```
|
|
|
|
### Amend the last commit
|
|
|
|
Fix the previous commit message:
|
|
|
|
```bash
|
|
git commit --amend -m "Better commit message"
|
|
```
|
|
|
|
Add forgotten files to last commit:
|
|
|
|
```bash
|
|
git add forgotten-file.txt
|
|
git commit --amend --no-edit
|
|
```
|
|
|
|
## Writing good commit messages
|
|
|
|
### Structure
|
|
|
|
```
|
|
Short summary (50 chars or less)
|
|
|
|
More detailed explanation if needed. Wrap at 72 characters.
|
|
Explain what changed and why, not how.
|
|
|
|
- Use bullet points for multiple changes
|
|
- Reference issue numbers if applicable
|
|
```
|
|
|
|
### Best practices
|
|
|
|
**Good commit messages:**
|
|
- `Fix broken link in API documentation`
|
|
- `Add user authentication endpoints`
|
|
- `Update dependencies to address security vulnerabilities`
|
|
|
|
**Poor commit messages:**
|
|
- `fix`
|
|
- `updates`
|
|
- `WIP`
|
|
- `asdfgh`
|
|
|
|
### Guidelines
|
|
|
|
- Use imperative mood: "Add feature" not "Added feature"
|
|
- Be specific about what changed
|
|
- Explain why, not just what
|
|
- Keep the first line under 50 characters
|
|
- Reference issues: "Fixes #123"
|
|
|
|
## Viewing commits
|
|
|
|
After committing, view your history:
|
|
|
|
```bash
|
|
git log
|
|
git log --oneline
|
|
git show HEAD
|
|
```
|
|
|
|
## Undoing commits
|
|
|
|
If you need to undo a commit:
|
|
|
|
```bash
|
|
# Undo last commit but keep changes
|
|
git reset --soft HEAD~1
|
|
|
|
# Undo last commit and discard changes
|
|
git reset --hard HEAD~1
|
|
|
|
# Create a new commit that reverses changes
|
|
git revert HEAD
|
|
```
|
|
|
|
## Related commands
|
|
|
|
- [git add](/git/add) - Stage changes
|
|
- [git status](/git/status) - Check what's staged
|
|
- [git log](/git/log) - View commit history
|
|
- [git push](/git/push) - Share commits with remote
|
|
- [git revert](/git/revert) - Undo commits safely
|