Add README and documentation files
- Add project README.md with overview - Add GIT_TUTORIAL.md documentation Made-with: Cursor
This commit is contained in:
364
docs/GIT_TUTORIAL.md
Normal file
364
docs/GIT_TUTORIAL.md
Normal file
@@ -0,0 +1,364 @@
|
||||
# Git 提交操作教程
|
||||
|
||||
本教程适用于 Astral3D 项目,包含日常开发中常用的 Git 操作流程。
|
||||
|
||||
---
|
||||
|
||||
## 基础配置
|
||||
|
||||
首次使用需配置用户信息(已完成):
|
||||
|
||||
```bash
|
||||
git config user.name "用户名"
|
||||
git config user.email "邮箱"
|
||||
```
|
||||
|
||||
查看配置:
|
||||
|
||||
```bash
|
||||
git config --list
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 远程仓库
|
||||
|
||||
项目远程仓库地址:`http://code-server:3000/lyf/Astral3D.git`
|
||||
|
||||
查看远程仓库:
|
||||
|
||||
```bash
|
||||
git remote -v
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 常用操作流程
|
||||
|
||||
### 1. 拉取最新代码
|
||||
|
||||
```bash
|
||||
# 拉取远程最新代码并合并到当前分支
|
||||
git pull origin master
|
||||
|
||||
# 或者使用 fetch + rebase
|
||||
git fetch origin
|
||||
git rebase origin/master
|
||||
```
|
||||
|
||||
### 2. 查看当前状态
|
||||
|
||||
```bash
|
||||
# 查看工作区状态
|
||||
git status
|
||||
|
||||
# 查看简略状态
|
||||
git status -s
|
||||
```
|
||||
|
||||
### 3. 查看文件变更
|
||||
|
||||
```bash
|
||||
# 查看未暂存的变更
|
||||
git diff
|
||||
|
||||
# 查看已暂存的变更
|
||||
git diff --cached
|
||||
|
||||
# 查看具体文件的变更
|
||||
git diff filename.java
|
||||
|
||||
# 查看最近几次提交
|
||||
git log --oneline -5
|
||||
```
|
||||
|
||||
### 4. 暂存文件
|
||||
|
||||
```bash
|
||||
# 暂存单个文件
|
||||
git add filename.java
|
||||
|
||||
# 暂存多个文件
|
||||
git add file1.java file2.java
|
||||
|
||||
# 暂存所有变更
|
||||
git add .
|
||||
|
||||
# 暂存所有 .java 文件
|
||||
git add *.java
|
||||
|
||||
# 暂存目录
|
||||
git add src/
|
||||
```
|
||||
|
||||
### 5. 取消暂存
|
||||
|
||||
```bash
|
||||
# 取消所有暂存
|
||||
git reset
|
||||
|
||||
# 取消单个文件暂存
|
||||
git reset filename.java
|
||||
```
|
||||
|
||||
### 6. 撤销修改
|
||||
|
||||
```bash
|
||||
# 撤销未暂存的修改(危险操作)
|
||||
git checkout -- filename.java
|
||||
|
||||
# 撤销所有未暂存的修改
|
||||
git checkout -- .
|
||||
|
||||
# 撤销已暂存的修改
|
||||
git reset HEAD filename.java
|
||||
git checkout -- filename.java
|
||||
```
|
||||
|
||||
### 7. 提交变更
|
||||
|
||||
```bash
|
||||
# 提交已暂存的变更
|
||||
git commit -m "提交说明"
|
||||
|
||||
# 提交并添加说明(会打开编辑器)
|
||||
git commit
|
||||
|
||||
# 修改最后一次提交(还没推送到远程)
|
||||
git commit --amend -m "新的提交说明"
|
||||
|
||||
# 将暂存和提交合并为一步(适合小改动)
|
||||
git commit -am "提交说明"
|
||||
```
|
||||
|
||||
### 8. 推送到远程
|
||||
|
||||
```bash
|
||||
# 推送到远程仓库
|
||||
git push origin master
|
||||
|
||||
# 第一次推送并设置上游分支
|
||||
git push -u origin master
|
||||
|
||||
# 简化写法(已设置上游分支后)
|
||||
git push
|
||||
```
|
||||
|
||||
### 9. 查看提交历史
|
||||
|
||||
```bash
|
||||
# 查看详细提交历史
|
||||
git log
|
||||
|
||||
# 查看简略历史
|
||||
git log --oneline
|
||||
|
||||
# 查看最近5条
|
||||
git log --oneline -5
|
||||
|
||||
# 查看分支合并历史
|
||||
git log --graph --oneline --all
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 分支操作
|
||||
|
||||
### 1. 查看分支
|
||||
|
||||
```bash
|
||||
# 查看本地分支
|
||||
git branch
|
||||
|
||||
# 查看远程分支
|
||||
git branch -r
|
||||
|
||||
# 查看所有分支
|
||||
git branch -a
|
||||
```
|
||||
|
||||
### 2. 创建分支
|
||||
|
||||
```bash
|
||||
# 基于当前分支创建新分支
|
||||
git branch feature-xxx
|
||||
|
||||
# 基于 master 创建新分支
|
||||
git branch feature-xxx master
|
||||
```
|
||||
|
||||
### 3. 切换分支
|
||||
|
||||
```bash
|
||||
# 切换到分支
|
||||
git checkout feature-xxx
|
||||
|
||||
# 创建并切换到新分支
|
||||
git checkout -b feature-xxx
|
||||
|
||||
# 切换到上一个分支
|
||||
git checkout -
|
||||
```
|
||||
|
||||
### 4. 合并分支
|
||||
|
||||
```bash
|
||||
# 切换到 master 分支
|
||||
git checkout master
|
||||
|
||||
# 合并 feature-xxx 分支到当前分支
|
||||
git merge feature-xxx
|
||||
|
||||
# 使用 rebase 合并(保持线性历史)
|
||||
git rebase feature-xxx
|
||||
```
|
||||
|
||||
### 5. 删除分支
|
||||
|
||||
```bash
|
||||
# 删除本地分支(已合并)
|
||||
git branch -d feature-xxx
|
||||
|
||||
# 强制删除本地分支
|
||||
git branch -D feature-xxx
|
||||
|
||||
# 删除远程分支
|
||||
git push origin --delete feature-xxx
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 标签操作
|
||||
|
||||
### 1. 创建标签
|
||||
|
||||
```bash
|
||||
# 创建轻量标签
|
||||
git tag v1.0.0
|
||||
|
||||
# 创建附注标签(带说明)
|
||||
git tag -a v1.0.0 -m "版本说明"
|
||||
|
||||
# 为历史提交打标签
|
||||
git tag -a v1.0.0 commitid -m "版本说明"
|
||||
```
|
||||
|
||||
### 2. 查看和推送标签
|
||||
|
||||
```bash
|
||||
# 查看标签
|
||||
git tag
|
||||
|
||||
# 查看标签详情
|
||||
git show v1.0.0
|
||||
|
||||
# 推送标签到远程
|
||||
git push origin v1.0.0
|
||||
|
||||
# 推送所有标签
|
||||
git push origin --tags
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 解决冲突
|
||||
|
||||
### 1. 识别冲突
|
||||
|
||||
```bash
|
||||
# 查看冲突文件
|
||||
git status
|
||||
```
|
||||
|
||||
冲突文件会显示为 `both modified`
|
||||
|
||||
### 2. 手动解决冲突
|
||||
|
||||
打开冲突文件,会看到类似以下标记:
|
||||
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
当前分支的代码
|
||||
=======
|
||||
合并进来的代码
|
||||
>>>>>>> branch-name
|
||||
```
|
||||
|
||||
手动修改后:
|
||||
|
||||
```bash
|
||||
# 标记冲突已解决
|
||||
git add filename.java
|
||||
|
||||
# 完成合并
|
||||
git commit
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 暂存操作(Stash)
|
||||
|
||||
用于临时保存未提交的更改:
|
||||
|
||||
```bash
|
||||
# 暂存当前更改
|
||||
git stash
|
||||
|
||||
# 暂存并添加说明
|
||||
git stash save "临时保存说明"
|
||||
|
||||
# 查看暂存列表
|
||||
git stash list
|
||||
|
||||
# 恢复暂存的更改
|
||||
git stash pop
|
||||
|
||||
# 删除暂存
|
||||
git stash drop
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 常见问题
|
||||
|
||||
### 1. 代理问题
|
||||
|
||||
如果推送时遇到 503 错误,尝试设置代理排除:
|
||||
|
||||
```bash
|
||||
export no_proxy="code-server,192.168.1.72,localhost,127.0.0.1"
|
||||
git push origin master
|
||||
```
|
||||
|
||||
### 2. 每次推送都要输入密码
|
||||
|
||||
可以配置凭证缓存:
|
||||
|
||||
```bash
|
||||
# 缓存凭证 15 分钟
|
||||
git config --global credential.helper cache
|
||||
|
||||
# 缓存更长时间(单位:秒)
|
||||
git config --global credential.helper 'cache --timeout=3600'
|
||||
```
|
||||
|
||||
### 3. 查看配置
|
||||
|
||||
```bash
|
||||
# 查看所有配置
|
||||
git config --list
|
||||
|
||||
# 查看特定配置
|
||||
git config user.name
|
||||
git config remote.origin.url
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 最佳实践
|
||||
|
||||
1. **提交前先拉取**:每次提交前先 `git pull origin master`
|
||||
2. **保持提交原子性**:一个提交只做一件事
|
||||
3. **提交说明清晰**:说明改了什么,为什么改
|
||||
4. **不要提交敏感信息**:不要提交密码、密钥等
|
||||
5. **使用 .gitignore**:排除不需要跟踪的文件(如 target/、.idea/)
|
||||
Reference in New Issue
Block a user