一、配置Git
1.1 Github
在本地创建ssh key
ssh-keygen -t rsa -C "[email protected]"
后面的[email protected]改为你在github上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。
回到github上,进入 Account Settings(账户配置),左边选择SSH Keys,Add SSH Key,title随便填,粘贴在你电脑上生成的key。
为了验证是否成功,在git bash下输入
ssh -T [email protected]
如果是第一次的会提示是否continue,输入yes就会看到:You've successfully authenticated, but GitHub does not provide shell access 。
这就表示已成功连上github。
1.2 GitLab
在本地创建ssh key
- Generate a new ED25519 SSH key pair:
ssh-keygen -t rsa -C "[email protected]" -b 4096
- Generate RSA:
ssh-keygen -o -t rsa -b 4096 -C "[email protected]"
将密钥保存到云端服务器。成功生成密钥后需要将它保存到云端,我们使用以下命令先将其复制到剪切板。或者按照Github复制密钥。
cat ~/.ssh/id_rsa.pub | clip
1.3 配置账户名和密码
接下来我们要做的就是把本地仓库传到github/gitlab上去,在此之前还需要设置username和email,因为github/gitlab每次commit都会记录他们。
git config --global user.name "your name"
git config --global user.email "[email protected]"
二、Git的使用
2.1 添加仓库
进入要上传的仓库,右键git bash,添加远程地址
git remote add origin [email protected]:yourName/yourRepo.git
后面的yourName和yourRepo表示你在github的用户名和刚才新建的仓库,加完之后进入.git,打开config,这里会多出一个remote "origin"内容,这就是刚才添加的远程地址,也可以直接修改config来配置远程地址。
创建新文件夹,打开,然后执行git init
以创建新的git仓库。
2.2 检出仓库
执行如下命令以创建一个本地仓库的克隆版本
git clone /path/to/repository
如果是远端服务器上的仓库,你的命令会是这个样子
git clone username@host:/path/to/repository
2.3 同步本地仓库
- 在github/gitlab创建仓库
- 本地目录初始化git
- 本地提交当前文件
- 连接到远程仓库
git remote add origin url
origin:(自定义)连接到远程仓库并为该仓库创建别名,一般为origin,push或者pull时需使用。
url:远程仓库链接
- 上传内容
git push -u origin master
创建一个 upStream (上传流),并将本地代码通过这个 upStream 推送到 别名为 origin 的仓库中的 master 分支上
- 正常使用
2.4 代理配置
- 设置代理
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'
- 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy