配置key
Github
在本地创建ssh key
ssh-keygen -t rsa -C "your_email@youremail.com"
后面的your_email@youremail.com改为你在github上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。
回到github上,进入 Account Settings(账户配置),左边选择SSH Keys,Add SSH Key,title随便填,粘贴在你电脑上生成的key。
为了验证是否成功,在git bash下输入
ssh -T git@github.com
如果是第一次的会提示是否continue,输入yes就会看到:You've successfully authenticated, but GitHub does not provide shell access 。
这就表示已成功连上github。
创建个人访问令牌(token)
remote: Support for password authentication was removed on August 13, 2021
github不再支持使用账号密码访问仓库,改为个人访问令牌(token)
路径
Settings->Developer settings->Personal access tokens
注意
- 需要将生成的token保存下来,离开页面后无法再看到token
- 像对待密码一样对待您的令牌,确保其机密性,使用 API 时,应将令牌用作环境变量,而不是将其硬编码到程序中
- token只能用于 HTTPS Git 操作
使用
$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token
gitLab
在本地创建ssh key
-
Generate a new ED25519 SSH key pair:
ssh-keygen -t rsa -C "your.email@example.com" -b 4096
-
Generate RSA:
ssh-keygen -o -t rsa -b 4096 -C "email@example.com"
将密钥保存到云端服务器。成功生成密钥后需要将它保存到云端,我们使用以下命令先将其复制到剪切板。或者按照Github复制密钥。
cat ~/.ssh/id_rsa.pub | clip
配置本地和远程
配置账户名和邮箱
账户名和邮箱是在git提交时记录的,在推送代码时会一并推送。
# local > global
# global
git config --global user.name "your name"
git config --global user.email "your_email@youremail.com"
# local
git config user.name "your name"
git config user.email "your_email@youremail.com"
获取远程仓库
本地无仓库,获取远程建立的仓库到本地进行开发
git clone username@host:/path/to/repository
同步本地仓库
将本地已有仓库同步到远程仓库
-
在github/gitlab创建仓库
-
本地目录初始化git
-
本地提交当前文件
-
连接到远程仓库
git remote add origin https://github.com/user/repository.git
验证
git remote -v # 参考输出 origin https://github.com/user/repository.git (fetch) origin https://github.com/user/repository.git (push)
origin:在默认情况下,origin指向的就是你本地的代码库托管在Github上的版本,
git remote add
就是添加远程仓库并设置名字为origin
,该名字可以自定义,push或者pull时需使用。url:远程仓库链接
-
上传内容
创建一个upStream (上传流),并将本地代码通过upStream推送到origin的仓库中的master分支上
git push -u origin master
master(默认名字,新git改为main)为当前repository中默认创建的第一个branch。
-
正常使用
配置代理
-
设置代理
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 http.proxy 'http://127.0.0.1:1080' git config --global https.proxy 'http://127.0.0.1:1080'
-
取消代理
git config --global --unset http.proxy git config --global --unset https.proxy
-
查看设置情况
通过编辑器直接打开配置文件,可以直接在这上面修改,和上面操作一致。
- Linux:
gedit ~/.gitconfig
- windows:配置文件路径为
C:\Users\Letter\.gitconfig
- Linux: