Git 常用命令教程
Git 常用命令教程(入门到日常使用)
Section titled “Git 常用命令教程(入门到日常使用)”适合写博客直接发布的 Git 基础教程,涵盖 最常用命令 + 使用场景 + 示例。 Markdown 文件,可直接复制或保存为
git-guide.md
一、Git 是什么?
Section titled “一、Git 是什么?”Git 是一个 分布式版本控制系统,用于:
- 追踪代码变化
- 多人协作开发
- 回滚历史版本
- 管理分支
二、Git 基本配置(第一次使用必做)
Section titled “二、Git 基本配置(第一次使用必做)”git config --global user.name "Your Name"git config --global user.email "you@example.com"查看配置:
git config --list三、创建仓库(Repository)
Section titled “三、创建仓库(Repository)”1. 初始化本地仓库
Section titled “1. 初始化本地仓库”git init2. 克隆远程仓库
Section titled “2. 克隆远程仓库”git clone https://github.com/user/repo.git四、工作区 / 暂存区 / 仓库
Section titled “四、工作区 / 暂存区 / 仓库”Git 有三个核心区域:
- 工作区(Working Directory):你正在编辑的文件
- 暂存区(Staging Area):准备提交的文件
- 仓库(Repository):已提交的历史记录
五、文件状态查看
Section titled “五、文件状态查看”git status常见状态:
untracked:未被 Git 管理modified:已修改staged:已加入暂存区
六、添加与提交
Section titled “六、添加与提交”1. 添加文件到暂存区
Section titled “1. 添加文件到暂存区”git add file.txt # 添加单个文件git add . # 添加所有改动2. 提交到仓库
Section titled “2. 提交到仓库”git commit -m "commit message"七、查看历史记录
Section titled “七、查看历史记录”git log简洁模式:
git log --oneline --graph --decorate八、撤销与回退(非常重要)
Section titled “八、撤销与回退(非常重要)”1. 撤销工作区修改(未 add)
Section titled “1. 撤销工作区修改(未 add)”git checkout -- file.txt或(新版本):
git restore file.txt2. 撤销暂存区(已 add)
Section titled “2. 撤销暂存区(已 add)”git restore --staged file.txt3. 回退到某次提交
Section titled “3. 回退到某次提交”git reset --hard commit_id⚠️
--hard会丢失修改,慎用
九、分支操作(核心)
Section titled “九、分支操作(核心)”1. 查看分支
Section titled “1. 查看分支”git branch2. 创建分支
Section titled “2. 创建分支”git branch dev3. 切换分支
Section titled “3. 切换分支”git checkout dev或:
git switch dev4. 创建并切换
Section titled “4. 创建并切换”git checkout -b dev十、合并分支
Section titled “十、合并分支”git merge dev如果发生冲突:
- 手动解决冲突
git add .git commit
十一、远程仓库(GitHub / GitLab)
Section titled “十一、远程仓库(GitHub / GitLab)”1. 查看远程仓库
Section titled “1. 查看远程仓库”git remote -v2. 添加远程仓库
Section titled “2. 添加远程仓库”git remote add origin https://github.com/user/repo.git3. 推送代码
Section titled “3. 推送代码”git push origin main首次推送:
git push -u origin main4. 拉取代码
Section titled “4. 拉取代码”git pull十二、解决“本地和远程不同步”
Section titled “十二、解决“本地和远程不同步””git fetchgit statusgit pull --rebase十三、常见场景速查表
Section titled “十三、常见场景速查表”| 场景 | 命令 |
|---|---|
| 初始化仓库 | git init |
| 查看状态 | git status |
| 添加所有文件 | git add . |
| 提交 | git commit -m "msg" |
| 查看日志 | git log --oneline |
| 创建分支 | git checkout -b dev |
| 合并分支 | git merge dev |
| 推送 | git push |
| 拉取 | git pull |
十四、推荐 .gitignore 示例
Section titled “十四、推荐 .gitignore 示例”node_modules/dist/.cache/.env.DS_Store十五、写在最后
Section titled “十五、写在最后”Git 的核心只有一句话:
小步提交,频繁提交,永远写清楚 commit message
这份文档适合:
- Git 初学者
- 日常开发速查
- 博客 / 教程发布
完。