Gitlab的SSH配置(linux和windows双版本)
1. 步骤
1.首先现在电脑端安装好git,windows端请安装Git for Windows,Linux端请自行网上查询(Ubuntu: sudo apt-get install git)
2.先核对下电脑上是已经有ssh配置
1 2 | #Git Bash on Windows / GNU/Linux / macOS / PowerShell: cat ~/. ssh /id_rsa .pub |
3.若没有,则需要生成ssh的公钥私钥
Git Bash on Windows / GNU/Linux / macOS:
1 2 3 4 5 6 7 8 9 10 | ##若要自定义id_rsa文件请先切换目录到 ~/.ssh/下,如果不切换,当保存的文件名是自定义时,会生成在当前的目录下。 ##经测试,邮箱不一定是登录gitlab的邮箱(我github也是用mygitlab@gitlab.com生成的id_rsa_pub作为公钥) ssh -keygen -t rsa -C your.email@example.com -b 4096 #提示是否使用新的文件名,如果不输入新的文件名,则生成id_rsa文件。 ##如果默认不配置config,就得默认为id_rsa文件名 Enter file in which to save the key (~/. ssh /id_rsa ): #请输入确认密码,后面还会用到(至少4位数),如果缺省直接按回车 ##此密码是验证id_rsa的密码,每次代码commit时得输入 Enter passphrase (empty for no passphrase): Enter same passphrase again: |
4. 复制公钥到GitLab中的SSH Keys中
Windows Command Line:
1 | type %userprofile%\. ssh \id_rsa.pub | clip |
Git Bash on Windows / Windows PowerShell:
1 | cat ~/. ssh /id_rsa .pub | clip |
如果提示clip没安装,则只要‘ |’ 的前半部分命令,然后复制出文本,或者直接到指定位置用文本打开。
5.测试SSH是否已配置好
1 2 3 | ssh -T git@gitlab.com #如果已经配置好,则会提示 Welcome to GitLab, Your GitLab NickName! |
6.配置config文件(可选,当第5步骤失败)
如果id_rsa是自己定义为id_rsa_custom的,才需要配置config,具体见参考[1]
在~/.ssh/下面新建一个文件
1 2 | cd ~/. ssh / touch config |
config内容如下:
1 2 3 4 5 | # GitLab.com server Host gitlab Hostname gitlab.com User git IdentityFile ~/. ssh /id_rsa_custom |
1 2 3 | ssh -T git@gitlab #如果已经配置好,则会提示 Welcome to GitLab, Your GitLab NickName! |
7.Clone项目到本地
如果gitlab上已经有Repo(项目),则直接clone一份代码到本地
1 2 3 4 5 6 | #如果用SSH clone失败了,请尝试用HTTPS clone ##目前只在company的服务器ubuntu系统失效了 git clone git@gitlab.com:username /yourProject .git #(SSH方式) git clone https: //gitlab .com /username/yourProject .git #(HTTPS方式) #此时会出现登录gitlab的账号和密码的输入,然后显示进度条 Receiving objects: 86% (797 /918 ), 2.48 MiB | 5.00 KiB /s |
如果gitlab还没有Repo,可以新建一个,再clone空项目下来。
按理说github设置SSH原理是完全相同的。
p.s.
如果git还没有配置过用户名和邮箱需要设置一下
1 2 3 4 5 | ##用户名和邮箱名和账号名没有必然相关性,可以不一样 ##我用github的账号设置了user.email,然而gitlab照样可以push数据 ##首次clone数据时有要求输入该网站的账号和密码,可以理解git的配置是git的账号和昵称 git config --global user.name "Your Name" git config --global user.email your_email@gmail.com |
2. 可能出现的情况
在windows系统下用git bash,在第5步骤,一直提示超时连接,不管是ssh还是https协议。(以上两项暂时只出现在company的PC)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)