git的常见问题处理

git的常见问题处理

1.Git Push 免输用户名和密码

  1. 进入C:\users\用户名目录,右击-Git-Bas-Here

  2. 创建.git-credentials文件

    1
    2
    3
    4
    vim .git-credentials
    # github的用户名和密码,如果是其他平台改成对应的
    https://{username}:{password}@github.com

  3. 添加Git Config 内容

    1
    git config --global credential.helper store
  4. 重新push,可能还会让输入一次密码,之后就不需要了

2. git配置用户

  1. 右击-Git-Bas-Here

  2. 设置Git的user name和eamil

    1
    2
    git config --global user.name "test1"
    git config --global user.email "test1@126.com"
  3. 生成SSH-KEY

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ssh-keygen -t rsa -C "test1@126.com"


    Generating public/private rsa key pair.
    # 这里会让输入key存储的位置和名称,默认就是括号里面的,可以直接回车
    Enter file in which to save the key (/c/Users/admin/.ssh/id_rsa):
    # 后面的都可以直接回车


    #默认会生成id_rsa和id_rsa.pub两个密钥文件
  4. 登录到对应git平台,添加SSH公钥即可(把id_rsa.pub中的内容贴进去)

3.git配置多用户

可能同时又多个平台分别用了不同的用户,例如GitHub和Gitee,分别使用了test1和test2两个用户。

  1. 分别为两个用户生成SSH

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    步骤和上面的一致,区别是再保存的时候分别给不同的名字

    Generating public/private rsa key pair.
    # github
    Enter file in which to save the key (/c/Users/admin/.ssh/id_rsa):/c/Users/admin/.ssh/github_id_rsa


    # gitee
    Enter file in which to save the key (/c/Users/admin/.ssh/id_rsa):/c/Users/admin/.ssh/gitee_id_rsa


    #默认会生成4个id_rsa和id_rsa.pub密钥文件
  2. 修改config文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    vim /c/Users/admin/.ssh/config

    #gitee
    Host gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitee_id_rsa

    #github
    Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_id_rsa
  3. 到各自的页面上导入证书即可

4.Git Bash终端中文输出显示乱码

image-20230428195617845

  1. 在终端的空白处,点击鼠标右键,弹出的菜单中选择【options…】

    image-20230428195707324

  2. 弹出的菜单中找到【Text】->【Local】,下拉菜单中选择zh_CN

    image-20230428195738986

  3. 在【Character set】下拉菜单中选择【utf-8】,既是选择简体中文,最后点击apply

    image-20230428195800339

  4. 执行命令: git config --global core.quotepath false ,执行命令git status显示结果如图所示

    image-20230428195837683

  5. 继续执行命令: git diff 仍然不能显示中文信息,这是因为git bash显示的编码格式是utf-8,而windows记事本的编码格式是gb2312,请确认文本的格式是utf-8

    image-20230428200205600

5. windows git 更改为unix模式换行符决解方法

Windows用CR LF来定义换行Linux用LF。可以设置git提交的时候进行统一的更改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true

#提交时转换为LF,检出时不转换
git config --global core.autocrlf input

#提交检出均不转换
git config --global core.autocrlf false
SafeCRLF
#拒绝提交包含混合换行符的文件
git config --global core.safecrlf true

#允许提交包含混合换行符的文件
git config --global core.safecrlf false

#提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

6.git log 的使用

1
2
3
4
5
6
7
8
9
10
11
1.列出所有最近两周内的提交
$ git log --since=2.weeks

2.显示指定作者的提交
git log --author

3.仅显示简要的增改行数统计
git log --stat

4.展开显示每次提交的内容差异, 用 -2 则仅显示最近的两次更新
git log -p -2

7.git tag 以及发布 release

  1. 查看提交过的commit,命令git log

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root@~]# git log

    commitc98c186ebb381005b495f6f1f2a65dc72195ad9d ---这个就是最近一次提交的

    Author:Yongshi Xu <yongshi.xu@samsung.com>

    Date: Wed Aug 31 15:24:26 2016 +0800


    basicsecurity(credential) - user:password


    commit0c2ac2f09edc41111d451043961a657f750651c6

    Author:Yongshi Xu <yongshi.xu@samsung.com>

    Date: Tue Aug 30 18:08:18 2016 +0800


    deletethe duplicated harbor/auth codes
  2. 在最后一次commit上 打标签

    1
    git tag -a "v0.2" c98c186ebb381005b495f6f1f2a65dc72195ad9d -m 'v0.2'
  3. 上传

    1
    git push origin v0.2
  4. 发布 release

    先对对应的 commit 绑定 tag,按照图片中按钮生成一个 release,选定 tag,填写 description 然后 publish 即可

    image-20230428202514875

image-20230428202629280

8. git用本地仓库恢复远程仓库

远程仓库被删除了,本地还有之前clone仓库,可以用这个来恢复远程被删除的仓库

  1. 现在远程仓库中创建一个新的仓库

  2. 进入到本地仓库的目录,打开git-bash

  3. 将之前的连接先清除掉

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # 查看当前的连接情况
    zmr@DESKTOP-KLMBVGR MINGW64 /e/git-code/hexo-next-blog (master)
    $ git remote -v
    origin http://10.1.100.4:3000/ha/hexo-next-blog.git (fetch)
    origin http://10.1.100.4:3000/ha/hexo-next-blog.git (push)

    #移除连接
    zmr@DESKTOP-KLMBVGR MINGW64 /e/git-code/hexo-next-blog (master)
    $ git remote rm origin

    # 确认已经移除了
    zmr@DESKTOP-KLMBVGR MINGW64 /e/git-code/hexo-next-blog (master)
    $ git remote -v

  4. 重新建立连接

    1
    2
    zmr@DESKTOP-KLMBVGR MINGW64 /e/git-code/hexo-next-blog (master)
    $ git remote add origin https://10.1.100.4:3000/ha/hexo-blog.git
  5. 强制push上去

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    zmr@DESKTOP-KLMBVGR MINGW64 /e/git-code/hexo-next-blog (master)
    $ git push origin master --force
    warning: ----------------- SECURITY WARNING ----------------
    warning: | TLS certificate verification has been disabled! |
    warning: ---------------------------------------------------
    Compressing objects: 100% (542/542), done.
    Writing objects: 100% (629/629), 907.51 KiB | 69.81 MiB/s, done.
    Total 629 (delta 56), reused 629 (delta 56), pack-reused 0
    remote: Resolving deltas: 100% (56/56), done.
    remote: . Processing 1 references
    remote: Processed 1 references in total
    To https://10.1.100.4:3000/ha/hexo-blog.git
    + 9bfca65...7545eac master -> master (forced update)
  6. 去远程仓库确认已经恢复了,并且之前的提交记录都是在的

9.git pull出错

错误信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
λ git pull                                                                                   
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more i
nformation.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more i
nformation.
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> master

原因可能是移动了本地仓库的目录,需要更新一下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 先确认一下当前仓库的连接地址是否还在
E:\git-code\hexo-next-blog (master)
λ git remote -v
origin https://gitea.kiki.kim/ha/hexo-blog.git (fetch)
origin https://gitea.kiki.kim/ha/hexo-blog.git (push)

# 更新一下
E:\git-code\hexo-next-blog (master)
λ git push -u origin master
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
warning: ----------------- SECURITY WARNING ----------------
warning: | TLS certificate verification has been disabled! |
warning: ---------------------------------------------------
warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information.
Everything up-to-date
Branch 'master' set up to track remote branch 'master' from 'origin'.

#再次pull测试是否可以

10. 一次性提交的文件大小超过限制,导致提交失败

报错信息:

1
error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413

原因:

因为 Nginx 的 client_max_body_size 默认大小只有 1M,一次性提交的文件大小超过了限制

处理方法,修改nginx的配置即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# client_max_body_size 128m;

location /{
client_max_body_size 128m; ---这里增加配置项
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.1.100.4:3000;
}
}

重启nginx