Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add 4.1.2.7 and fixed some typo in other chapters #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
site/
.vscode/
2 changes: 1 addition & 1 deletion docs/ch3/sec1/subsec1/1-git-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

### 2.1 仓库(Repository):代码的霍格沃茨

- **git 目录**:隐藏在项目中的魔法学院,记录所有时空轨迹
- **.git 目录**:隐藏在项目中的魔法学院,记录所有时空轨迹
- **工作区 → 暂存区 → 版本库**:三级防御体系,像精心设计的魔法结界
- **克隆魔法**:`git clone` 是复制时空门的咒语

Expand Down
4 changes: 2 additions & 2 deletions docs/ch3/sec1/subsec1/3-practice-code-hosting-platforms.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

注册之后登录首先看到的应该是你的 Dashboard 界面,就像下图这样,不过你的可能比较空白(😁)

![Dashboard](../../../assets/Dashborad.png)
![Dashboard](./assets/lec3/Dashboard.png)

图 1. Dashboard
{: .caption }
Expand All @@ -41,7 +41,7 @@

注册之后就可以做一些操作了,比如说收藏一个仓库,就是给这个仓库一颗⭐️,可以点击[这里](https://github.com/hust-open-atom-club/intro2oss)去送出你的第一颗⭐️

![repository](../../../assets/repo.png)
![repository](./assets/lec3/repo.png)

图 2. Repository
{: .caption }
Expand Down
2 changes: 1 addition & 1 deletion docs/ch3/sec1/subsec1/4-create-repo.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

- **创建远程仓库**:登录 GitHub 并点击“New repository”按钮。填写仓库名称、描述并选择公开或私有。点击“Create repository”完成创建。

![Create-repo](../../../assets/createrepo.png)
![Create-repo](./assets/lec4/createrepo.png)

- **克隆仓库到本地**:复制新仓库的克隆 URL。在终端中使用以下命令将仓库克隆到本地:

Expand Down
14 changes: 7 additions & 7 deletions docs/ch3/sec1/subsec1/5-open_source_License.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ GPL 协议就是实现这些自由的 “法律武器”,它像 “开源病
## 第三章:选择合适的许可证

### 选择流程图
![alt text](../../../assets/ChooseOSL.png)
![How to choose open source licence](./assets/lec5/ChooseOSL.png)

来源:https://www.ruanyifeng.com/blog/2011/05/how_to_choose_free_software_licenses.html
来源:[https://www.ruanyifeng.com/blog/2011/05/how_to_choose_free_software_licenses.html](https://www.ruanyifeng.com/blog/2011/05/how_to_choose_free_software_licenses.html)
### 额外补充
如果你还是没办法选出适合自己的开源许可证,你可以通过[开放源代码许可证选择器](https://open-source-license-chooser.toolsnav.top/)来帮助你选择。

Expand All @@ -59,23 +59,23 @@ GPL 协议就是实现这些自由的 “法律武器”,它像 “开源病
### 刚创建仓库(repository)时
若我们想在刚创建仓库时候便添加许可证,可观察到创建页面下方的 **Choose a license**,从这便可在创建仓库时候添加上许可证

![alt text](../../../assets/addLICENSE_creat.png)
![alt text](./assets/lec5/addLICENSE_creat.png)
### 对已有仓库添加许可证
转到 GitHub 上项目的主页。然后单击**添加文件**(Add file)并选择**创建新文件** (Creat new file) 选项。

![alt text](../../../assets/addLICENSE_first.png)
![alt text](./assets/lec5/addLICENSE_first.png)

接下来在文件名字段中填写 “LICENSE” 或 “LICENSE.md”,然后单击选择许可证模板。

![alt text](../../../assets/addLICENSE_second.png)
![alt text](./assets/lec5/addLICENSE_second.png)

接着查看可用的许可证并选择最适合您的项目的许可证,点击**审核并提交**,如下图:

![alt text](../../../assets/addLICENSE_third.png)
![alt text](./assets/lec5/addLICENSE_third.png)

最后,为您的许可证编写一条提交消息,并选择是否要将其直接提交到主分支或创建一个新分支并打开拉取请求。然后,按提交新文件按钮将许可证添加到您的项目中,如下图所示。

![alt text](../../../assets/addLICENSE_fourth.png)
![alt text](./assets/lec5/addLICENSE_fourth.png)

如果您创建了拉取请求以将许可证添加到您的项目,请合并该拉取请求,您应该会在 GitHub 上的项目主页上看到 LICENSE 文件。

Expand Down
File renamed without changes
184 changes: 184 additions & 0 deletions docs/ch3/sec1/subsec2/7-remote-repo-push-conflict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# 远程仓库操作与 Push 冲突

> 本子节旨在让学生掌握远程仓库的基础操作,能够处理 Push Conflict 等常见问题。

---

## 导学
在前面的学习中,我们已经学习了**远程仓库**和**本地仓库**的区别,了解了这种 “本地-远程” 协作模式带来的好处,并在 Github 上创建了一个远程仓库。

在本节课中,我们将学习操作 Git 远程仓库的基础操作,包括如何将本地仓库推送到远程仓库、如何从远程仓库拉取代码,以及如何解决推送冲突的问题。

---

## 获取一个与远程仓库的本地仓库

运用 Git 的 “本地-远程” 协作模式来修改托管在云端的项目分为三步:

```mermaid
graph TD
A[拉取远程内容到本地] --> B
B[在本地修改项目内容] --> C
C[推送本地内容到远程]
```

但以上操作都要在一个 git 仓库里进行,我们首先要在本地有一个与远程仓库对应的仓库。

如果本地本来没有仓库,我们可以把远程仓库直接克隆到本地,使用的命令是 `git clone <远程仓库的 URL>`。git 会自动为克隆的本地仓库设置与远程仓库的对应关系。

例如,假设你准备把远程仓库 `https://github.com/username/repository.git` 的内容克隆到 `/home/username/projects` 下面,我们可以执行以下命令:
```bash
cd /home/username/projects
git clone https://github.com/username/repository.git
# 输出示例
Cloning into 'repository'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 10 (delta 1), reused 10 (delta 1), pack-reused 0
Receiving objects: 100% (10/10), done.
```

这样,就会在 `/home/username/projects` 下建立一个文件夹 `repository`,并把仓库内容克隆到其中。

而如果本地本来就有一个与远程仓库对应的仓库,你也可以直接建立本地仓库与远程仓库的连接关系。假设我们本地仓库的路径是 `/home/username/projects/repository/`,我们可以执行以下命令:

```bash
cd /home/username/projects/repository
git remote add origin https://github.com/username/repository.git
```

---

## 拉取远程仓库的更改

你可能注意到,修改仓库内容的第一步是 “拉取远程内容到本地”,这是因为在修改仓库内容之前,如果本地仓库与远程仓库不同步,协作可能会出现问题。例如,假设你在参与小组合作实践,你的同学已经完成了项目的某个功能,而你因为没有同步本地代码而不知道,就有可能重复地再写一遍这个功能,浪费了时间与精力。所以,在协作开放中做修改前,要先将远程修改同步到本地。

我们可以用 `git pull` 命令同步远程仓库的修改到本地:
```bash
git pull
# 输出示例
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/username/repository
abc1234..def5678 main -> origin/main
Updating abc1234..def5678
Fast-forward
file1.txt | 2 +-
file2.txt | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
```

---

## 推送本地仓库到远程仓库

在本地仓库作出修改之后,我们就可以用 `git push` 操作把本地的修改同步到远程仓库。

首先,我们对仓库内容进行一些修改,例如添加一个文件 `a.txt`,并写入一行字 `hello world`,然后提交 commit:
```bash
echo "hello world" > a.txt
git add a.txt
git commit -m "add new file"
```

接下来,我们就用 `git push` 命令将本地仓库的修改同步到远程仓库。
```bash
git push
# 输出示例
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 292 bytes | 292.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To https://github.com/username/repository.git
abc1234..def5678 main -> main
```

---

## 处理推送冲突(Push Conflict)

如果你在远程仓库修改了文件,但在开始修改本地仓库文件之前忘记拉取远程更改了,你可能会 push 失败:
```bash
git push
To https://github.com/username/repository.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/username/repository.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
```

### 为什么会发生冲突?

直接翻译以上的输出:
```bash
错误:未能将部分引用推送到 'https://github.com/username/repository.git'
提示:更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另
提示:一个仓库已向该引用进行了推送。您需要先整合远程变更(如 'git pull ...')
提示:详见 'git push --help' 中的 'Note about fast-forwards' 章节。
```

当远程仓库存在本地没有的新提交时(可能是其他协作者推送的代码),Git 会拒绝你的 push,以防协作者们的代码互相覆盖。

### 如何解决冲突?

```mermaid
graph TD
A[git push 失败] --> B[git pull 获取最新代码]
B --> C{存在冲突吗?}
C -->|是| D[解决文件冲突]
C -->|否| E[直接推送]
D --> F[重新提交并推送]
```

首先,用 `git pull` **拉取最新代码**:
```bash
git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 884 bytes | 126.00 KiB/s, done.
From https://github.com/username/repository.git
1234abc..5678def main -> origin/main
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
```

在 pull 后,git 会自动合并远程仓库的更改到本地仓库的更改历史上。如果本地与远程修改的文件是不重叠的,就可以直接 `git push`。

但如果本地与远程修改了同一个文件,就可能会发生合并冲突,比如最后三行显示了:
```bash
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.
```

我们可以根据该提示打开冲突文件,git 会在文件中用 `<<<<<<< HEAD` 和 `>>>>>>>` 标识出本地与远程修改的重叠部分,例如:
```
<<<<<<< HEAD
本地更改
=======
远程更改
>>>>>>> 6daf9f4bc600f0ed655f70f4e92fc0a3f488e0da
```

我们需要手动选择要保留的代码段,然后删除冲突标记,即可**解决冲突**:
```
本地更改(选择了一份修改,并删除了冲突标记)
```

接下来,**重新提交并推送**即可。
```bash
git add a.txt
git commit -m "解决合并冲突"
git push
```
13 changes: 7 additions & 6 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,22 @@ nav:
- 第三节 开源世界规则: ch2/sec3/rules.md
- 三、开源贡献基础技能:
- 导览: ch3/index.md
- 第一节 Git训练营:
- 第一节 Git 训练营:
- 导学阶段:
- Git简介: ch3/sec1/subsec1/1-git-introduction.md
- Git 简介: ch3/sec1/subsec1/1-git-introduction.md
- 代码托管平台简介: ch3/sec1/subsec1/2-code-hosting-platforms.md
- 注册并熟悉代码托管平台: ch3/sec1/subsec1/3-practice-code-hosting-platforms.md
- 创建并管理仓库: ch3/sec1/subsec1/4-create-repo.md
- 开源许可证选择指南: ch3/sec1/subsec1/5-open_source_License.md
- 基础阶段:
- Git的基础配置: ch3/sec1/subsec2/1-basic-configuration.md
- Git 的基础配置: ch3/sec1/subsec2/1-basic-configuration.md
- 暂存区操作: ch3/sec1/subsec2/2-staging.md
- 如何写好一个commit message: ch3/sec1/subsec2/6-commit-message.md
- 如何写好一个 Commit Message: ch3/sec1/subsec2/6-commit-message.md
- 远程仓库的基础操作 与 Push 冲突: ch3/sec1/subsec2/7-remote-repo-push-conflict.md
- 专业阶段:
- Git Rebase与Git Merge的使用与区别: ch3/sec1/subsec3/1-rebase-merge.md
- Git Rebase 与 Git Merge 的使用与区别: ch3/sec1/subsec3/1-rebase-merge.md
- 项目阶段:
- 贡献一个Linux patch: ch3/sec1/subsec4/1-linux-patch.md
- 贡献一个 Linux patch: ch3/sec1/subsec4/1-linux-patch.md
- 四、领导开源社区:
- 第四章 导览: ch4/index.md
- 附录:
Expand Down
Loading