目录
1 前言
在日常开发、团队协作、开源项目维护中,Git 是版本控制的必备工具。很多同学命令记混乱、只会简单提交、遇到代码冲突 / 回退 / 分支合并就手足无措。
本文完全按实际开发场景分类,涵盖初始化配置、日常提交、分支开发、代码回退、远程协作、临时存代码、版本打标、冲突解决等高频场景,命令拿来即用,适合收藏、复习、面试背诵。
环境:Windows / Mac / Linux 通用
适用:个人开发、多人团队协作、GitHub/Gitee/GitLab
本文以github演示 =>>
2 初次使用:环境全局配置场景
首次安装 Git 必须配置用户名和邮箱,所有提交记录都会绑定该信息。
# 查看全部配置
git config --list
# 全局配置用户名(所有项目生效)
git config --global user.name "你的GitHub用户名"
# 全局配置提交邮箱(绑定GitHub注册邮箱)
git config --global user.email "你的GitHub注册邮箱"
3 本地已有项目 提交到 GitHub 完整流程
# 1. Go to the local project root
cd your project folder path
# 2. Initialize as a local Git repository
git init
# 3. Add all files to the staging area
git add .
# 4. Local first commit
git commit -m "Initialize project, first commit"
# 5. Associate a remote GitHub repository
git remote add origin "GitHub repository address"
# 6. See if the remote association is successful
git remote -v
# 7. Force pull remote files for the first time (avoid conflicts, ignore if the repository is empty)
git pull origin main --allow-unrelated-histories
# 8. Push local code to the GitHub remote main branch
git push -u origin main #若默认分支为 master,将命令中 main 替换为 master
4 仓库创建场景:本地新建项目 / 拉取远程项目
场景 1:本地文件夹初始化为 Git 仓库
git init
场景 2:克隆远程已有仓库(GitHub 开源项目)
# HTTPS 方式
git clone https://github.com/xxx/demo.git
# SSH 方式(免密码推送,推荐长期使用)
git clone git@github.com:xxx/demo.git
场景 3:本地项目更换关联远程仓库
# 解绑旧远程
git remote remove origin
# 绑定新GitHub仓库
git remote add origin 新仓库地址
5 日常开发场景:代码修改、暂存、提交
开发最频繁的一套流程:改代码 → 暂存 → 本地提交。
# 查看文件状态(红色未暂存、绿色已暂存)
git status
# 查看具体代码修改差异
git diff
# 1. 暂存:单个文件
git add index.js
# 2. 暂存:所有新增/修改文件(日常最常用)
git add .
# 3. 本地提交,必须填写提交备注
git commit -m "新增登录页面,修复按钮样式bug"
# 简化操作:已追踪文件直接 add+commit 一步执行
git commit -am "优化接口请求逻辑"
6 查看历史场景:查看提交日志、版本记录
# 完整日志(作者、时间、备注、版本id)
git log
# 精简单行日志,适合快速浏览
git log --oneline
# 图形化展示分支合并、提交走向
git log --graph --oneline --all
7 版本回退场景:写错代码、提交错误如何撤销
开发高频痛点,分三种撤销层级:未暂存、已暂存、已提交。
场景 1:修改了代码,还没 add,撤销改动
# 撤销单个文件工作区修改
git checkout -- index.js
# 撤销所有未暂存的本地修改
git checkout .
场景 2:已经 add 暂存,想撤回暂存
# 取消单个文件暂存
git reset HEAD index.js
# 取消全部暂存
git reset HEAD .
场景 3:已经 commit 提交,需要回退版本
# 软回退:保留代码改动,只撤销提交记录(适合改完重新提交)
git reset --soft 版本ID
# 混合回退(默认):撤销提交+取消暂存,保留本地代码
git reset 版本ID
# 硬回退:彻底删除当前版本后所有代码改动【谨慎使用】
git reset --hard 版本ID
场景 4:修改上一次提交备注
git commit --amend
8 分支管理场景:多人协作、功能并行开发
Git 核心能力,主干分支稳定,功能分支开发 是团队规范。
1. 分支查看与创建
# 查看本地所有分支
git branch
# 查看本地+远程所有分支
git branch -a
# 新建分支(不切换)
git branch dev
# 新建分支并直接切换(最常用)
git checkout -b dev
2. 分支切换与合并
# 切换到指定分支
git checkout main
# 将 dev 分支代码合并到当前 main 分支
git merge dev
3. 分支删除
# 删除已合并的本地分支
git branch -d dev
# 强制删除未合并的分支(谨慎)
git branch -D dev
9 远程协作场景:推送代码、拉取最新代码
对接 GitHub 远程仓库,多人同步代码。
# 拉取远程最新代码,合并到本地
git pull
# 推送本地当前分支到远程
git push
# 第一次推送当前分支,绑定远程关联
git push -u origin dev
# 推送指定本地分支到远程
git push origin dev
# 删除远程分支
git push origin --delete dev
10 临时存档场景:紧急切分支,代码不想提交
正在开发一半,临时需要切换分支改 bug,代码不想强行提交,使用 stash 储藏。
# 储藏当前所有未提交的修改
git stash
# 查看所有储藏记录
git stash list
# 恢复最近一次储藏,并删除储藏记录
git stash pop
# 仅恢复储藏,保留记录
git stash apply
# 清空所有储藏内容
git stash clear
11 版本发布场景:打标签 Tag 管理版本
项目迭代发版(v1.0、v1.1)使用标签管理稳定版本。
# 新建本地标签
git tag v1.0.0
# 查看所有标签
git tag
# 推送单个标签到远程
git push origin v1.0.0
# 推送所有标签
git push origin --tags
# 删除本地标签
git tag -d v1.0.0
# 删除远程标签
git push origin --delete tag v1.0.0

2万+

被折叠的 条评论
为什么被折叠?



