Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit abd5fe7

Browse files
committed
docs: lab0
1 parent 0bf62b6 commit abd5fe7

17 files changed

Lines changed: 248 additions & 21 deletions

docs/lab/classroom1.png

70.5 KB
Loading

docs/lab/classroom2.png

66.8 KB
Loading

docs/lab/classroom3.png

79.5 KB
Loading

docs/lab/classroom4.png

147 KB
Loading

docs/lab/github1.png

176 KB
Loading

docs/lab/lab0.md

Lines changed: 248 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -159,36 +159,36 @@ git config --global alias.cm "commit -m"
159159
160160
:::tip
161161
VS Code 原生集成了 Git,同时提供了一系列插件,例如 Git 提交树可视化插件 Git Graph。
162-
:::details
162+
:::details 示例
163163
以下是在 VS Code 中使用 Git 的演示:
164164
165-
1. 创建一个空文件夹 `Git-Test`
165+
1. 创建一个空文件夹 `test-git`
166166
2. 点击左侧边栏中的 `源代码管理` 图标。
167-
![1](Git-Test/1.png)
167+
![1](test-git/1.png)
168168
169169
3. 点击 'Initialize Repository'(这一步等同于 `git init`)。
170170
4. 新建 `main.cpp`,会出现 `U` 标记,意为 `Untracked(未跟踪的)`
171-
![2](Git-Test/2.png)
171+
![2](test-git/2.png)
172172
173173
5. 回到 `源代码管理` 界面,这里有两个 `+` 按钮。
174174
上方的 `+` 代表将项目中的所有更改添加到暂存区,相当于在项目目录下执行 `git add -A`
175175
下方的 `+` 代表将指定文件添加到暂存区,相当于执行 `git add main.cpp`
176-
![3](Git-Test/3.png)
176+
![3](test-git/3.png)
177177
178178
6. 点击上方的 `+``main.cpp` 的标记变为 `A`,意为 `Added(已暂存)`
179-
![4](Git-Test/4.png)
179+
![4](test-git/4.png)
180180
181181
7. 在文本框输入 commit message(可以是多行),点击提交,相当于执行 `git commit -m "Initial commit"`
182-
![5](Git-Test/5.png)
182+
![5](test-git/5.png)
183183
184184
8. 修改 `main.cpp`。这时 `main.cpp` 的标记会变为 `M`,意为 `Modified(已修改)`
185-
![6](Git-Test/6.png)
185+
![6](test-git/6.png)
186186
187187
9. 重复上述暂存、提交操作。
188-
![7](Git-Test/7.png)
188+
![7](test-git/7.png)
189189
190190
10. 点击下方的 `Git Graph`,可以查看 Git 提交树。
191-
![8](Git-Test/8.png)
191+
![8](test-git/8.png)
192192
193193
:::
194194
@@ -201,8 +201,7 @@ VS Code 原生集成了 Git,同时提供了一系列插件,例如 Git 提交
201201
经常性地在运行 Git 命令前运行 `git status` 是个好习惯,它可以告诉你,你当前在哪个分支下,有哪些修改还未被暂存,有哪些暂存区的文件还没被提交。
202202
203203
```bash
204-
(base) [11:45:14] zecyel@ZDesktop ~/test-git
205-
$ git status
204+
user@linux:~/test-git$ git status
206205
On branch main
207206
Changes to be committed:
208207
(use "git restore --staged <file>..." to unstage)
@@ -222,14 +221,11 @@ VS Code 原生集成了 Git,同时提供了一系列插件,例如 Git 提交
222221
如果你想查看当前有哪些分支,可以使用 `git branch` 或者 `git branch -a`。查阅资料并在报告中回答,这两条命令的区别是什么?
223222

224223
```bash
225-
(base) [15:06:54] zecyel@ZDesktop ~/test-git
226-
$ git branch dev
227-
(base) [15:24:08] zecyel@ZDesktop ~/test-git
228-
$ git branch
224+
user@linux:~/test-git$ git branch dev
225+
user@linux:~/test-git$ git branch
229226
dev
230227
* main
231-
(base) [15:24:10] zecyel@ZDesktop ~/test-git
232-
$ git branch -a
228+
user@linux:~/test-git$ git branch -a
233229
dev
234230
* main
235231
```
@@ -239,8 +235,7 @@ VS Code 原生集成了 Git,同时提供了一系列插件,例如 Git 提交
239235
可以使用 `git switch <branch-name>` 来切换到已经存在的分支。注意,你需要先保存你在当前分支上的所有文件,假如在当前分支上还有未 commit 的文件,那么这次 `git switch` 会失败。
240236

241237
```bash
242-
(base) [15:28:26] zecyel@ZDesktop ~/test-git
243-
$ git switch dev
238+
user@linux:~/test-git$ git switch dev
244239
Switched to branch 'dev'
245240
```
246241

@@ -250,4 +245,236 @@ VS Code 原生集成了 Git,同时提供了一系列插件,例如 Git 提交
250245

251246
- `git merge`
252247

253-
Await for fduTristin...
248+
`git merge` 用来把另一个分支的提交历史合并到当前分支。
249+
250+
假设当前在 main 分支,你想合并 feature 分支:
251+
252+
```bash
253+
git checkout main
254+
git merge feature
255+
```
256+
257+
合并有两种常见的结果:
258+
259+
1. Fast-forward 合并。如果 main 没有新的提交,只落后于 feature,执行 `git merge feature``main` 分支会直接“快进”到 `D`
260+
261+
```bash
262+
main: A---B
263+
feature: A---B---C---D
264+
```
265+
266+
2. 非 fast-forward 合并。如果两个分支各有提交:
267+
268+
```bash
269+
main: A---B
270+
\
271+
feature: C---D
272+
```
273+
274+
执行 `git merge feature` 后 Git 会创建一个新的合并提交(merge commit)`E`
275+
276+
```bash
277+
main: A---B-------E
278+
\ /
279+
feature: C---D
280+
```
281+
282+
:::tip
283+
284+
当两个分支修改了同一文件的同一位置,就会出现冲突(conflict),Git 无法自动合并。
285+
286+
```bash
287+
user@linux:~/test-git# git merge feature
288+
Auto-merging main.cpp
289+
CONFLICT (content): Merge conflict in main.cpp
290+
Automatic merge failed; fix conflicts and then commit the result.
291+
```
292+
293+
这时需要你手动修改文件处理冲突并提交。
294+
:::
295+
296+
## GitHub
297+
298+
GitHub 是一个 **基于 Git 的代码托管平台**,你可以将你的本地 Git 仓库上传为远程仓库,这样别人就可以拉取你的代码,与你进行协作。
299+
300+
### 配置
301+
302+
- 你需要注册一个 [GitHub](https://github.com/) 账户。
303+
304+
> [!note]
305+
>
306+
> 注册 GitHub 的邮箱和你本地 `git config` 使用的邮箱最好一致,这样远程仓库的 commit 记录才能与你的 GitHub 账户对应上。当然,一个 GitHub 账户支持绑定多个邮箱,只要你 `git config` 中的邮箱包括在其中就没问题了。
307+
308+
- 在 Github 上配置 SSH 公钥
309+
310+
1. 复制 `cat ~/.ssh/id_rsa.pub` 输出的结果(即公钥)。
311+
2. 打开 Github 并登录自己的账号。
312+
3. 点击右上角头像,进入 Settings :
313+
314+
![1](ssh-key1.png)
315+
316+
4. 进入页面后,在左侧选择 `SSH and GPG keys`, 在右侧点击 `New SSH Key`
317+
318+
![2](ssh-key2.png)
319+
320+
5. 在框中粘贴入自己复制的公钥,点击 `Add SSH key` 即可。
321+
322+
> [!note]
323+
>
324+
> SSH key 的生成参考[这个文档](/appendix/ssh-server/#_2-生成-ssh-密钥对)
325+
>
326+
> 如果你在服务器上实验,需要在服务器上生成密钥对;如果在自己电脑的虚拟机上实验,需要在 wsl 中生成;如果你以后希望在本机拉取/上传 GitHub 仓库,则需要在本机生成密钥对。
327+
328+
6. 验证配置是否成功
329+
330+
```bash
331+
user@linux:~# ssh -T git@github.com
332+
Hi <用户名>! You've successfully authenticated, but GitHub does not provide shell access.
333+
```
334+
335+
如果没有得到期望的输出,请检查密钥对配置,或参考[这个文档](/appendix/misc-qa)
336+
337+
### GitHub 基本操作
338+
339+
- `git clone`
340+
341+
克隆远程仓库到本地。
342+
343+
```bash
344+
user@linux:~$ git clone <remote URL>
345+
```
346+
347+
- `git pull`
348+
349+
拉取最新的代码。这个操作相当于 `git fetch(将远程分支拉到本地)` + `git merge(将远程分支合并入本地分支)`
350+
351+
- `git push`
352+
353+
将本地仓库的修改推送到远程仓库。
354+
355+
:::details 示例
356+
如果你想修正我们课程网页上的错误,可以在 GitHub 点进我们的仓库,点击 `Code`,再选择 `SSH`,复制这串 URL。
357+
358+
![1](github1.png)
359+
360+
在终端运行 `git clone git@github.com:ICS-25Fall-FDU/ICS-25Fall-FDU.github.io.git`
361+
362+
```bash
363+
user@linux:~# git clone git@github.com:ICS-25Fall-FDU/ICS-25Fall-FDU.github.io.git
364+
Cloning into 'ICS-25Fall-FDU.github.io'...
365+
remote: Enumerating objects: 893, done.
366+
remote: Counting objects: 100% (48/48), done.
367+
remote: Compressing objects: 100% (46/46), done.
368+
remote: Total 893 (delta 15), reused 13 (delta 2), pack-reused 845 (from 1)
369+
Receiving objects: 100% (893/893), 15.23 MiB | 239.00 KiB/s, done.
370+
Resolving deltas: 100% (143/143), done.
371+
```
372+
373+
下一个 lab 发布时,我们的网页仓库会有更新,需要运行 `git pull`
374+
375+
```bash
376+
linux@user:~/ICS-25Fall-FDU.github.io# git pull
377+
Updating 67568b7..10fcbcf
378+
Fast-forward
379+
.gitignore | 3 +-
380+
docs/.vitepress/config/zh.ts | 3 +-
381+
docs/appendix/misc-qa.md | 45 +++++++++++++++++++++++++++++
382+
3 files changed, 49 insertions(+), 2 deletions(-)
383+
create mode 100644 docs/appendix/misc-qa.md
384+
```
385+
386+
如果你在本地仓库新增了 commit,你就可以 `git push`。当然,对于这个仓库你并没有 push 权限。你需要先 fork 该仓库,将本地修改 push 到你自己的仓库,然后向我们的仓库发起 pull request。
387+
:::
388+
389+
## 加入 GitHub Classroom
390+
391+
1. 点击[这个链接](https://classroom.github.com/a/_QjXIaPr)接受第一份作业,你就加入了我们的[课程组织](https://github.com/orgs/ICS-25Fall-FDU)。
392+
393+
> [!tip]
394+
>
395+
> 你需要登录 GitHub Classroom,请使用你的 GitHub 账户登录。
396+
>
397+
> 如果链接无法访问,请尝试使用 [Watt Toolkit](/appendix/watt_toolkit),如果仍然无法访问请联系助教。
398+
399+
2. 你将跳转到以下界面:
400+
![1](classroom1.png)
401+
402+
3. 选择你的名字。
403+
404+
> [!warning]
405+
>
406+
> 请勿选择其他同学的名字,如果发现自己的名字已被使用请及时联系 [houzexu22@m.fudan.edu.cn](mailto:houzexu22@m.fudan.edu.cn)。
407+
408+
4. 点击 `Accept this assignment`
409+
![2](classroom2.png)
410+
411+
5. 你将看到以下界面,这里的 URL (形如`https://github.com/ICS-25Fall-FDU/lab0-gitlab-<username>`)就是你个人本次作业的远程仓库。
412+
![3](classroom3.png)
413+
414+
6. 点进远程仓库 URL 获取 remote URL(`git@github.com` 开头),这将是你 `git clone` 的 URL。
415+
![4](classroom4.png)
416+
417+
## 实验任务
418+
419+
1. 认真阅读文档,学习 Git 的基本用法。
420+
2. 加入 GitHub Classroom。
421+
3. 克隆你的个人远程仓库,完成 `main.c` 文件中的 `TODO` 部分并进行一次 commit。(50分)
422+
> [!info]
423+
>
424+
> 只要填入任意字符串就算完成,当然你也可以随意发挥(程序的正确性不纳入计分,有修改即可)。
425+
>
426+
> 如果你想要编译运行 `main.c`,执行
427+
>
428+
> ```C
429+
> make
430+
> ./main
431+
> make clean
432+
> ```
433+
434+
4. 学习 Git 分支管理,新建 `feature` 分支,在该分支以及 `main` 分支上对 `main.c` 分别进行一次修改与提交(10分)。
435+
436+
随后将 `feature` 分支 merge 到 `main` 分支(即切换回 main 分支执行 `git merge feature`)。
437+
438+
> [!important]
439+
>
440+
> 在两个分支上的提交需要满足:在 `main` 分支合并时会出现冲突。请你解决这个冲突,并在实验报告里截图表明你遇到并解决了冲突。(20分)
441+
> > [!tip]
442+
> > 请阅读 `git merge` 部分,思考如何修改 `main.c` 会出现冲突。
443+
> >
444+
> > 如果你两次提交之后合并没有出现冲突,不必担心,你可以重复提交与合并而不用撤回之前的提交,直到出现冲突并解决。
445+
446+
5. 在 `main` 分支提交一份实验报告(10分),格式要求为 `PDF``Markdown`。内容包括
447+
- 你的实验步骤
448+
- 必要的截图
449+
- 你的建议(可选)
450+
451+
> [!tip]
452+
>
453+
> 这里的“提交”是指在文件夹内添加一个 PDF 或 Markdown 文件,然后 `git add -A && git commit`
454+
>
455+
> 如果你是 Word 爱好者,请你将它导出为 PDF。
456+
>
457+
> 你可以在自己电脑上任一位置用 Word 写实验报告并导出,然后把 PDF 文件拖拽复制到 VS Code 编辑器左侧的目录下。
458+
> ![VS Code](vscode.png)
459+
460+
## 提交
461+
462+
**提交方式**:在本地仓库完成上述所有提交后,在 main 分支执行 `git push`(当然你也可以每提交一次就 push)。
463+
464+
**截止时间**:10 月 8 日 23:59。逾期将扣除部分分数。
465+
466+
> [!info] 写在最后的话
467+
>
468+
> 作为第一次作业,这个文档的字数过多,但实际的任务很少。如果你对 Git 感兴趣可以认真读完,甚至在网上寻找其他学习资源。
469+
>
470+
> 如果你觉得内容过于冗长,只需对照实验任务针对性地学习重点。完成后续实验最简单的流程就是 `git clone` -> 写完所有代码 -> `git add -A && git commit -m "xxx" && git push`
471+
472+
## 学习资源
473+
474+
- [Pro Git](https://git-scm.com/book/en/v2) / [Pro Git中文版](https://git-scm.com/book/zh/v2),推荐阅读1-3章
475+
- [学习git的在线游戏](https://learngitbranching.js.org/),挺好玩的
476+
- [ohshitgit](https://ohshitgit.com/),简短的介绍了如何从 Git 错误中恢复
477+
- [Git for Computer Scientists](https://eagain.net/articles/git-for-computer-scientists/),简短的介绍了 Git 的数据模型
478+
- [git-from-the-bottom-up](https://jwiegley.github.io/git-from-the-bottom-up/),详细的介绍了 Git 的实现细节
479+
- [explain-git-in-simple-words](https://xosh.org/explain-git-in-simple-words/),如其名
480+
- [用动图展示10大Git命令](https://zhuanlan.zhihu.com/p/132573100),一篇精美文章

docs/lab/ssh-key1.png

35.8 KB
Loading

docs/lab/ssh-key2.png

123 KB
Loading

0 commit comments

Comments
 (0)