Skip to content

Commit c2722aa

Browse files
lurenjia1213Plucky923
authored andcommitted
docs:Add examples for CI/CD,add reset-diff-and-commit,Corrected previous errors in 2-staging
Signed-off-by: lurenjia1213 <[email protected]>
1 parent b3d4494 commit c2722aa

File tree

4 files changed

+184
-6
lines changed

4 files changed

+184
-6
lines changed

docs/ch3/sec1/subsec1/2-code-hosting-platforms.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
- **是什么**:自动化测试、构建和部署的工具,就像你在 Steam 上设置了自动更新游戏,每次有新版本都会自动下载安装。
9898
- **为什么重要**:提升开发效率,确保代码质量,就像你设置了自动回复,不用每次手动处理重复的事情。
9999
- **使用场景**:自动化测试、持续集成/持续交付,比如每次提交代码后,自动运行测试并发布新版本。
100-
100+
- **案例**:[开源操作系统训练营的自动化测评](https://github.com/LearningOS/template-2024a-rcore/blob/ch8/.github/workflows/build.yml)[本教程](https://github.com/hust-open-atom-club/intro2oss/actions)
101101
---
102102

103103
## 常用代码托管平台

docs/ch3/sec1/subsec2/2-staging.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050
如果你使用的是
5151

5252
```bash
53-
git .
53+
git add .
5454
```
5555

5656
那么将把当前所在目录的所有文件添加进暂存区
5757

58-
???+ tip“小提示”
59-
可以把构建产物的文件路径加入 `.gitignore` 文件中,避免被错误加入暂存区
58+
59+
>可以把构建产物的文件路径加入 `.gitignore` 文件中,避免被错误加入暂存区
6060
6161
#### 交互式添加
6262

@@ -69,8 +69,8 @@
6969
**作用**:进入交互模式,逐块(hunk)选择要添加到暂存区的修改。Git 会显示每个修改块,并提示是否将其添加到暂存区。
7070
**使用场景**:当需要对修改进行精细控制时,可以使用交互式添加。
7171

72-
??? tip“类比一下”
73-
git rmgit mv 的用法与 git add 差不多
72+
73+
>`git rm``git mv` 的用法与 `git add` 差不多
7474
7575
---
7676

@@ -104,6 +104,9 @@
104104

105105
**作用**:将所有暂存的文件移回工作目录。
106106

107+
108+
>`git restore` 是在 Git 2.23 引入的,在此之前,使用 `git checkout` 从某个提交恢复文件
109+
107110
## 总结
108111

109112
通过本节的学习,学生应掌握以下内容:
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
## Git 暂存区(续)
2+
3+
### 4. Git Reset
4+
前面我们提到了 `git restore`,与其不同,`git reset` 会移动头指针,并且可以选择性地修改暂存区和工作目录。
5+
6+
以下是 `git reset` 的常见用法:
7+
8+
#### 撤销暂存区的修改(保留工作目录的更改)
9+
10+
**命令**
11+
12+
```bash
13+
git reset
14+
```
15+
16+
**作用**:将暂存区的内容重置为最后一次提交的状态,但保留工作目录中的修改。
17+
18+
#### 撤销暂存区和工作目录的修改
19+
20+
**命令**
21+
22+
```bash
23+
git reset --hard
24+
```
25+
26+
**作用**:将暂存区和工作目录的内容都重置为最后一次提交的状态。**注意**:这将丢弃所有未提交的更改,使用时要小心。
27+
28+
#### 回退到某个提交
29+
30+
**命令**
31+
32+
```bash
33+
git reset <commit-hash>
34+
```
35+
36+
**作用**:将当前分支的 HEAD 移动到指定的提交,并可以选择是否重置暂存区和工作目录。
37+
38+
- `git reset --soft <commit-hash>`:仅移动 HEAD,保留暂存区和工作目录的更改。
39+
- `git reset --mixed <commit-hash>`:移动 HEAD 并重置暂存区,但保留工作目录的更改(默认行为)。
40+
- `git reset --hard <commit-hash>`:移动 HEAD 并重置暂存区和工作目录,丢弃所有更改。
41+
- **使用场景**: 比如,在你不小心把一些奇怪的文件交上去后,可以用这个撤销。
42+
43+
---
44+
45+
### 5. Git Diff
46+
47+
`git diff` 用于比较工作目录、暂存区和仓库之间的差异。以下是 `git diff` 的常见用法:
48+
49+
#### 比较工作目录和暂存区
50+
51+
**命令**
52+
```bash
53+
git diff
54+
```
55+
**作用**:显示工作目录中尚未添加到暂存区的更改。
56+
57+
**输出示例**
58+
```diff
59+
diff --git a/file.txt b/file.txt
60+
index 1234567..89abcde 100644
61+
--- a/file.txt
62+
+++ b/file.txt
63+
@@ -1,3 +1,4 @@
64+
Hello, World!
65+
-This is the old content.
66+
+This is the new content.
67+
+Added a new line.
68+
```
69+
- `---` 表示旧文件(暂存区中的文件)。
70+
- `+++` 表示新文件(工作目录中的文件)。
71+
- `-` 表示删除的行。
72+
- `+` 表示新增的行。
73+
74+
- **使用场景**:在将修改添加到暂存区之前,检查工作目录中的更改。
75+
76+
---
77+
78+
#### 比较暂存区和最后一次提交
79+
80+
**命令**
81+
82+
```bash
83+
git diff --cached
84+
```
85+
>或者是`git diff --staged`
86+
87+
**作用**:显示暂存区中尚未提交的更改。
88+
89+
#### 比较工作目录和最后一次提交
90+
91+
**命令**
92+
93+
```bash
94+
git diff HEAD
95+
```
96+
97+
**作用**:显示工作目录和最后一次提交之间的所有差异(包括暂存区和工作目录的更改)。
98+
99+
#### 比较两个提交之间的差异
100+
101+
**命令**
102+
103+
```bash
104+
git diff <commit-hash-1> <commit-hash-2>
105+
```
106+
**作用**:显示两个提交之间的差异。
107+
>类似的,可以使用`git diff <branch-1> <branch-2>` 来比较两个分支的最新提交之间的差异。
108+
109+
#### diff 的其他用法
110+
**命令与输出**:
111+
112+
```bash
113+
#统计
114+
git diff --stat
115+
docs/ch3/sec1/subsec2/3-reset-diff-and-commit.md | 25 +++++++++++++++++++++++--
116+
1 file changed, 23 insertions(+), 2 deletions(-)
117+
```
118+
使用`git diff`查看某个文件在几个 commit 之间的差异
119+
120+
命令:`git diff a6274 293c5 docs/ch3/sec1/subsec1/2-code-hosting-platforms.md`
121+
```diff
122+
123+
diff --git a/docs/ch3/sec1/subsec1/2-code-hosting-platforms.md b/docs/ch3/sec1/subsec1/2-code-hosting-platforms.md
124+
index 0a257d5..4b2f348 100644
125+
--- a/docs/ch3/sec1/subsec1/2-code-hosting-platforms.md
126+
+++ b/docs/ch3/sec1/subsec1/2-code-hosting-platforms.md
127+
@@ -97,7 +97,7 @@
128+
- **是什么**:自动化测试、构建和部署的工具,就像你在 Steam 上设置了自动更新游戏,每次有新版本都会自动下载安装。
129+
- **为什么重要**:提升开发效率,确保代码质量,就像你设置了自动回复,不用每次手动处理重复的事情。
130+
- **使用场景**:自动化测试、持续集成/持续交付,比如每次提交代码后,自动运行测试并发布新版本。
131+
-
132+
+- **案例**:[开源操作系统训练营的自动化测评](https://github.com/LearningOS/template-2024a-rcore/blob/ch8/.github/workflows/build.yml),[本教程](https://github.com/hust-open-atom-club/intro2oss/actions)
133+
---
134+
135+
## 常用代码托管平台
136+
```
137+
138+
---
139+
140+
### 6. Git Commit
141+
142+
`git commit` 用于将暂存区的内容提交到本地仓库。以下是 `git commit` 的常见用法:
143+
144+
#### 提交暂存区的更改
145+
146+
**命令**
147+
148+
```bash
149+
git commit -m "提交信息"
150+
```
151+
152+
**作用**:将暂存区的内容提交到本地仓库,并附带提交信息。
153+
154+
#### 提交时跳过暂存区
155+
156+
**命令**
157+
158+
```bash
159+
git commit -a -m "提交信息"
160+
```
161+
162+
**作用**:将所有已跟踪文件的修改直接提交到本地仓库。**注意**:未跟踪的文件不会被提交。
163+
164+
#### 修改最后一次提交
165+
166+
**命令**
167+
168+
```bash
169+
git commit --amend
170+
```
171+
172+
**作用**:修改最后一次提交的内容或提交信息。
173+
174+
> 对 commit 进行签名 [参考链接](https://docs.github.com/zh/authentication/managing-commit-signature-verification)

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ nav:
102102
- 基础阶段:
103103
- Git的基础配置: ch3/sec1/subsec2/1-basic-configuration.md
104104
- 暂存区操作: ch3/sec1/subsec2/2-staging.md
105+
- 暂存区操作(续): ch3/sec1/subsec2/3-reset-diff-and-commit.md
105106
- 如何写好一个commit message: ch3/sec1/subsec2/6-commit-message.md
106107
- 专业阶段:
107108
- Git Rebase与Git Merge的使用与区别: ch3/sec1/subsec3/1-rebase-merge.md

0 commit comments

Comments
 (0)