写在最前
- 本文以为Azure Devlop的平台为例子操作三种获取Git权限的办法
- Github、Gitlab、Gitee、Gitcode等平台操作类似
- 主要是便于理解获取Git权限的几种采用方式
- Git工具安装时,如果没有特别勾选不安装SSH,默认是Git安装好之后是会携带了SSH工具
- 本文以git clone指令拉取代码为例子,当没有权限获取项目时,会出现类似错误:
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest $ git clone https://XXXX8009@dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject Cloning into 'GitTestProject'... fatal: User canceled authentication. error: unable to read askpass response from 'D:/Git/mingw64/bin/git-askpass.exe' Password for 'https://XXXX8009@dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject': fatal: Authentication failed for 'https://dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject/' R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest $ git clone git@ssh.dev.azure.com:v3/XXXX8009/GitTestProject/GitTestProject Cloning into 'GitTestProject'... Load key "/c/Users/R7000/.ssh/id_rsa": invalid format git@ssh.dev.azure.com's password: Permission denied, please try again. git@ssh.dev.azure.com's password: Permission denied, please try again. git@ssh.dev.azure.com's password: git@ssh.dev.azure.com: Permission denied (password,publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
1. HTTPS配合用户名和密码访问Git
1.1 获取当前项目的用户和密码
-
- 选择项目的方式为HTTPS:
- 选择项目的方式为HTTPS:
-
- 生成用户名和密码
- 生成用户名和密码
1.2 通过临时用户获取Git权限
-
- 由于Azure比较特殊会提示登录VS,这边取消了登录VS会提示
fatal: User canceled authentication.
但是用户名和密码是可以使用的
- 由于Azure比较特殊会提示登录VS,这边取消了登录VS会提示
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest
$ git clone https://XXXX8009@dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject
Cloning into 'GitTestProject'...
fatal: User canceled authentication.
remote: Azure Repos
remote: Found 22 objects to send. (2 ms)
Unpacking objects: 100% (22/22), 5.62 KiB | 122.00 KiB/s, done.
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest/GitTestProject (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
-
- 这里打开项目里面的
.git
文件夹查看config
文件
- 这里打开项目里面的
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://XXXX8009@dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
2. HTTPS配合token访问Git
2.1 创建token
-
- 点击
Personal access token
按钮 创建token
- 点击
-
- 跳转之后点击
New token
- 跳转之后点击
-
- 输入名字和时间以及权限之后创建token,千万保存并记录好token秘钥那串字符数字
-
- 有意思的是之前自动生成用户名和密码 实际上也是携带了一个token,为什么这么猜测呢,因为之后查看用token获取权限后的config文件可以看出
- 有意思的是之前自动生成用户名和密码 实际上也是携带了一个token,为什么这么猜测呢,因为之后查看用token获取权限后的config文件可以看出
2.2 HTTPS配合token获取Git权限
-
- 同样拉去代码,选择HTTPS的方式,这里不需要创建用户名和密码
-
- 打开git bash,手动在git 连接前面 @开头的信息去掉换成token信息,即原来的git连接为
https://XXXX8009@dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject
现在的为https://XXXXXXXXXXXXXXXX7pgv6xxfeigim7ygw3lydz4xbpo7vyy2q@dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject
- 打开git bash,手动在git 连接前面 @开头的信息去掉换成token信息,即原来的git连接为
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest
$ git clone https://XXXXXXXXXXXXXXXX7pgv6xxfeigim7ygw3lydz4xbpo7vyy2q@dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject
Cloning into 'GitTestProject'...
remote: Azure Repos
remote: Found 22 objects to send. (19 ms)
Unpacking objects: 100% (22/22), 5.62 KiB | 85.00 KiB/s, done.
-
- 此时的项目下的git config信息中的URL是携带token的,可以猜测之前随机生成的用户名和密码时,也是携带了token信息
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = https://XXXXXXXXXXXXXXXX7pgv6xxfeigim7ygw3lydz4xbpo7vyy2q@dev.azure.com/XXXX8009/GitTestProject/_git/GitTestProject
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
3. SSH访问Git
3.1 生成SSH公钥和私钥
-
- 打开cmd或者git bash,输入
ssh-keygen -t rsa
,为了方便,提示选项可以enter键跳过,最后生成文件在C盘用户目录的.ssh文件夹下
,如这里的/c/Users/R7000/.ssh/
,其中.pub
为公钥
- 打开cmd或者git bash,输入
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/R7000/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/R7000/.ssh/id_rsa
Your public key has been saved in /c/Users/R7000/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:QevS8lBgx8OihC4ipYP+4fmg1vBuH9DrXTuzqRHmq2A R7000@DESKTOP-30AFPHP
The key's randomart image is:
+---[RSA 3072]----+
| . ooo |
| o ...++. |
|.+ . . .+. |
|B . o + . |
|+o . .+oS |
| o .. +=. |
| *Eoo o.. |
| .oB+ o +oo |
|..o.o=.+.++ |
+----[SHA256]-----+
-
- 复制公钥信息,到代码仓库的SSH中
文章来源:https://www.toymoban.com/news/detail-795589.html
-
- 点击添加SSH KEY:
- 点击添加SSH KEY:
-
- 将公钥信息复制进去,其中Name自定义
- 将公钥信息复制进去,其中Name自定义
文章来源地址https://www.toymoban.com/news/detail-795589.html
3.2 使用SSH获取Git权限
-
- 添加成功SSH信息之后,利用SSH连接就可以直接获取Git权限了
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest
$ git clone git@ssh.dev.azure.com:v3/XXXX8009/GitTestProject/GitTestProject
Cloning into 'GitTestProject'...
remote: Azure Repos
remote: Found 22 objects to send. (6 ms)
Receiving objects: 100% (22/22), 5.64 KiB | 1.41 MiB/s, done.
Resolving deltas: 100% (1/1), done.
到了这里,关于获取Git权限的三种方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!