1418 字
7 分钟
git - 常用命令
commit 规范
可以创建一个 commit_msg.txt 文件, 然后使用 git commit -F commit_msg.txt 来规范格式
举例:
fix(login): 修复登录页加载失败的问题
因为 API 返回的数据结构变了,导致解析失败, 这里调整了逻辑, 详情如下:
- 1
- 2
| 类型 | 说明 |
|---|---|
| feat | 新功能 |
| fix | 修 bug |
| refactor | 重构, 不新增功能, 也不修 bug |
| docs | 改文档, 如 README |
| style | 改代码风格, 不影响功能 |
| test | 加测试, 改测试 |
| chore | 杂项, 比如改 .gitignore, 构建脚本 |
| perf | 性能优化 |
| ci | CI/CD 相关改动 |
| build | 改构建系统或者依赖 |
| revert | 回滚某个提交 |
1. 配置 Git
初始化 Git 配置
git config --global user.name "Your Name" # 设置用户名git config --global user.email "you@example.com" # 设置邮箱git config --global core.editor "vim" # 设置默认编辑器git config --global core.autocrlf input # 处理换行符(Linux/Mac)git config --global core.autocrlf true # 处理换行符(Windows)查看配置
git config --list # 查看所有配置信息2. 基本操作
初始化 Git 仓库
git init # 初始化一个本地 Git 仓库克隆远程仓库
git clone <repository-url> # 克隆远程仓库到本地3. 分支操作
查看分支
git branch # 列出本地分支git branch -r # 列出远程分支git branch -a # 列出所有分支(本地 + 远程)创建分支
git branch <branch-name> # 创建新分支切换分支
git checkout <branch-name> # 切换到某个分支git switch <branch-name> # 新版切换分支命令创建并切换分支
git checkout -b <branch-name> # 创建并切换到新分支git switch -c <branch-name> # 新版创建并切换删除分支
git branch -d <branch-name> # 删除本地分支(已合并)git branch -D <branch-name> # 强制删除本地分支(未合并)推送本地分支到远程
git push origin <branch-name>删除远程分支
git push origin --delete <branch-name>4. 提交代码
添加文件到暂存区
git add <file-name> # 添加单个文件git add . # 添加当前目录的所有文件git add *.txt # 添加所有 .txt 文件查看当前状态
git status # 查看工作区和暂存区的状态提交代码到本地仓库
git commit -m "commit message" # 提交并附带提交信息git commit # 启动编辑器进行提交修改最后一次提交
git commit --amend # 修改上一次的提交信息或文件5. 查看历史
查看提交历史
git log # 查看完整提交历史git log --oneline # 简化输出(每个提交一行)git log --graph --oneline # 显示分支图git log -p # 查看每次提交的代码改动查看某文件的修改历史
git log -- <file-name> # 查看某个文件的历史6. 回滚与撤销
撤销文件修改
git checkout -- <file-name> # 撤销工作区文件修改(本地未 add)撤销暂存区文件
git reset HEAD <file-name> # 将暂存区文件撤回到工作区回滚到指定提交
git reset --soft <commit-id> # 回滚到某提交,保留工作区文件和暂存区git reset --mixed <commit-id> # 回滚到某提交,保留工作区文件,清空暂存区git reset --hard <commit-id> # 回滚到某提交,清空工作区和暂存区7. 同步远程仓库
查看远程仓库
git remote -v # 查看远程仓库地址添加远程仓库
git remote add origin <repository-url> # 绑定远程仓库拉取代码
git pull origin <branch-name> # 拉取远程分支代码并合并到当前分支推送代码
git push origin <branch-name> # 推送当前分支到远程仓库8. 合并与冲突解决
合并分支
git merge <branch-name> # 将指定分支合并到当前分支解决冲突
-
打开冲突文件,手动解决冲突。
-
将解决冲突后的文件添加到暂存区:
Terminal window git add <file-name> -
提交合并:
Terminal window git commit
9. 标签操作
创建标签
git tag <tag-name> # 创建轻量级标签git tag -a <tag-name> -m "message" # 创建带注释的标签查看标签
git tag # 查看所有标签推送标签到远程
git push origin <tag-name> # 推送指定标签git push origin --tags # 推送所有标签删除标签
git tag -d <tag-name> # 删除本地标签git push origin --delete <tag-name> # 删除远程标签10. Stash(存储现场)
保存当前工作现场
git stash # 保存未提交的修改git stash save "message" # 保存并附加描述查看所有存储的现场
git stash list恢复工作现场
git stash apply # 应用最近一次存储的现场git stash pop # 应用最近一次存储的现场并且将此次 stash 从 stash list 中 删除git stash apply stash@{n} # 应用指定的现场删除存储的现场
git stash drop stash@{n} # 删除指定的存储git stash clear # 清空所有存储11. 高级操作
比较差异
git diff # 查看工作区和暂存区的差异git diff --cached # 查看暂存区和上一次提交的差异git diff <branch-name> # 比较当前分支和指定分支的差异git diff <commit-id1> <commit-id2> # 比较两次提交的差异Rebasing(变基)
git rebase <branch-name> # 将当前分支的提交变基到指定分支Cherry-pick(挑拣提交)
git cherry-pick <commit-id> # 应用某次提交到当前分支12. 清理操作
删除未追踪的文件
git clean -f # 删除未追踪的文件git clean -fd # 删除未追踪的文件和文件夹清理缓存
git rm --cached <file-name> # 从暂存区删除文件但保留在工作区13. 常见问题解决
忽略某些文件
创建 .gitignore 文件并写入需要忽略的文件或 :
*.log*.pycnode_modules/plaintext123修改远程仓库地址
git remote set-url origin <new-url>