Submit updates (git commit)
Submitting Updates (git commit)
Submitting updates is one of the core operations in Git version control. It permanently saves the changes in the staging area to the local repository, creating a new version record. Each commit generates a unique SHA-1 hash as an identifier.
Basic Commit Operation
The simplest commit command only requires adding a commit message:
git commit -m "Fix login page styling issue"
This command will commit all staged changes to the local repository. The commit message should concisely describe the changes made.
Viewing Commit History
After committing, you can use git log
to view the commit history:
git log --oneline
Example output:
a1b2c3d (HEAD -> main) Fix login page styling issue
e4f5g6h Initialize project structure
Amending the Last Commit
If you find an error in the commit message or missed a file after committing, you can use the --amend
option to modify the last commit:
git commit --amend -m "Fix login page button styling issue"
To add a missed file:
git add forgot-file.js
git commit --amend --no-edit
Best Practices for Multi-File Commits
It is recommended to commit logically related changes together and separate unrelated changes. For example:
# Commit login-related changes
git add login.html login.css login.js
git commit -m "Implement frontend logic for login page"
# Commit registration-related changes
git add register.html register.css register.js
git commit -m "Complete form validation for registration page"
Commit Message Standards
Good commit messages should follow this format:
Type(Scope): Brief description
Detailed description (optional)
Related issue (optional)
Example:
fix(auth): Fix password strength validation logic
Password strength validation now checks for special characters
Minimum length requirement increased from 6 to 8
Closes #123
Common types include:
- feat: New feature
- fix: Bug fix
- docs: Documentation changes
- style: Code formatting
- refactor: Code refactoring
- test: Test-related changes
- chore: Build process or auxiliary tool changes
Interactive Committing
For complex changes, you can use interactive committing:
git commit -p
This will display each modified chunk and ask whether to include it in the current commit. It is ideal for splitting a large change into multiple logical commits.
Empty Commits
Sometimes you may need to create a commit without any changes, such as to trigger CI/CD:
git commit --allow-empty -m "Trigger deployment process"
Commit Signature Verification
To ensure the authenticity of commits, you can add a GPG signature:
git commit -S -m "Security update"
This requires prior configuration of a GPG key.
Commit Templates
You can create commit message templates to improve consistency:
git config --global commit.template ~/.gitmessage
Example template file:
# <Type>(<Scope>): <Subject>
# |<---- Limit to 50 characters ---->|
# Detailed description, wrap at 72 characters
# Optional footer
# Closes #<issue number>
Commit Hooks
Git supports automatic script execution before or after commits. For example, add the following to .git/hooks/pre-commit
:
#!/bin/sh
npm run lint
This ensures code passes linting checks before each commit.
Undoing Commits
If you commit incorrect changes, you can use:
git reset HEAD~1
This undoes the last commit but keeps the changes in the working directory. To discard changes completely:
git reset --hard HEAD~1
Partial Commits
Sometimes you only need to commit part of a file's changes:
git add -p
Then select the chunks to stage and commit.
Specifying Commit Scope
You can specify which paths to commit:
git commit src/ utils/ -m "Update utility functions"
Commit Strategy Recommendations
- Small, frequent commits are better than large, infrequent ones.
- Each commit should be a complete, working change.
- Commit messages should clearly express the intent of the change.
- Avoid committing auto-generated or temporary files.
- The commit history on public branches should remain clean.
Cross-Platform Line Ending Issues
Different line endings between Windows and Unix systems may cause entire files to appear modified. Configure Git to handle this automatically:
git config --global core.autocrlf true
Relationship Between Commits and Branches
Each commit creates a new version based on the current branch. Use git branch --contains <commit>
to see which branches contain a specific commit.
Binary Search for Problematic Commits
When a bug is introduced but the exact commit is unknown:
git bisect start
git bisect bad
git bisect good a1b2c3d
Git will automatically perform a binary search until it finds the problematic commit.
Commit Statistics
View commit statistics:
git shortlog -sn
This shows the number of commits per contributor.
Comparing Commit Differences
Compare differences between two commits:
git diff a1b2c3d..e4f5g6h
Rewriting Commit History
For already pushed commits, you can use interactive rebase to modify:
git rebase -i HEAD~3
You can then reorder, merge, or edit commit messages. Note that this changes commit hashes.
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
下一篇:跳过暂存区域直接提交