name: git-commit-push
# 查看工作区状态
git status
# 查看具体改动
git diff
# 查看已暂存的改动
git diff --cached
# 暂存所有修改
git add .
# 暂存指定文件
git add <文件路径>
# 暂存指定目录
git add <目录路径>
# 交互式暂存(选择性暂存)
git add -p
# 提交并写提交信息
git commit -m "提交信息"
# 打开编辑器写详细提交信息
git commit
# 暂存并提交(跳过 git add)
git commit -am "提交信息"
# 推送到当前分支的远程分支
git push
# 推送到指定远程仓库的指定分支
git push origin main
# 首次推送并设置上游分支
git push -u origin <分支名>
# 强制推送(谨慎使用!)
git push -f
git push --force-with-lease # 更安全的强制推送
# 推送指定标签
git push origin <标签名>
# 推送所有标签
git push --tags
# 修改提交信息
git commit --amend -m "新的提交信息"
# 添加遗漏的文件到最后一次提交
git add <遗漏的文件>
git commit --amend --no-edit
# 修改提交信息和内容
git commit --amend
# 如果已经推送过,需要强制推送
git push --force-with-lease
# 撤销所有暂存
git reset HEAD
# 撤销指定文件的暂存
git reset HEAD <文件路径>
# 或使用 restore(推荐)
git restore --staged <文件路径>
# 撤销指定文件的修改
git checkout -- <文件路径>
# 或使用 restore(推荐)
git restore <文件路径>
# 撤销所有未暂存的修改
git checkout .
# 撤销最后一次提交,保留修改
git reset --soft HEAD~1
# 撤销最后一次提交,取消暂存但保留修改
git reset HEAD~1
# 撤销最后一次提交,丢弃所有修改(危险!)
git reset --hard HEAD~1
# 撤销最近 N 次提交
git reset --soft HEAD~N
# 暂存当前修改
git stash
# 暂存并添加说明
git stash save "暂存说明"
# 暂存包括未跟踪的文件
git stash -u
# 查看暂存列表
git stash list
# 恢复最近的暂存
git stash pop
# 恢复指定的暂存
git stash apply stash@{0}
# 删除暂存
git stash drop stash@{0}
# 清空所有暂存
git stash clear
# 查看提交历史
git log
# 简洁的单行显示
git log --oneline
# 查看最近 N 次提交
git log -n 5
# 查看图形化分支历史
git log --graph --oneline --all
# 查看指定文件的提交历史
git log <文件路径>
# 查看指定作者的提交
git log --author="作者名"
git add .
git commit -m "feat: 添加新功能"
git push
git commit --amend -m "新的提交信息"
git push --force-with-lease
# 暂存当前工作
git stash
# 切换分支处理其他事情
git checkout other-branch
# 切回来恢复工作
git checkout original-branch
git stash pop
# 撤销提交但保留修改
git reset --soft HEAD~1
# 修改代码
# ...
# 重新提交
git add .
git commit -m "正确的提交"
# 撤销提交
git reset --hard HEAD~1
# 强制推送(谨慎!)
git push --force-with-lease
# 查看冲突文件
git status
# 手动编辑解决冲突,或选择使用某一方的版本
git checkout --ours <文件> # 使用本地版本
git checkout --theirs <文件> # 使用远程版本
# 标记为已解决
git add <文件>
# 完成合并
git commit -m "解决合并冲突"
# 或者放弃本次合并
git merge --abort
建议使用语义化提交信息格式:
<类型>(<范围>): <简短描述>
<详细描述>
<关联信息>
常用类型:
feat: 新功能fix: 修复 bugdocs: 文档修改style: 代码格式调整(不影响功能)refactor: 重构(既不是新功能也不是修复)perf: 性能优化test: 测试相关chore: 构建、工具或配置修改示例:
git commit -m "feat(账号组): 添加批量删除功能"
git commit -m "fix(广告计划): 修复创建时的参数校验问题"
git commit -m "refactor(utils): 优化水印添加工具类"
git log 和 git diff 确认要推送的内容-f,使用 --force-with-lease 更安全# 设置默认编辑器
git config --global core.editor "vim"
# 设置提交模板
git config --global commit.template ~/.gitmessage
# 显示中文文件名
git config --global core.quotepath false
# 设置默认推送行为
git config --global push.default current
# 设置 pull 默认使用 merge
git config --global pull.rebase false