天天看点

《Git-使用与配置》

文章目录

    • 一、在线文档
      • 官方文档:https://git-scm.com/docs
      • 中文文档:https://git-scm.com/book/zh/v2
    • 二、公钥管理
      • 1、公钥管理
      • 2、生成/添加SSH公钥
      • 3、Git配置多个SSH-Key
      • 4、如何在码云上使用 GPG
    • 三、用户配置
      • 1、设置用户名
      • 2、设置用户邮箱
      • 3、查看git设置列表信息
      • 4、查看用户名
    • 四、.gitignore、.gitattribute 文件作用
    • 五、Git常用命令
      • 1、git clone 不指定分支
      • 2、git clone 指定分支
      • 3、添加或指定远程仓库地址
      • 4、添加超出范围100M文件
      • 5、git status中文文件名编码问题解决
    • 六、Git常见问题解决
      • 1、Git-remote Incorrect username or password ( access token )
      • 2、git clone 克隆或下载一个仓库单个文件夹
      • 3、git push大文件处理方法
      • 4、git pull 时每次都要输入用户名和密码的解决办法
      • 5、git status 查看中文乱码问题

前言:

git是分为三部分,一部分是你自己的文件,另外一个是缓存区,最后一个是本地库。当你修改了自己的文件后,你会git add

xx将修改保存到缓存区,然后再用commit推送修改到本地库中。 git push 将本地仓库修改推送到服务器上的仓库中 commit

是将本地修改保存到本地仓库中

一、在线文档

官方文档:https://git-scm.com/docs

中文文档:https://git-scm.com/book/zh/v2

二、公钥管理

1、公钥管理

地址:https://gitee.com/help/articles/4180

2、生成/添加SSH公钥

地址:https://gitee.com/help/articles/4181

https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

ssh-keygen -t ed25519 -C "[email protected]"
           

重要一环,如下图:

《Git-使用与配置》

3、Git配置多个SSH-Key

地址:https://gitee.com/help/articles/4229

4、如何在码云上使用 GPG

地址:https://gitee.com/help/articles/4248

三、用户配置

1、设置用户名

$ git config --global user.name “yourname”
           

2、设置用户邮箱

$ git config --global user.email [email protected]
           

3、查看git设置列表信息

$ git config --list
           

4、查看用户名

$ git config user.name
           

四、.gitignore、.gitattribute 文件作用

gitignore 用于忽略你不想提交到Git上的文件

.gitattribute 指定非文本文件的对比合并方式

.gitigonre 你想要忽略的文件或者目录

/mtk/ 过滤整个文件夹

*.zip 过滤所有.zip文件

/mtk/do.c 过滤某个具体文件

.gitattribute 用于设置文件的对比方式(常用非文本文件)

五、Git常用命令

1、git clone 不指定分支

git clone  http://10.1.1.11/service/tmall-service.git
           

2、git clone 指定分支

git clone -b dev_jk http://10.1.1.11/service/tmall-service.git
           

3、添加或指定远程仓库地址

git remote set-url origin "https://..."
git config remote.origin.url "https://..."
git remote rm origin-删除
           

4、添加超出范围100M文件

用这个代码找到100m以上的文件

find ./ -size +100M
           

将打文件加入git large file storage

git lfs track "name_of_a_giant_file"
#example:
#git lfs track "fc6_W.csv"
           

加入track之后可以把想要保存的打文件添加到git上

git add path_of_a_giant_file
#example:
#git add vgg16_weight/fc6_W.csv
           

最后进行提交和推送

git commit -m "Add design file"
git push origin master
           

5、git status中文文件名编码问题解决

git config --global core.quotepath false
           

六、Git常见问题解决

1、Git-remote Incorrect username or password ( access token )

$ git clone https://gitee.com/***.git
Cloning into '***'...
remote: ***@163.com: Incorrect username or password (access token)
fatal: Authentication failed for 'https://gitee.com/***.git/'
           

解决办法:清除本地的gitee用户名和密码

git config --system --unset credential.helper
           

2、git clone 克隆或下载一个仓库单个文件夹

地址:https://www.cnblogs.com/zhoudaxiaa/p/8670481.html

1、如果是想克隆别人的项目或者自己的 很简单的一个网站就解决了。DownGit: 只需要找到仓库中对应文件夹的url,输入之后,点击 download 自动打包下载:

(这里说明一下,因为原作者的项目无法使用,这是我修改过的新项目吧,把资源链接改到了国内CDN,所以访问速度很快!)

2、克隆自己的项目 注意:本方法会下载整个项目,但是,最后出现在本地项目文件下里只有需要的那个文件夹存在。类似先下载,再过滤。

有时候因为需要我们只想gitclone 下仓库的单个或多个文件夹,而不是全部的仓库内容,这样就很省事,所以下面就开始教程啦

在Git1.7.0以前,这无法实现,但是幸运的是在Git1.7.0以后加入了Sparse Checkout模式,这使得Check

Out指定文件或者文件夹成为可能。

举个例子:

现在有一个test仓库https://github.com/mygithub/test 你要gitclone里面的tt子目录:

在本地的硬盘位置打开Git Bash

git init test && cd test     //新建仓库并进入文件夹
git config core.sparsecheckout true //设置允许克隆子目录

echo 'tt*' >> .git/info/sparse-checkout //设置要克隆的仓库的子目录路径   //空格别漏

git remote add origin [email protected]:mygithub/test.git  //这里换成你要克隆的项目和库

git pull origin master    //下载
           

3、git push大文件处理方法

地址:https://www.jianshu.com/p/f4f34c67707a

4、git pull 时每次都要输入用户名和密码的解决办法

在使用https git拉取代码时,每次git pull的时候都会让输入用户名和密码

1.进入项目目录

git config --global credential.helper store
           

然后会生成一个本地文件用于记录用户名和密码,这个文件我们无需关心

再次git pull一下,会让输入用户名和密码。这次输入之后以后就不会每次输入了。 如果要清除用户名和密码

2.运行一下命令缓存输入的用户名和密码

git config --global credential.helper wincred
           

3.清除掉缓存在git中的用户名和密码

git credential-manager uninstall
           

4.warning: LF will be replaced by CRLF in 解决办法

原因是需要提交的文件是在windows下生成的,windows中的换行符为 CRLF, 而在linux下的换行符为LF,所以在执行add . 时出现提示,解决办法:

git config --global core.autocrlf false
           

再执行git 提交

5.码云 remote: error: GE007: Your push would publish a private email address.

5、git status 查看中文乱码问题

第一步

打开你的终端,依次输入以下命令:

$ git config --global core.quotepath false # 设置 git status utf-8编码

$ git config --global gui.encoding utf-8 # 设置Git GUI界面utf-8编码

$ git config --global i18n.commit.encoding utf-8 #设置commit信息utf-8编码

$ git config --global i18n.logoutputencoding utf-8 # 设置输出 log utf-8 编码

第二步

新建系统环境变量 LESSCHARSET并赋值为utf-8。

《Git-使用与配置》

以上两步完成后,注销电脑重新进入即可。

继续阅读