主题
代码提交
提交代码可分为 3 步
- 同步(fetch、pull)远程仓库的最新代码。
- 将本地暂存区中的文件提交(commit)到本地仓库。
- 再将本地仓库的代码推送(push)到远程仓库。
git fetch
git fetch
命令用于从远程仓库 获取 最新的提交,但不会自动合并到本地分支中,而是将这些提交保存到本地仓库中。
bash
# 获取默认远程仓库的所有分支数据
git fetch
# 获取指定远程仓库的所有分支数据
git fetch <remote-name>
# 获取指定远程仓库的指定分支数据
git fetch <remote-name> <branch-name>
git fetch --all
- 作用:从所有已添加的远程仓库获取所有分支的更新。
- 使用场景:当你有多个远程仓库(例如 origin 和 upstream)时,--all 可以让你从所有远程仓库中一次性拉取更新。
git fetch -p / git fetch --prune
- 作用:清理本地引用中已被远程删除的分支。
- 使用场景:当远程仓库中删除了一些分支,而你在本地 git branch -a 还可以看到这些分支的缓存信息时,使用 -p 可以清除这些无效的远程分支引用。
git pull
git pull
命令用于从远程仓库 获取 最新的提交并 自动合并(merge) 到本地分支中。
bash
# 获取并合并默认远程仓库的所有分支数据
git pull
# 获取并合并指定远程仓库的所有分支数据
git pull <remote-name>
# 获取并合并指定远程仓库的指定分支数据
git pull <remote-name> <branch-name>
git add
git add
命令只会将文件提交到 暂存区。
bash
# 提交一个文件
git add <file-path>
# 提交一个文件夹及其内容
git add <directory-path>
# 当前目录及其所有子目录中的更改添加到暂存区,但不包括删除的文件。
git add .
# 将当前目录及其所有子目录中的所有更改(包括新创建、已修改和已删除的文件)都添加到暂存区。
git add -A
git add --all
git commit
git add
命令会将 暂存区 的文件提交到 本地仓库。
bash
# 提交暂存区中的所有文件并添加提交信息。
git commit -m "<commit message>"
# 提交暂存区中指定的文件并添加提交信息。
# 其中,<file1>、<file2> 等是需要提交的文件名。
git commit -m "<commit message>" <file1> <file2> ...
git push
git push
命令会将 本地仓库 的 提交(commit) 文件提交到 远程仓库。
bash
# 推送当前分支的提交到默认远程仓库中的同名分支
git push
# 推送当前分支的提交到指定远程仓库中同名分支
git push <remote-name>
# 推送当前分支的提交到指定远程仓库的指定分支
git push <remote-name> <branch-name>
信息获取
git log
bash
git log
git log --oneline
git log -p <file-path>
git show
bash
git show
git show <commit-hash>
git show --name-only <commit-hash>
约定式提交
基本格式
bash
<type>[optional scope]: <subject>
[optional body]
[optional footer]
示例:
md
feat(api): add support for user profile updates
This commit introduces a new endpoint to allow users to update their
profiles. It includes both the backend API and associated frontend changes.
Key changes:
- Added `PATCH /api/users/:id` endpoint to handle user profile updates.
- Implemented input validation to ensure data integrity.
- Updated frontend components to include a new profile editing form.
- Added unit and integration tests to cover edge cases like invalid input.
Motivation:
Previously, users could only view their profiles but couldn't edit them.
This feature enhances the user experience and aligns with the product
roadmap for Q1.
Breaking changes:
- None.
Future work:
- Add support for uploading profile pictures.
- Optimize input validation for large-scale data submissions.
Refs: #123, #456
type
feat
: 新功能,新需求fix
: 修复 bugdocs
: 仅仅修改了文档,比如 README, CHANGELOG, CONTRIBUTE 等等style
: 仅仅修改了空格、格式缩进、逗号等等,不改变代码逻辑refactor
: 代码重构,没有加新功能或者修复 bugtest
: 测试用例,包括单元测试、集成测试等revert
: 回滚到上一个版本perf
: 性能优化chore
: 改变构建流程、或者增加依赖库、工具等,包括打包和发布版本conflict
: 解决合并过程中的冲突