跳转至

01. 进阶教程

1. 本地构建

1.克隆本项目仓库

git clone https://github.com/yzbaaa/ZZU-CS-RESOURCES.git
cd ZZU-CS-RESOURCES

2.安装 python 依赖

pip install -r requirements.txt

国内可使用清华源安装依赖

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt

3.启动 mkdocs 本地服务

mkdocs serve

之后可以通过访问 localhost:8000 预览网站。

2. 贡献方式

2.1. PR(Pull Request)

  • 首先将项目仓库 fork
  • 然后将自己账号的对应仓库 clone 到本地修改,commit,push
  • 修改后,点击 New pull request,提交 PR
  • 最后等待作者审核,合并

也可以点击标题右侧的编辑按钮直接跳转到对应的文档,进行在线编辑,然后可以直接 PR。

附 git 快速入门教程:git、github 保姆级教程入门 以及对应笔记。

git 常用命令

基本操作
命令 作用
cd <directory> 切换目录
cd .. 切换到上一级文件夹
git config --global user.name "<name>" 配置全局用户名
git config --global user.email "<email>" 配置全局邮箱
git init 初始化 git 记录版本
git add <file> 添加文件到暂存区(不保存提交记录)
git add . 添加当前目录下所有文件到暂存区
git commit 把暂时保存的变更提交固定成一个版本
git commit -m "<comment>" 写提交说明的简化版操作
git reset 回退版本
git branch <branch_name> 建立本地分支
git branch -M <branch_name> 建立本地分支。- -M 选项:这是 --move 的简写,表示重命名分支。如果目标分支已经存在,-M 会强制覆盖
git branch --set-upstream-to=<remote_repo_alias/branch> <local_branch> 令指定的本地分支追踪指定的远程分支
git checkout <branch_name> 切换分支
git merge <another_branch_name> 把当前分支和另一个分支合并
查询操作
命令 作用
git log 显示当前分支的提交历史。按 q 键退出
git branch 列出所有本地分支,并标记当前所在的分支
git branch -vv 列出所有本地分支及其对应的远程分支
git remote -v 显示所有配置的远程仓库及其对应的 URL
git config --list 显示 git 配置信息。按 q 键退出
git config user.name 显示配置用户名
git config user.password 显示配置密码
git config user.email 显示配置邮箱
与仓库相关操作
命令 作用
git remote add origin <repository_url> 添加一个远程仓库地址
git push -u origin <branch_name> 将本地的 <branch_name> 分支推送到远程仓库,并设置跟踪关系
git clone <repository_url> 克隆地址对应的仓库到本地
git fetch <repo_name> 用于从远程仓库获取最新的更改,但不会修改你的工作目录或当前分支。它会更新本地仓库中的远程跟踪分支

git remote add origin <repository_url> 命令用于将远程仓库添加到本地 Git 仓库中。具体来说:

  • git remote add:这是添加远程仓库的命令。
  • origin:这是远程仓库的别名。通常,origin 是默认的远程仓库名称,但你可以使用任何你喜欢的名称。
  • <repository_url>:这是远程仓库的 URL 地址。它可以是 HTTPS 或 SSH 格式的 URL。

git push -u origin <branch_name> 命令用于将本地的 <branch_name> 分支推送到远程仓库,并设置跟踪关系。具体来说:

  • git push:将本地的更改推送到远程仓库。
  • -u 或 --set-upstream:设置本地分支与远程分支的跟踪关系。这样以后你只需要使用 git push 或 git pull,Git 就会知道要推送或拉取到哪个远程分支。
  • origin:这是远程仓库的默认名称。通常在克隆仓库时,默认的远程仓库名称是 origin
  • <branch_name>:这是你要推送的本地分支的名称。
常见问题
fatal: refusing to merge unrelated histories

当你在使用 Git 时遇到 fatal: refusing to merge unrelated histories 错误,通常是因为你尝试合并的两个分支没有共同的提交历史。解决方法:

  1. 使用 --allow-unrelated-histories 选项: 在执行 git pull 或 git merge 命令时,添加 --allow-unrelated-histories 选项。例如:

    git pull origin master --allow-unrelated-histories
    

    或者

    git merge master --allow-unrelated-histories
    
  2. 重新添加远程仓库: 如果上述方法不能解决问题,可以尝试删除并重新添加远程仓库:

    1
    2
    3
    git remote remove origin
    git remote add origin <远程仓库地址>
    git pull origin master --allow-unrelated-histories
    

要确保两个分支有共同的提交历史,可以从以下几个方面入手:

  1. 从同一个仓库克隆: 确保所有分支都是从同一个仓库克隆出来的,这样它们会共享相同的提交历史。例如:

    git clone <仓库地址>
    
  2. 创建新分支: 在同一个仓库中创建新分支,这样新分支会自动继承主分支的提交历史。例如:

    git checkout -b new-branch
    
  3. 合并分支: 在合并分支时,确保你在同一个仓库中操作。例如:

    git checkout master
    git merge new-branch
    
  4. 使用 rebase: 如果两个分支有不同的提交历史,可以使用 rebase 命令将一个分支的提交应用到另一个分支上。例如:

    git checkout feature-branch
    git rebase master
    
fatal: unable to access

完美解决 git 报错 "fatal: unable to access"

最常用的方法是取消代理:

git config --global --unset http.proxy 
git config --global --unset https.proxy