Git 简单笔记

Git 使用封面图

1. 最基础的工作流

Git 基本流程

三步走:工作区 → 暂存区 → 仓库

Terminal window
1
git init # 初始化仓库
2
git status # 查看当前状态
3
git add . # 添加到暂存区
4
git commit -m "消息内容" # 提交到本地仓库

2. 常用配置

Terminal window
1
git config --global user.name "Your Name" # 配置用户名
2
git config --global user.email "you@example.com" # 配置邮箱

3. 分支(够用版)

Terminal window
1
git checkout -b feature/login # 新建并切换分支
2
git checkout main # 切回主分支
3
git merge feature/login # 合并分支

4. 远程协作(最常用)

Terminal window
1
git remote add origin git@github.com:your/repo.git # 绑定远程仓库
2
git push -u origin main # 首次推送并关联
3
git pull # 拉取远程更新

5. 撤销与回滚(常见场景)

场景命令说明
修改了文件但不想要git checkout -- file回到上次提交
已 add 但不想提交git reset file从暂存区移出
提交错了git revert <commit>生成反向提交

6. .gitignore

1
node_modules/
2
.DS_Store
3
dist/
4
*.log

7. 快速命令清单

Terminal window
1
git status # 查看当前状态
2
git add . # 添加到暂存区
3
git commit -m "msg" # 提交到本地仓库
4
git checkout -b feat/xxx # 新建并切换分支
5
git merge feat/xxx # 合并分支
6
git pull # 拉取远程更新
7
git push # 推送到远程仓库
8
git log # 查看历史记录
9
git reset --hart 版本号 # 版本穿梭