docs(git): add advanced and error

This commit is contained in:
zjkjzj
2021-03-18 20:08:24 +08:00
parent 967b4b575d
commit 481904f1dc
39 changed files with 1820 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
# error: RPC failed; curl 56 GnuTLS recv error (-9)
## 问题描述
下载`Github`仓库时出现如下错误
```
remote: Enumerating objects: 150, done.
remote: Counting objects: 100% (150/150), done.
remote: Compressing objects: 100% (90/90), done.
error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length was received.
fatal: The remote end hung up unexpectedly
fatal: 过早的文件结束符EOF
fatal: index-pack 失败
```
## 解析
参考[git error: RPC failed; curl 56 GnuTLS](https://stackoverflow.com/questions/38378914/git-error-rpc-failed-curl-56-gnutls)和[Git 克隆错误RPC failed; curl 56 Recv failure.... 及克隆速度慢问题解决](https://blog.csdn.net/qq_34121797/article/details/79561110),有可能是`http缓存不够`或者`网络不稳定`问题
* 对于网络不稳定问题
>Rebuilding git with openssl instead of gnutls fixed my problem.
* 对于`http`缓存不够
```
# httpBuffer加大
$ git config --global http.postBuffer 524288000
```
## 解决
重新设置了`http`缓存,不过最后是通过码云的方式解决的
1. 先将`Github`仓库拉取到码云仓库
2. 在从码云上下载

View File

@@ -0,0 +1,36 @@
# 为每个项目单独设置用户名和邮箱
## 问题描述
提交`git`记录前需要先设置用户名和邮箱,最常用的方式就是全局设置
```
$ git config --global user.name xxx
$ git config --global user.email xxx
# 查看
$ git config -l
```
最近想要在服务器上进行版本管理,不方便全局设置,可以在项目路径下进行本地设置
## 方式一
进入项目路径,执行如下命令
```
$ git config --local user.name xxx
$ git config --local user.email xxx
# 查询
$ git config --local -l
```
## 方式二
直接在`.git/config`配置文件中设置
```
[user]
name = xxx
email = xxx
```

View File

@@ -0,0 +1,60 @@
# 创建裸仓库
裸仓库,也就是不包含当前工作目录的仓库,即`.git`文件夹,可作为服务器`git`仓库
有两种方式,一是使用本地已存在的仓库,二是新建远程裸仓库
## 本地导出
从已有仓库中导出`.git`文件夹,有两种方式
1. 使用`git`命令
git clone --bare my_project my_project.git
$ git clone --bare TEST TEST.git
Cloning into bare repository 'TEST.git'...
done.
2. 使用`cp`命令
cp -rf my_project/.git my_project.git
### 放置在远程服务器
在服务器上新建文件夹`git`
$ pwd
/home/ubuntu/git
复制本地裸仓库到服务器
$ scp -r TEST.git ubuntu@132.232.142.219:/home/ubuntu/git/TEST.git
这样在`git`文件夹下就有了裸仓库`TEST.git`
$ pwd
/home/ubuntu/git/TEST.git
## 远程新建
在远程服务器新建文件夹,并在其中初始化为裸仓库
$ mkdir TE.git
$ cd TE.git
$ git init --bare
Initialized empty Git repository in /home/ubuntu/git/TE.git/
# 如果想要同一组内的其他用户也可访问该仓库,添加参数--shared来修改仓库权限
$ git init --bare --shared
Initialized empty shared Git repository in /home/ubuntu/git/TE.git/
## 克隆裸仓库
`github`操作方式类似
git clone ubuntu@132.232.142.219:/home/ubuntu/git/TEST.git
## 相关阅读
* [4.2 服务器上的 Git - 在服务器上搭建 Git](https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E5%9C%A8%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E6%90%AD%E5%BB%BA-Git)

View File

@@ -0,0 +1,32 @@
# 工作目录和裸仓库分离
裸仓库指`.git`文件夹,工作目录指除`.git`文件夹外的其他文件
使用场景之一如下:
1. 本地将修改好的代码上传到远程裸仓库
2. 裸仓库获取到代码后,通过钩子生成工作目录,放置在其他路径下
3. 服务器再对工作目录进行操作,比如`nginx`对静态文件进行托管
`.git/hooks`文件夹下新建`post-receive`文件
$ vim post-receive
添加如下内容
#!/bin/sh
git --work-tree=工作目录 --git-dir=裸仓库 checkout -f
授予执行权限
$ sudo chmod +x post-receive
## 相关阅读
* [GIT_DIR和GIT_WORK_TREE的妙用工作区和仓储可隔离](http://www.yinqisen.cn/blog-718.html)
* [通过git自动部署WEB服务上的PHP代码提交即生效](http://www.yinqisen.cn/blog-675.html)
* [8.3 自定义 Git - Git 钩子](https://git-scm.com/book/zh/v2/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-Git-%E9%92%A9%E5%AD%90)

View File

@@ -0,0 +1,37 @@
# 拉取指定远程分支到本地
由于`git`去中心化的特性,下载远程仓库时会将所有分支都下载下来,不仅耗时而且占用本地磁盘容量。拉取指定远程分支到本地
## 思路
* 首先在本地新建`git`仓库;
* 然后下载指定远程分支;
* 最后创建本地分支并关联到指定远程分支即可。
## 实现
新建仓库
```
$ mkdir gitrepo
$ cd giterpo
$ git init
```
拉取远程指定分支
```
$ git remote add origin https://github.com/zjZSTU/zjzstu.github.com.git
$ git fetch origin dev
```
新建本地分支并关联到指定远程分支
```
$ git checkout -b dev origin/dev
```
## 相关阅读
* [git 拉取远程分支到本地](https://blog.csdn.net/carfge/article/details/79691360)

View File

@@ -0,0 +1,133 @@
# 推送到多个远程托管服务器
将代码推送到多个远程托管服务器,多重备份保证安全性,比如推送到`github``coding`
有两种情况
1. 同时推送代码到多个仓库
2. 指定推送的地址,比如本次操作仅推送到`github`,不推送到`coding`
首先查看当前已追踪的远程仓库地址
$ git remote -v
origin https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin https://git.dev.tencent.com/zjZSTU/TEST.git (push)
当前已追踪了`coding`服务器上的仓库,需要加入`github`仓库
*针对多个远程仓库的推送,使用`SSH`传输协议可以提高操作便捷性*
## 同时推送
### 命令行操作
添加`github`仓库地址
$ git remote set-url --add --push origin https://github.com/zjZSTU/TEST.git
$ git remote -v
origin https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin https://github.com/zjZSTU/TEST.git (push)
再重新添加`coding`的仓库地址
$ git remote set-url --add --push origin https://git.dev.tencent.com/zjZSTU/TEST.git
$ git remote -v
origin https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin https://github.com/zjZSTU/TEST.git (push)
origin https://git.dev.tencent.com/zjZSTU/TEST.git (push)
这样就可以同时推送到多个远程仓库
$ git push -u origin master
Username for 'https://github.com': zjZSTU
Password for 'https://zjZSTU@github.com':
...
...
To https://github.com/zjZSTU/TEST.git
d19a01e..4b76b5d master -> master
Branch master set up to track remote branch master from origin.
Username for 'https://git.dev.tencent.com': zjZSTU
Password for 'https://zjZSTU@git.dev.tencent.com':
...
...
To https://git.dev.tencent.com/zjZSTU/TEST.git
d19a01e..4b76b5d master -> master
Branch master set up to track remote branch master from origin.
### 文件操作
修改`.git`文件夹下的`CONFIG`文件
# 原先
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://git.dev.tencent.com/zjZSTU/TEST.git
# 修改为
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://git.dev.tencent.com/zjZSTU/TEST.git
pushurl = https://github.com/zjZSTU/TEST.git
pushurl = https://git.dev.tencent.com/zjZSTU/TEST.git
`origin`小节中添加属性`pushurl`,设置多个远程仓库地址
## 指定推送
指定推送操作和同时推送操作能够同时并存在同一个仓库中
默认情况下使用`origin`作为远程仓库名,可以添加另外的名字,在旗下指定新的远程仓库
当前操作如下:默认`origin`指向`coding`,添加`github`指向`github`仓库,添加`all`指向`github``coding`
### 命令行操作
添加`github`远程
$ git remote add github https://github.com/zjZSTU/TEST.git
$ git remote -v
github https://github.com/zjZSTU/TEST.git (fetch)
github https://github.com/zjZSTU/TEST.git (push)
origin https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin https://git.dev.tencent.com/zjZSTU/TEST.git (push)
添加`all`远程
$ git remote add all https://github.com/zjZSTU/TEST.git
$ git remote set-url --add --push all https://git.dev.tencent.com/zjZSTU/TEST.git
$ git remote set-url --add --push all https://github.com/zjZSTU/TEST.git
$ git remote -v
all https://github.com/zjZSTU/TEST.git (fetch)
all https://git.dev.tencent.com/zjZSTU/TEST.git (push)
all https://github.com/zjZSTU/TEST.git (push)
github https://github.com/zjZSTU/TEST.git (fetch)
github https://github.com/zjZSTU/TEST.git (push)
origin https://git.dev.tencent.com/zjZSTU/TEST.git (fetch)
origin https://git.dev.tencent.com/zjZSTU/TEST.git (push)
上传代码
# 上传到coding
git push -u origin master
# 上传到github
git push -u github master
# 同时上传
git push -u all master
### 文件操作
修改`.git`文件夹下的`CONFIG`文件,添加`github``all`小节
[remote "github"]
url = https://github.com/zjZSTU/TEST.git
fetch = +refs/heads/*:refs/remotes/github/*
[remote "all"]
url = https://github.com/zjZSTU/TEST.git
fetch = +refs/heads/*:refs/remotes/all/*
pushurl = https://git.dev.tencent.com/zjZSTU/TEST.git
pushurl = https://github.com/zjZSTU/TEST.git
## 相关阅读
* [Git - Pushing code to two remotes [duplicate]](https://stackoverflow.com/questions/14290113/git-pushing-code-to-two-remotes)

View File

@@ -0,0 +1,117 @@
# 本地分支操作
主要学习内容:
1. 分支显示、创建和切换
2. 分支暂存
3. 分支删除
4. 分支合并
使用命令
1. [git branch](https://git-scm.com/docs/git-branch)
2. [git checkout](https://git-scm.com/docs/git-checkout)
3. [git stash](https://git-scm.com/docs/git-stash)
4. [git merge](https://git-scm.com/docs/git-merge)
## 分支显示、创建和切换
显示所有分支以及当前所处分支
```
$ git branch
```
创建新分支testing
```
$ git branch testing
```
切换到新分支testing
```
$ git checkout testing
```
创建新分支`aa`并自动切换
```
$ git checkout -b aa
```
## 分支暂存
在切换分支之前最好先保存当前在暂存区域的内容,使用`git stash`命令将其保存在一个*脏*目录中
暂存当前工作状态
```
$ git stash
```
可进行多次暂存操作,查看暂存列表
```
$ git stash list
```
重新释放最新的暂存内容
```
$ git stash apply
```
指定暂存节点进行释放
```
$ git stash apply 节点名
```
清除所有的暂存节点
```
$ git stash clear
```
## 分支删除
删除指定分支
```
$ git branch -d 分支名
```
## 分支合并
合并指定分支到当前分支
```
$ git merge 分支名
```
在分支合并出现冲突时可以使用命令回复到之前为合并状态
```
$ git merge --abort
```
不合并其他分支的提交历史
```
# 仅添加文件,不进行提交
$ git merge --squash
# 添加文件并进行提交
$ git merge --no-squash
```
使用参数`--merged``--no-merged`过滤已合并和未合并到当前分支的分支
## 相关阅读
* [3.1 Git 分支 - 分支简介](https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%AE%80%E4%BB%8B)
* [3.2 Git 分支 - 分支的新建与合并](https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6)
* [7.3 Git 工具 - 储藏与清理](https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%82%A8%E8%97%8F%E4%B8%8E%E6%B8%85%E7%90%86#r_git_stashing)

View File

@@ -0,0 +1,8 @@
# 绘制分支图
## 相关阅读
* [Pretty git branch graphs](https://stackoverflow.com/questions/1057564/pretty-git-branch-graphs)
* [Visualizing branch topology in git](https://stackoverflow.com/questions/1838873/visualizing-branch-topology-in-git)

View File

@@ -0,0 +1,208 @@
# 自建服务器
在云服务器上自建`git`服务器,最简单的方式就是创建裸仓库然后用`ssh`的方式进行代码的拉取和推送,参考[创建裸仓库](./创建裸仓库.md)
下面实现一个规范的`git`服务器,类似第三方服务器(`github、coding、gitlab`等),其支持`ssh``http`协议的仓库操作,但不允许进行远程登录
第三方仓库名如下:
* `https://<domain-name>/<user-name>/<repo-name>.git`
* `git@<domain-name>:<user-name>/<repo-name>.git`
操作步骤如下:
1. 创建用户`git`
2. `ssh`设置
3. 下载仓库
## 创建用户`git`
$ sudo adduser git
正在添加用户"git"...
正在添加新组"git" (1000)...
正在添加新用户"git" (1000) 到组"git"...
创建主目录"/home/git"...
正在从"/etc/skel"复制文件...
输入新的 UNIX 密码:
重新输入新的 UNIX 密码:
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
这些信息是否正确? [Y/n]
`/data`目录下创建目录`repositories`,然后在里面创建一个裸仓库
# 创建目录
$ sudo mkdir -p /data/repositories
# 设置用户和组
$ sudo chown -R git:git /data/repositories
# 设置可访问权限
$ sudo chmod 755 /data/repositories/
# 创建裸仓库
$ git init --bare hello.git
Initialized empty Git repository in /data/repositories/hello.git/
## 设置ssh连接
将客户端的`ssh`公钥添加到用户`git``.ssh/authorized_keys`文件中,即可实现`ssh`连接
$ mkdir .ssh && chmod 700 .ssh
$ touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
### 设置git-shell连接
设置用户`git`仅能操作`git`相关内容,可以将用户`git`的登录`shell`设置为`git-shell`
#查看用户git登录shell
$ cat /etc/passwd | grep git
git:x:1000:1000:,,,:/home/git:/bin/bash
修改为`git-shell`
# 查找
$ which git-shell
/usr/bin/git-shell
# 修改
$ sudo vim /etc/passwd
# 修改后
$ cat /etc/passwd | grep git
git:x:1000:1000:,,,:/home/git:/usr/bin/git-shell
## 下载仓库
下载仓库`hello.git`到本地
$ git clone git@132.232.142.219:/data/repositories/hello.git
Cloning into 'hello'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
$ cd hello/
$ ls -al
total 12
drwxrwxr-x 3 zj zj 4096 3月 9 16:29 .
drwxr-xr-x 4 zj zj 4096 3月 9 16:29 ..
drwxrwxr-x 7 zj zj 4096 3月 9 16:29 .git
添加文件`hello.txt`,加入版本管理并上传
$ touch hello.txt
$ ls
hello.txt
$ git add hello.txt
$ git commit -m "hello git"
[master (root-commit) 24265ed] hello git
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello.txt
$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 207 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@132.232.142.219:/data/repositories/hello.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
### 配置config
使用`config`文件可以简易仓库下载地址,在本地`~/.ssh`文件夹下新建配置文件`config`,添加如下
$ cat config
host git-server
user server
hostname 132.232.142.219
port 22
identityfile ~/.ssh/tencent_id_rsa
# 设置访问权限
$ sudo chmod 600 config
**注意:用`tab`键保持文档格式**
`config`文件指定了主机名`git-server`配置的用户名、`ip`地址、端口号和私钥地址
添加配置文件后下载仓库地址如下
$ git clone git-server:/data/repositories/hello.git
Cloning into 'hello'...
remote: Counting objects: 3, done.
Receiving objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Checking connectivity... done.
## ERROR
* 问题一:`publickey denied`
`ssh`连接失败,这个问题很迷,耗了`1`天时间,网上找了很多资料没有结果,有一个博客主也遇到了同样的问题,他最后解决是因为突然就可以了,我也一样
* 问题二:`git is not in the sudoers file. This incident will be reported.`
是因为`git`没有添加到`sudo`权限,参考[xxx is not in the sudoers file.This incident will be reported.的解决方法](https://www.cnblogs.com/xiaochaoyxc/p/6206481.html)
1. 先切换到`ubuntu`用户
$ su ubuntu
2. 赋予`sudo`文件写权限
$ sudo chmod u+w /etc/sudoers
3. 修改文件`/etc/sudoers`
$ sudo vim /etc/sudoers
# 添加
youuser ALL=(ALL) ALL
表示允许用户`youuser`执行`sudo`命令(需要输入密码)
4. 撤销`sudo`文件写权限
$ sudo chmod u-w /etc/sudoers
5. 重新切换回原来用户
$ su git
* 问题三:`perl: warning: Falling back to a fallback locale ("en_US.utf8").`
```
$ sudo adduser zhujian
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_MEASUREMENT = "zh_CN.UTF-8",
LC_PAPER = "zh_CN.UTF-8",
LC_MONETARY = "zh_CN.UTF-8",
LC_NAME = "zh_CN.UTF-8",
LC_ADDRESS = "zh_CN.UTF-8",
LC_NUMERIC = "zh_CN.UTF-8",
LC_TELEPHONE = "zh_CN.UTF-8",
LC_IDENTIFICATION = "zh_CN.UTF-8",
LC_TIME = "zh_CN.UTF-8",
LANG = "en_US.utf8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.utf8").
Adding user `zhujian'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+VVpw5kOZBmzJYz/vngYpAAV61Fq9oChSflQFkfzr1sKHRqq2/sqeZD3gzPQZbrKWcHbuGCWyQOvm1gH+67gW+TpUO9DWeeHqo3h5rlCW+ElJcL/q4b+ZBVEmGDjzE+Sg+6wM+izBl5xzHDFeLhN3Yw1OVc2rwQFQ/CD6FSKdL4b5bt0/5rpu65sv7haXjfDMSEsIVgPY5behLzZzoXy81iN4/tPF3cjDsn/x5Yywc60LdslJ5hW5wlozhq1LibUXk9JQu/+5DDZKi8ytMEoe1S7yROvaC/ofJQR22hINnFoLNBC8gSFM2YR+t9oBF0eiAaVwfgddA0+ScYrWA5Yr zj@zj-ThinkPad-T470p ...
Adding new group `zhujian' (1000) ...
Adding new user `zhujian' (1000) with group `zhujian' ...
Creating home directory `/home/zhujian' ...
...
...
```
提示语言包问题,参考[perl: warning: Falling back to a fallback locale ("en_US.UTF-8")](https://blog.csdn.net/jmpjmpkiss/article/details/55098794)
$ sudo apt install locales-all
## 相关阅读
* [搭建 Git 服务器](https://cloud.tencent.com/developer/labs/lab/10045)
* [4.4 服务器上的 Git - 配置服务器](https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E9%85%8D%E7%BD%AE%E6%9C%8D%E5%8A%A1%E5%99%A8)
* [干货 | 简单几步搭建一个远程git服务器](https://www.onelib.biz/blog/a/581047797628141c0fbfb0ed)

View File

@@ -0,0 +1,103 @@
# 远程分支操作
学习本地分支与远程分支的交互
1. 分支显示
2. 分支同步
3. 分支推送
4. 分支跟踪
5. 分支删除
使用命令
1. [git branch](https://git-scm.com/docs/git-branch)
2. [git fetch](https://git-scm.com/docs/git-fetch)
3. [git push](https://git-scm.com/docs/git-push)
4. [git checkout](https://git-scm.com/docs/git-checkout)
5. [git merge](https://git-scm.com/docs/git-merge)
## 分支显示
显示本地分支和远程分支的关系
```
# 使用参数-vv列出本地分支是否有跟踪远程分支并且本地分支是否领先或者落后远程分支
$ git branch -vv
```
## 分支同步
获取远程分支数据
```
$ git fetch 远程仓库 # origin是默认的远程仓库
```
使用`git fetch`不会合并远程分支,需要再显式使用`git merge`命令
使用`git pull`可实现拉取远程分支数据并合并
```
$ git pull 远程仓库 本地分支名
```
## 分支推送
推送本地分支内容到远程分支
```
$ git push 远程仓库 本地分支名/远程分支名
```
如果需要本地分支和远程分支名一致,实现如下:
```
$ git push 远程仓库 本地分支名
```
使用参数`-u`设置要推送的远程分支为本地待跟踪,方便后续拉取代码操作
```
$ git push -u 远程仓库 本地分支
```
## 分支跟踪
设置本地分支想要跟踪的远程分支
```
$ git checkout -b [branch] [remotename]/[branch]
```
如果远程分支和本地分支一致,那么使用简易方式
```
$ git checkout --track [remotename]/[branch]
```
修改要跟踪的远程分支,使用参数`-u``--set-upstream-to`
```
$ git branch -u [branch] [remotename]/[branch]
# 同样的分支名
$ git branch -u [remotename]/[branch]
```
## 分支删除
删除远程分支
```
$ git push 远程仓库 --delete 远程分支名
```
如果远程分支已在本地分支之后,可以不删除远程分支而使用强制方式推送到远程分支,参考[[Git高级教程(二)] 远程仓库版本回退方法](https://blog.csdn.net/fuchaosz/article/details/52170105)
```
$ git push -f
```
## 相关阅读
* [3.5 Git 分支 - 远程分支](https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF)

View File

@@ -0,0 +1,43 @@
# 远程分支替换本地分支
## 问题描述
本地分支有`master`,想要使用远程仓库的`master`分支,如何替换?
## 问题解决
有两种解决方案
### 方案一
拉取远程分支后覆盖本地分支。首先拉取远程分支到本地
```
# 删除已有远程仓库
$ git remote remove origin
# 设置远程仓库链接
$ git remote add origin git@xxx.com:xx.git
$ 拉取远程分支
$ git fetch origin master
```
然后使用远程分支重置本地分支
```
$ git reset --hard origin/master
```
### 方案二
```
删除您的本地分支: git branch -d master
获取最新的远程分支: git fetch origin master
重建基于远程的本地分支: git checkout -b master origin/master
```
## 相关阅读
* [拉取指定远程分支到本地](./拉取指定远程分支到本地.md)
* [如何完全替换本地分支与远程分支?](https://cloud.tencent.com/developer/ask/26508)

136
docs/git/error.md Normal file
View File

@@ -0,0 +1,136 @@
# ERROR
## 问题一publickey denied
`ssh`连接失败,这个问题很迷,耗了`1`天时间,网上找了很多资料没有结果,有一个博客主也遇到了同样的问题,他最后解决是因为突然就可以了,我也一样
## 问题二git is not in the sudoers file. This incident will be reported.
是因为`git`没有添加到`sudo`权限,参考[xxx is not in the sudoers file.This incident will be reported.的解决方法](https://www.cnblogs.com/xiaochaoyxc/p/6206481.html)
1. 先切换到`ubuntu`用户
$ su ubuntu
2. 赋予`sudo`文件写权限
$ sudo chmod u+w /etc/sudoers
3. 修改文件`/etc/sudoers`
$ sudo vim /etc/sudoers
# 添加
youuser ALL=(ALL) ALL
表示允许用户`youuser`执行`sudo`命令(需要输入密码)
4. 撤销`sudo`文件写权限
$ sudo chmod u-w /etc/sudoers
5. 重新切换回原来用户
$ su git
## 问题三perl: warning: Falling back to a fallback locale ("en_US.utf8").
```
$ sudo adduser zhujian
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LC_MEASUREMENT = "zh_CN.UTF-8",
LC_PAPER = "zh_CN.UTF-8",
LC_MONETARY = "zh_CN.UTF-8",
LC_NAME = "zh_CN.UTF-8",
LC_ADDRESS = "zh_CN.UTF-8",
LC_NUMERIC = "zh_CN.UTF-8",
LC_TELEPHONE = "zh_CN.UTF-8",
LC_IDENTIFICATION = "zh_CN.UTF-8",
LC_TIME = "zh_CN.UTF-8",
LANG = "en_US.utf8"
are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.utf8").
Adding user `zhujian'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+VVpw5kOZBmzJYz/vngYpAAV61Fq9oChSflQFkfzr1sKHRqq2/sqeZD3gzPQZbrKWcHbuGCWyQOvm1gH+67gW+TpUO9DWeeHqo3h5rlCW+ElJcL/q4b+ZBVEmGDjzE+Sg+6wM+izBl5xzHDFeLhN3Yw1OVc2rwQFQ/CD6FSKdL4b5bt0/5rpu65sv7haXjfDMSEsIVgPY5behLzZzoXy81iN4/tPF3cjDsn/x5Yywc60LdslJ5hW5wlozhq1LibUXk9JQu/+5DDZKi8ytMEoe1S7yROvaC/ofJQR22hINnFoLNBC8gSFM2YR+t9oBF0eiAaVwfgddA0+ScYrWA5Yr zj@zj-ThinkPad-T470p ...
Adding new group `zhujian' (1000) ...
Adding new user `zhujian' (1000) with group `zhujian' ...
Creating home directory `/home/zhujian' ...
...
...
```
提示语言包问题,参考[perl: warning: Falling back to a fallback locale ("en_US.UTF-8")](https://blog.csdn.net/jmpjmpkiss/article/details/55098794)
$ sudo apt install locales-all
## 问题四:`remote: error: cannot lock ref 'refs/heads/master': ref refs/heads/master`
`Travis CI`上上传文件到远程仓库,出现如下问题
remote: error: cannot lock ref 'refs/heads/master': ref refs/heads/master
### 起因
我在上传提交时中间中断了
$ git push -u origin dev
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 556 bytes | 0 bytes/s, done.
Total 6 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), completed with 5 local objects.
^C
然后再更新文章后再一次提交,就出现了远程仓库锁定的问题
### 解决
参考:[git: error: cannot lock ref, error: cannot lock ref](https://blog.csdn.net/sinat_36246371/article/details/79959598)
将远程仓库下载到本地,然后执行如下命令
git remote prune origin
其作用是同步本地远程分支,删除本地已过时的远程分支
重新触发`CI`工具执行,此时能够成功更新了
## 问题五error: RPC failed; curl 56 GnuTLS recv error (-9)
### 问题描述
下载`Github`仓库时出现如下错误
```
remote: Enumerating objects: 150, done.
remote: Counting objects: 100% (150/150), done.
remote: Compressing objects: 100% (90/90), done.
error: RPC failed; curl 56 GnuTLS recv error (-9): A TLS packet with unexpected length was received.
fatal: The remote end hung up unexpectedly
fatal: 过早的文件结束符EOF
fatal: index-pack 失败
```
### 解析
参考[git error: RPC failed; curl 56 GnuTLS](https://stackoverflow.com/questions/38378914/git-error-rpc-failed-curl-56-gnutls)和[Git 克隆错误RPC failed; curl 56 Recv failure.... 及克隆速度慢问题解决](https://blog.csdn.net/qq_34121797/article/details/79561110),有可能是`http缓存不够`或者`网络不稳定`问题
* 对于网络不稳定问题
>Rebuilding git with openssl instead of gnutls fixed my problem.
* 对于`http`缓存不够
```
# httpBuffer加大
$ git config --global http.postBuffer 524288000
```
### 解决
重新设置了`http`缓存,不过最后是通过码云的方式解决的
1. 先将`Github`仓库拉取到码云仓库
2. 在从码云上下载

View File

@@ -0,0 +1,91 @@
# GitLab导致8080端口冲突
## 问题复现
安装完`GitLab`后,修改配置文件`/etc/gitlab/gitlab.rb`
```
##external_url 'http://gitlab.example.com'
external_url 'http://localhost:8800'
```
本以为这样就能修改`GitLab`端口号为`8800`了,没想到再次登录`8080`仍旧出现了`GitLab`页面
![](./imgs/8080.png)
## 问题解析
查询哪个程序监听了`8080`端口
```
# netstat -lnp | grep 8080
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 26436/config.ru
```
查询相应的进程
```
# netstat -lnp | grep 26436
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 26436/config.ru
unix 2 [ ACC ] STREAM LISTENING 343538 26436/config.ru /var/opt/gitlab/gitlab-rails/sockets/gitlab.socket
```
仍然是`GitLab`在监听`8080`端口,参考[gitlab 8.13 80 8080端口冲突问题](https://blog.csdn.net/vbaspdelphi/article/details/52979836),查看配置文件`unicorn.rb`
```
# This file is managed by gitlab-ctl. Manual changes will be
# erased! To change the contents below, edit /etc/gitlab/gitlab.rb
# and run `sudo gitlab-ctl reconfigure`.
# What ports/sockets to listen on, and what options for them.
listen "127.0.0.1:8080", :tcp_nopush => true
```
默认情况下`unicorn`同样监听`8080`端口,查询`/etc/gitlab/gitlab.rb`中相应的设置
```
# cat gitlab.rb | grep unicorn
#unicorn['port'] = 8800
```
## 解决方案
需要在`gitlab.rb`上同时修改`unicorn`监听端口号,修改配置文件`/etc/gitlab/gitlab.rb`如下
```
##external_url 'http://gitlab.example.com'
external_url 'http://localhost:8800'
unicorn['port'] = 8801
```
重新启动`GitLab`
```
# gitlab-ctl reconfigure
# gitlab-ctl restart
```
查询配置文件`/var/opt/gitlab/gitlab-rails/etc/unicorn.rb`
```
# cat unicorn.rb | grep listen
# What ports/sockets to listen on, and what options for them.
listen "127.0.0.1:8801", :tcp_nopush => true
```
测试端口号
```
$ curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
$ curl localhost:8800
<!DOCTYPE html>
<html>
<head>
<meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
...
...
# curl localhost:8801
<html><body>You are being <a href="http://localhost:8801/users/sign_in">redirected</a>.</body></html>
```

View File

@@ -0,0 +1,24 @@
# Prometheus吃磁盘空间
在云服务器上部署`Gitlab`,今天发现无法登录了。登上云服务器后发现磁盘容量没有了,通过查找发现`Prometheus`目录下占据了`30GB`的空间(总共`50GB`
参考:
[Monitoring GitLab with Prometheus](https://docs.gitlab.com/ee/administration/monitoring/prometheus/)
[Prometheus eats disk space in /var/opt/gitlab/prometheus/data](https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/4166)
[GitLab 官方镜像内部集成 Prometheus 历史数据过大的问题处理](https://lakelight.net/2020/03/24/GitLab-Prometheus-clean.html)
`Prometheus`是一个监控服务,会保存历史监控数据,下面尝试关闭该服务并删除之前的数据(在`Docker Gitlab`上操作)
1. 修改配置文件`/srv/gitlab/config/gitlab.rb`,添加
```
prometheus_monitoring['enable'] = false
```
2. 关闭`gitlab`并删除`/srv/gitlab/data/prometheus`文件夹数据
3. 重启`gitlab`,问题解决

View File

@@ -0,0 +1,112 @@
# [Docker]GitLab使用
参考:
[GitLab Docker images](https://docs.gitlab.com/omnibus/docker/)
[gitlab docker Web界面打开反应迟钝的解决办法](https://blog.csdn.net/happyfreeangel/article/details/88653846)
[GitLab Docker 容器的内存优化之路](https://www.imooc.com/article/48668)
[gitlab的docker安装,非标准端口,如何处理?](https://www.cnblogs.com/aguncn/p/10336175.html)
`gitlab`提供了官方镜像 - [gitlab/gitlab-ce](https://hub.docker.com/r/gitlab/gitlab-ce/)
## 启动容器
运行以下命令启动容器
```
$ docker run --detach \
--publish 7010:7010 \
--publish 7020:22 \
--name gitlab \
--restart always \
--volume /srv/gitlab/config:/etc/gitlab \
--volume /srv/gitlab/logs:/var/log/gitlab \
--volume /srv/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
```
等待容器状态从`Up 1 second (health: starting)`变为`Up 3 minutes (healthy)`
* 绑定`7010`端口(作用于后续的`external_url`
* 绑定主机`7020`端口到容器`22`端口
* 挂载主机`/srv/gitlab`文件夹到容器
- `/etc/gitlabGitlab`配置文件
- `/var/log/gitlab`:日志
- `/var/opt/gitlab`:应用数据
## 设置external_url
进入`gitlab`容器
```
$ docker exec -it COMTAINER_ID bash
```
修改配置文件`/etc/gitlab/gitlab.rb`,添加`external_url`属性
```
external_url 'http://IP_ADDRESS:7010' # 指定IP地址设置访问地址
nginx['listen_port'] = 7010
gitlab_rails['gitlab_shell_ssh_port'] = 7020
```
在容器内部重启`gitlab`服务
```
$ gitlab-ctl reconfigure
$ gitlab-ctl restart
```
*也可以退出系统后,重新启动容器*
```
$ docker restart CONTAINER_ID
```
等待容器状态从`Restarting (1) 19 seconds ago``Up 18 minutes (healthy)`
启动浏览器登录`http://localhost:7010`
![](./imgs/gitlab-docker-start.png)
## [问题]容器一直重启
使用`docker logs`命令查询
```
$ docker log CONTAINER_ID
...
...
System Info:
------------
chef_version=14.13.11
platform=ubuntu
platform_version=16.04
ruby=ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
program_name=/opt/gitlab/embedded/bin/chef-client
executable=/opt/gitlab/embedded/bin/chef-client
Running handlers:
There was an error running gitlab-ctl reconfigure:
/etc/gitlab/gitlab.rb:1: unexpected fraction part after numeric literal
external_url = 192.168.0.144:7002
^~~~~~~
/etc/gitlab/gitlab.rb:1: syntax error, unexpected tINTEGER, expecting end-of-input
external_url = 192.168.0.144:7002
^~~
Running handlers complete
Chef Client failed. 0 resources updated in 01 seconds
```
发现是`external_url`配置失误导致。解决方法如下:
1. 停止`gitlab`容器
2. 修改配置文件(在主机中)`/srv/gitlab/config/gitlab.rb`
3. 重新启动容器

View File

@@ -0,0 +1,12 @@
# [GitHub]版本发布
之前通过本地进行版本标记,然后上传到`github`,在`release`页面能够查询到相应的版本。参考[标签设置](https://zj-git-guide.readthedocs.io/zh_CN/latest/basic/%E6%A0%87%E7%AD%BE%E8%AE%BE%E7%BD%AE.html)
![](./imgs/release/local-release.png)
但是查看其他的`github`项目的`release`页面,发现发布的版本中还包含了多个压缩文件(比如`Windows/Linux/MacOS`),可以单独下载
![](./imgs/release/remote-release.png)
学习`GitHub`文档[About releases](https://help.github.com/en/github/administering-a-repository/about-releases)和[Creating releases](https://help.github.com/en/github/administering-a-repository/creating-releases)后发现必须通过**在线标签设置**的方式才能额外上传单独的压缩文件

View File

@@ -0,0 +1,18 @@
# [GitLab][Webhook]不允许本地连接
`gitlab`仓库中设置`Webhook`,使用本地连接出现如下错误:
```
Url is blocked: Requests to the local network are not allowed
```
参考:[gitlab使用webhook向jenkins发送请求报错 Requests to the local network are not allowed](https://blog.csdn.net/xukangkang1hao/article/details/80756085)
登录`root`账户,点击`Configure Gitlab`选项
![](./imgs/configure-gitlab.png)
进入`Settings -> Network`,展开`Outbound requests`,选中`Allow requests to the local network from web hooks and services`
![](./imgs/allow-local-request.png)

View File

@@ -0,0 +1,52 @@
# [GitLab][nginx]反向代理
当前`gitlab`登录路径是`localhost:8080`
```
$ curl localhost:8800
<html><body>You are being <a href="http://localhost:8800/users/sign_in">redirected</a>.</body></html>
```
地址会跳转到`http://localhost:8800/users/sign_i`,下面通过反向代理简化登录地址
## 调整gitlab登录地址
修改`gitlab`配置文件`/etc/gitlab/gitlab.rb`,修改`external_url`访问路径
```
external_url 'http://localhost:8800/gitlabs/'
```
更新`gitlab`配置
```
$ gitlab-ctl reconfigure
$ gitlab-ctl restart
```
## nginx配置
修改`nginx`配置文件`/etc/nginx/conf.d/default.conf`,新增`location`
```
$ cat gitlab.conf
server {
...
...
location /gitlabs/ {
proxy_pass http://localhost:8800;
}
...
...
}
```
刷新`nginx`
```
$ sudo nginx -t
$ sudo nginx -s reload
```
之后输入`localhost/gitlabs/`即可登录`gitlab`

View File

@@ -0,0 +1,16 @@
# [GitLab]受保护分支
强制上传代码到`Gitlab`仓库的`dev`分支出错,提示如下:
```
GitLab: You are not allowed to force push code to a protected branch on this project
```
参考:[解决 GitLab: You are not allowed to force push code to a protected branch on this project问题](https://blog.csdn.net/mqdxiaoxiao/article/details/95794053)
是由于分支保护的原因,需要进入仓库`setting -> Repository -> Protected Branches`
![](./imgs/protected-branches.png)
允许`dev`分支能够强制推送

View File

@@ -0,0 +1,26 @@
# [GitLab]命令行操作
* 启动
sudo gitlab-ctl start
* 停止
sudo gitlab-ctl stop
* 重启
sudo gitlab-ctl restart
* 更新配置
sudo gitlab-ctl reconfigure
* 查询日志
sudo gitlab-ctl tail
* 查询状态
sudo gitlab-ctl status

View File

@@ -0,0 +1,140 @@
# [GitLab]安装
## 硬件配置
参考:[Requirements](https://docs.gitlab.com/ce/install/requirements.html#hardware-requirements)
官方推荐的硬件配置为:
1. `2 cores CPU`
2. `8GB RAM`
## 安装
进入[gitlab/gitlab-ce](https://packages.gitlab.com/gitlab/gitlab-ce)查询
首先安装依赖项
```
$ curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
Detected operating system as Ubuntu/xenial.
Checking for curl...
Detected curl...
Checking for gpg...
Detected gpg...
Running apt-get update... done.
Installing apt-transport-https... done.
Installing /etc/apt/sources.list.d/gitlab_gitlab-ce.list...done.
Importing packagecloud gpg key... done.
Running apt-get update... done.
The repository is setup! You can now install packages.
```
然后安装`gitlab-ce`
```
# 当前是ubuntu 16.04
$ sudo apt-get install gitlab-ce
```
*也可以下载.deb包后安装*
```
$ sudo dpkg -i gitlab-ce_12.3.5-ce.0_amd64.deb
Selecting previously unselected package gitlab-ce.
(Reading database ... 368157 files and directories currently installed.)
Preparing to unpack gitlab-ce_12.3.5-ce.0_amd64.deb ...
Unpacking gitlab-ce (12.3.5-ce.0) ...
Setting up gitlab-ce (12.3.5-ce.0) ...
It looks like GitLab has not been configured yet; skipping the upgrade script.
*. *.
*** ***
***** *****
.****** *******
******** ********
,,,,,,,,,***********,,,,,,,,,
,,,,,,,,,,,*********,,,,,,,,,,,
.,,,,,,,,,,,*******,,,,,,,,,,,,
,,,,,,,,,*****,,,,,,,,,.
,,,,,,,****,,,,,,
.,,,***,,,,
,*,.
_______ __ __ __
/ ____(_) /_/ / ____ _/ /_
/ / __/ / __/ / / __ `/ __ \
/ /_/ / / /_/ /___/ /_/ / /_/ /
\____/_/\__/_____/\__,_/_.___/
Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
sudo gitlab-ctl reconfigure
For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
```
## 系统配置
打开文件`/etc/gitlab/gitlab.rb`修改external_url为自己的地址
```
$ sudo cat /etc/gitlab/gitlab.rb | grep external_url
[sudo] password for zj:
Sorry, try again.
[sudo] password for zj:
##! For more details on configuring external_url see:
##external_url 'http://gitlab.example.com'
external_url 'http://localhost:8800'
...
...
```
重新进行gitlab配置
```
$ sudo gitlab-ctl reconfigure
Starting Chef Client, version 14.13.11
resolving cookbooks for run list: ["gitlab"]
Synchronizing Cookbooks:
- gitlab (0.0.1)
- package (0.1.0)
- redis (0.1.0)
- postgresql (0.1.0)
- monitoring (0.1.0)
- registry (0.1.0)
- mattermost (0.1.0)
- consul (0.1.0)
- gitaly (0.1.0)
- letsencrypt (0.1.0)
- nginx (0.1.0)
- runit (4.3.0)
- crond (0.1.0)
- acme (4.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
...
...
Running handlers:
Running handlers complete
Chef Client finished, 526/1417 resources updated in 02 minutes 32 seconds
gitlab Reconfigured!
```
登录修改后的网址:`http://localhost:8800`
![](./imgs/gitlab-start.png)
管理员`root`的密码设置完成后就自动跳转到登录页面
可以直接使用`root`用户登录,也可以注册一个新帐号

View File

@@ -0,0 +1,63 @@
# [GitLab]重置密码
昨天新建的账户,今天突然等不上去了,参考以下文章重置密码
[gitlab 重置用户密码](https://blog.csdn.net/shanpenghui/article/details/89195511)
[gitlab管理员密码忘记如何强制重置找回](https://jingyan.baidu.com/article/6525d4b181bd41ac7d2e94af.html)
进入`gitlab`管理后台。当前使用`docker`版本`gitlab`,所以先进入容器
```
$ docker exec -it CONTIANINER_ID bash
```
切换到`git`用户
```
$ su git
```
登录`gitlab-rails`
```
$ gitlab-rails console production
DEPRECATION WARNING: Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead. (called from require at bin/rails:4)
--------------------------------------------------------------------------------
GitLab: 12.4.2 (393a5bdafa2)
GitLab Shell: 10.2.0
PostgreSQL: 10.9
--------------------------------------------------------------------------------
Loading production environment (Rails 5.2.3)
irb(main):001:0>
```
(上面这一步有点慢),查询用户是否存在
```
irb(main):001:0> user=User.where(name: "zxxU").first
=> nil
irb(main):002:0> user=User.where(name: "zxxu").first
=> nil
irb(main):003:0> user=User.where(name: "zxxxn").first
=> #<User id:2 @zxxxU>
```
充值密码并确认
```
irb(main):005:0> user.password=12345678
=> 12345678
irb(main):006:0> user.password_confirmation=12345678
=> 12345678
```
保存并退出
```
irb(main):007:0> user.save
Enqueued ActionMailer::DeliveryJob (Job ID: 2b220e18-d0ac-415e-a1cc-52f31069d3be) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007f4e84a25510 @uri=#<URI::GID gid://gitlab/User/2>>
=> true
irb(main):008:0> quit
```

View File

@@ -0,0 +1,29 @@
# [Github]主页美化
看到有些人的主页有一些自定义的样式,在网上找了一些资料,主要修改两方面:
1. 添加自定义`README`
2. 启动`Activity View`
## 自定义README
参考:[Github 美化设置个人主页 ](https://www.cnblogs.com/Cl0ud/p/13764921.html)
创建一个同名仓库,即可启动`Github`隐藏功能:编辑这个仓库的`README`文件可以展示在公共`profile`
找到一些好用的工具和资料:
1. 优秀模板:[ kautukkundan/Awesome-Profile-README-templates](https://github.com/kautukkundan/Awesome-Profile-README-templates/tree/master/elaborate)
2. 统计工具:[ anuraghazra/github-readme-stats ](https://github.com/anuraghazra/github-readme-stats/blob/master/docs/readme_cn.md)
3. 模板生成器:[GitHub Profile README Generator](https://github.com/rahuldkjain/github-profile-readme-generator)
## 启动Activity View
参考:
[Viewing contributions on your profile](https://docs.github.com/en/github/setting-up-and-managing-your-github-profile/viewing-contributions-on-your-profile)
[Showing an overview of your activity on your profile](https://docs.github.com/en/github/setting-up-and-managing-your-github-profile/showing-an-overview-of-your-activity-on-your-profile)
在主页的`Contribution settings`下拉框中选中`Activity overview`选项即可

View File

@@ -0,0 +1,26 @@
# Access deined: You do not have permission push to this repository
在gitee上新建了一个账户在上面新建一个仓库将其ssh链接设置到本地已存在的仓库上想要把本地仓库推送到远程仓库出现如下错误
```
$ git push origin master
Access deined: You do not have permission push to this repository
fatal: 无法读取远程仓库。
请确认您有正确的访问权限并且仓库存在。
```
其原因在于本地新建了两个`ssh`密钥,设置在`gitee`的不同账户上ssh使用之前的密钥读取gitee远程仓库导致出错。参考[如何使用特定的SSH Key提交GIT](https://www.jianshu.com/p/82aa1678411e)
`~/.ssh/config`文件上添加如下内容
```
Host gitee.com
HostName gitee.com
User git
IdentityFile 私钥文件路径
IdentitiesOnly yes
```
重新提交即可

View File

@@ -0,0 +1,38 @@
# [github]DNS加速
## 加速原理
通过在本地域名文件中加入`github`相关网站的`DNS`地址,加快`DNS`解析过程,从而加快网页加载速度
## 方式一
参考:[加快国内访问Github网站的速度](https://aoenian.github.io/2018/05/12/github-access-fast/)
登录[DNS查询](http://tool.chinaz.com/dns/?type=1&host=github.com&ip=)网站进行查询
![](./imgs/github-dns.png)
分别查询以下网址对应的`DNS`服务器地址
```
github.com
github.global.ssl.fastly.net
assets-cdn.github.com
```
将查询得到的最短响应时间的服务器`IP`放置到文件`/etc/hosts`文件中
```
$ cat /etc/hosts
...
...
52.74.223.119 github.com
69.171.248.65 github.global.ssl.fastly.net
185.199.111.153 assets-cdn.github.com
```
## 方式二(推荐)
配置公告`DNS`,参考[[Ubuntu 18.04][resolv.conf]公共DNS设置](https://zj-network-guide.readthedocs.io/zh_CN/latest/advanced/[Ubuntu%2018.04][resolv.conf]%E5%85%AC%E5%85%B1DNS%E8%AE%BE%E7%BD%AE/)

View File

@@ -0,0 +1,70 @@
# [github][gitee]SSH公钥设置
## [github]公钥设置
参考:[Adding a new SSH key to your GitHub account](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/)
登录`github`,进入个人主页,选择`Settings->SSH and GPG keys->New SSH key`
填写`title`,并复制个人主机中的公钥到`key`
$ cat github_id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDIUsnRNu/+Zeurm2Ty/Xac+q+0y87aHWr0N7fb+gygQK7xRux9UAI7UK3GwP7dwxYT8KTOrxf0tPnI4d/bCtETHcoj0D2UlwMO20FWFaoOocteccJLbmQ9XXsrt05KlJcDGr9L8e9eMyWKj5ZXMOxSwravv+e64XGkFaAmNX4XkCkENlZcIq6w+Fo/xtyPZA0W/oYnBWAvKd937GbKEA1m1rARzo/xGCn8uf76PUlNy2UscujnWbMUYGeLxf5jRCgWD0KZLJsJAsOCxczeRn0EAczgNcgyBbjotDnpTKbP6Xtqvf3TtVty9fgQgs7/oLNb8K+v0XVVR9XsX+DZkqF+JCzIOf+0dpnJiery187rw4MuRIlct+vGM1P+FaiPphh2vMhe5YEaFErrHNTDZiw+LbHCE3Eo9wK9t0wb/yFiHELempoZI+K3Zjv6cnUnl3urXQFHV/RJCj/JHOEA1Q3jvVLa3nETRMfBBEiDuK0YPH6OUac+GSg+ldCVwDydeCa2Z4/OdgZqsoyTU5o4vewbT3vKwmQGfu7BNOmHgnIzMZQsg0JWScDX9/c/DoCaGW95ej68niZ3ICtUWnlYKZVlzg+cyIRzuzSmaa5ecOgzxZ6moW4wRGrvMM94X0HMmWWXXV/WLXiUG2KN1upz7m78zwJ6ZXvh2sWmCe+q+YhJIw== github.com
`github`上设置的公钥同时具备`推送/拉取`的权限
### 密钥失效
参考:[About SSH](https://help.github.com/articles/about-ssh/)
If you haven't used your SSH key for a year, then GitHub will automatically delete your inactive SSH key as a security precaution. For more information, see "Deleted or missing SSH keys."
`github`规定自动删除一年内未使用的`SSH`密钥
## [gitee]公钥设置
参考:
[公钥管理](https://gitee.com/help/articles/4180)
[SSH 公钥设置](https://gitee.com/help/articles/4191#article-header0)
码云可以在个人主页上设置公钥,也可以在个人仓库上设置公钥,这两个地方的读写权限不一致
### 个人主页
登录码云,选择`设置->安全设置->SSH公钥`,在添加公钥页面输入标题和公钥即可设置
在个人主页上设置的公钥同时具备`推送/拉取`的权限
### 个人仓库
进入仓库页面,选择`管理->部署公钥管理->公钥管理`,点击添加公钥选项,输入标题和公钥即可
在个人仓库上设置的公钥仅具备`拉取仓库代码`的功能
## 测试
参考:[Testing your SSH connection](https://help.github.com/articles/testing-your-ssh-connection/)
`github`上设置好公钥之后
SSH
zj-github
d9:fc:d8:02:ee:6c:75:68:a1xxx Added on Feb 4, 2019 Last used within the last week — Read/write
在本地测试是否成功
$ ssh -T git@github.com
Hi zjZSTU! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@gitee.com
Hi zjZSTU! You've successfully authenticated, but GITEE.COM does not provide shell access.
## 下载代码
选择仓库,以`ssh`协议下载
$ git clone git@github.com:zjZSTU/git-guide.git
这一次会要求你输入设置好的密码(`passphrase`),之后的拉取代码操作都可以自动认证完成

BIN
docs/platform/imgs/8080.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 100 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

View File

@@ -0,0 +1,62 @@
# unicorn出错
## 问题描述
通过`docker`部署`gitlab`一段时间后,突然出现`502`错误
查看容器状态,显示不健康
```
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
89481faa2ed1 gitlab/gitlab-ce:latest "/assets/wrapper" 2 weeks ago Up 7 hours (unhealthy)
```
浏览容器日志,发现如下错误:
```
==> /var/log/gitlab/unicorn/unicorn_stdout.log <==
bundler: failed to load command: unicorn (/opt/gitlab/embedded/bin/unicorn)
==> /var/log/gitlab/unicorn/unicorn_stderr.log <==
ArgumentError: Already running on PID:460 (or pid=/opt/gitlab/var/unicorn/unicorn.pid is stale)
/opt/gitlab/embedded/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:205:in `pid='
/opt/gitlab/embedded/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/lib/unicorn/http_server.rb:137:in `start'
/opt/gitlab/embedded/lib/ruby/gems/2.6.0/gems/unicorn-5.4.1/bin/unicorn:126:in `<top (required)>'
/opt/gitlab/embedded/bin/unicorn:23:in `load'
/opt/gitlab/embedded/bin/unicorn:23:in `<top (required)>'
==> /var/log/gitlab/unicorn/current <==
2019-12-18_12:27:47.34406 failed to start a new unicorn master
2019-12-18_12:27:47.35882 starting new unicorn master
2019-12-18_12:27:47.91735 master failed to start, check stderr log for details
```
## 问题解析
看样子是`unicorn`的问题,参考:
[gitlab服务器502恢复过程](https://blog.csdn.net/zhouchuan152/article/details/95871798)
[gitlab docker Web界面打开反应迟钝的解决办法](https://blog.csdn.net/happyfreeangel/article/details/88653846)
进入容器内部,查看`unicorn`状态
```
$ docker exec -it xxxx bash
# gitlab-ctl tail unicorn
```
发现每次`unicorn`显示的`PID`都不同,修改`/etc/gitlab/gitlab.rb`,添加
```
unicorn['listen'] = 'localhost'
unicorn['port'] = 8999
```
更新并重启后,`gitlab`服务恢复正常
```
# gitlab-ctl reconfigure
# gitlab-ctl restart
```

View File

@@ -0,0 +1,36 @@
# 代码库大小限制
## 问题描述
上传代码到`Gitee`时,打印出如下日志:
```
git push --force git@gitee.com:xx.xx.git master
remote: Powered by GITEE.COM [GNK-3.8]
remote: Recommended to reduce the repository size
remote: This repository(including wiki) size 581.64 MB exceeds 500.00 MB
remote: HelpLink: https://gitee.com/help/articles/4232
To gitee.com:zjZSTU/xx.xxx.git
```
在网上查询发现是因为单个仓库大小超出了限制(`500MB`),经过一番查询发现`Gitee``Coding`的个人仓库都限制在`500MB`
参考:
[仓库体积过大,如何减小? ](https://gitee.com/help/articles/4232#article-header0)
[仓库大小限制吗?](https://cloud.tencent.com/developer/ask/197652)
`Github`对单个仓库大小的限制为`1GB`。参考:[File and repository size limitations](https://help.github.com/en/github/managing-large-files/what-is-my-disk-quota#file-and-repository-size-limitations)
## 问题解决
阿里云的单个仓库限制为`2GB`,能够满足当前需求。参考:[代码库对文件大小是否有限制?](https://help.aliyun.com/document_detail/153791.html?spm=5176.11065259.1996646101.searchclickresult.30c1183bbCmNS7)
各个远程`Git`仓库服务器的官网地址如下:
1. [Gitee](https://gitee.com/)
2. [Coding](https://coding.net/)
3. [Github](https://github.com/)
4. [Aliyun Code](https://code.aliyun.com)

View File

@@ -0,0 +1,40 @@
# 版本更新后database出错
### 问题描述
拉取了最新的`Docker Gitlab`镜像,部署时发现了如下错误:
```
gitlab | System Info:
gitlab | ------------
gitlab | chef_version=14.14.29
gitlab | platform=ubuntu
gitlab | platform_version=16.04
gitlab | ruby=ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
gitlab | program_name=/opt/gitlab/embedded/bin/chef-client
gitlab | executable=/opt/gitlab/embedded/bin/chef-client
gitlab |
gitlab |
gitlab | Running handlers:
gitlab | There was an error running gitlab-ctl reconfigure:
gitlab |
gitlab | bash[migrate gitlab-rails database] (gitlab::database_migrations line 55) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
gitlab | ---- Begin output of "bash" "/tmp/chef-script20200618-23-sfpkhu" ----
gitlab | STDOUT: rake aborted!
gitlab | PG::ConnectionBad: could not connect to server: No such file or directory
gitlab | Is the server running locally and accepting
gitlab | connections on Unix domain socket "/var/opt/gitlab/postgresql/.s.PGSQL.5432"?
gitlab | /opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:48:in `block (3 levels) in <top (required)>'
gitlab | /opt/gitlab/embedded/bin/bundle:23:in `load'
gitlab | /opt/gitlab/embedded/bin/bundle:23:in `<main>'
gitlab | Tasks: TOP => gitlab:db:configure
gitlab | (See full trace by running task with --trace)
gitlab | STDERR:
gitlab | ---- End output of "bash" "/tmp/chef-script20200618-23-sfpkhu" ----
gitlab | Ran "bash" "/tmp/chef-script20200618-23-sfpkhu" returned 1
```
### 问题解决
在网上查了一些资料,说是新旧版本的配置格式不一致,需要删除之前的配置数据。幸好在本地保存了之前使用的镜像,打包该镜像后上传到服务器重新加载即可

View File

@@ -54,7 +54,7 @@ nav:
- 工具:
- Git:
- 引言: git/index.md
- Base:
- 入门:
- 关于版本控制: git/base/关于版本控制.md
- 工作区域: git/base/工作区域.md
- '[git config]配置': git/base/git-config.md
@@ -64,4 +64,16 @@ nav:
- 标签设置: git/base/标签设置.md
- '.gitkeep': git/base/gitkeep.md
- 子模块: git/base/子模块.md
- '[ssh][http]传输方式切换': git/base/ssh-http-传输方式切换.md
- '[ssh][http]传输方式切换': git/base/ssh-http-传输方式切换.md
- 进阶:
- 绘制分支图: git/advanced/绘制分支图.md
- 创建裸仓库: git/advanced/创建裸仓库.md
- 工作目录和裸仓库分离: git/advanced/工作目录和裸仓库分离.md
- 自建服务器: git/advanced/自建服务器.md
- 推送到多个远程托管服务器: git/advanced/推送到多个远程托管服务器.md
- 本地分支操作: git/advanced/本地分支操作.md
- 远程分支操作: git/advanced/远程分支操作.md
- 拉取指定远程分支到本地: git/advanced/拉取指定远程分支到本地.md
- 远程分支替换本地分支: git/advanced/远程分支替换本地分支.md
- 为每个项目单独设置用户名和邮箱: git/advanced/为每个项目单独设置用户名和邮箱.md
- ERROR: git/error.md