Git配置及使用(附解决clone或push速度缓慢问题)

Git配置

  1. 指定仓库的用户名和邮箱地址。–global参数表示这台机器上所有的Git仓库都会使用这个配置,去除此参数则表示为当前项目配置。
git config --global user.name "Your Name"

git config --global user.email "[email protected]"
  1. 进入项目文件夹,可使用以下命令使用Git。
git init    //初始化空仓库

git add filename    //添加文件(* 表示添加全部)

git commit -m "wrote a readme file" //文件提交到本地仓库(-m 表示后接本次提交说明)

git status    //查看仓库状态
  1. 生成SSH key,-t参数表示制定密钥类型,默认生成用于SSH-2的RSA密钥。
#windows下
ssh-keygen -t rsa -C "[email protected]"    //默认在C:\Users\用户名\.ssh\id_rsa.pub中

#Mac下
ls -al ~/.ssh 
//第一种情况,显示已经存在id_rsa.pub文件
-rw-------   1 xx  staff  1675 11 17 18:08 id_rsa
-rw-r--r--   1 xx  staff   398 11 17 18:08 id_rsa.pub
//第二种情况,未存在id_rsa.pub文件,则输入以下命令来生成你的SSH Key
ssh-keygen -t rsa -C "你的邮箱"
//再次运行上一步中ls命令,显示生成了id_rsa.pub文件,此时再运行pbcopy命令将SSH Key复制到剪切板。
pbcopy < ~/.ssh/id_rsa.pub
  1. 对本地或远程仓库进行操作。
git remote -v    //查看关联的远程仓库信息

git remote rm 远程仓库名    //删除关联的远程仓库

git remote add 远程仓库名 git仓库地址    //远程仓库名可指定,如码云则可填gitee

git clone git仓库地址    //克隆远程仓库

git push -f 远程仓库名 master    //推送到远程仓库,-f参数表示强制覆盖远程库,master代表分支名

解决clone或push速度缓慢问题

若发现clone或push速度缓慢可尝试以下步骤:

  1. host重定向:通过http://tool.chinaz.com/dns寻找github.com最高效地址,就是TTL最短的IP地址。添加到C:\Windows\System32\drivers\etc\hosts文件中。
192.30.255.112 github.com
103.245.222.249 github.global.ssl.fastly.net
  1. 全局代理:所有git操作都会经过代理。
//添加http和https代理
git config --global http.proxy 127.0.0.1:本地代理端口
git config --global https.proxy 127.0.0.1:本地代理端口

//删除代理
git config --global --unset http.proxy
git config --global --unset https.proxy
  1. 只针对github代理
git config --global http.https://github.com.proxy 127.0.0.1:本地代理端口
git config --global https.https://github.com.proxy 127.0.0.1:本地代理端口

猜你喜欢

转载自blog.csdn.net/qq_39506912/article/details/88774478