0


获取Git权限的三种方式

获取Git权限的三种方式

写在最前

  1. 本文以为Azure Devlop的平台为例子操作三种获取Git权限的办法
  2. Github、Gitlab、Gitee、Gitcode等平台操作类似
  3. 主要是便于理解获取Git权限的几种采用方式
  4. Git工具安装时,如果没有特别勾选不安装SSH,默认是Git安装好之后是会携带了SSH工具
  5. 本文以git clone指令拉取代码为例子,当没有权限获取项目时,会出现类似错误:
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest $ git clone
https://[email protected]/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://[email protected]/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
[email protected]:v3/XXXX8009/GitTestProject/GitTestProject
Cloning into 'GitTestProject'... Load key
"/c/Users/R7000/.ssh/id_rsa": invalid format [email protected]'s
password: Permission denied, please try again. [email protected]'s
password: Permission denied, please try again. [email protected]'s
password: [email protected]: 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 获取当前项目的用户和密码

    1. 选择项目的方式为HTTPS:在这里插入图片描述在这里插入图片描述
    1. 生成用户名和密码在这里插入图片描述

1.2 通过临时用户获取Git权限

    1. 由于Azure比较特殊会提示登录VS,这边取消了登录VS会提示fatal: User canceled authentication.但是用户名和密码是可以使用的
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest
$ git clone https://[email protected]/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
    1. 这里打开项目里面的.git文件夹查看config文件
[core]
    repositoryformatversion =0
    filemode =false
    bare =false
    logallrefupdates =true
    symlinks =false
    ignorecase =true[remote "origin"]
    url = https://[email protected]/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

    1. 点击Personal access token按钮 创建token在这里插入图片描述
    1. 跳转之后点击New token

在这里插入图片描述

    1. 输入名字和时间以及权限之后创建token,千万保存并记录好token秘钥那串字符数字

在这里插入图片描述

    1. 有意思的是之前自动生成用户名和密码 实际上也是携带了一个token,为什么这么猜测呢,因为之后查看用token获取权限后的config文件可以看出在这里插入图片描述

2.2 HTTPS配合token获取Git权限

    1. 同样拉去代码,选择HTTPS的方式,这里不需要创建用户名和密码

在这里插入图片描述

    1. 打开git bash,手动在git 连接前面 @开头的信息去掉换成token信息,即原来的git连接为https://[email protected]/XXXX8009/GitTestProject/_git/GitTestProject现在的为https://[email protected]/XXXX8009/GitTestProject/_git/GitTestProject
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest
$ git clone https://[email protected]/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.
    1. 此时的项目下的git config信息中的URL是携带token的,可以猜测之前随机生成的用户名和密码时,也是携带了token信息
[core]
    repositoryformatversion =0
    filemode =false
    bare =false
    logallrefupdates =true
    symlinks =false
    ignorecase =true[remote "origin"]
    url = https://[email protected]/XXXX8009/GitTestProject/_git/GitTestProject
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
    remote = origin
    merge = refs/heads/main

3. SSH访问Git

3.1 生成SSH公钥和私钥

    1. 打开cmd或者git bash,输入ssh-keygen -t rsa,为了方便,提示选项可以enter键跳过,最后生成文件在C盘用户目录的.ssh文件夹下,如这里的/c/Users/R7000/.ssh/,其中.pub为公钥
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter fileinwhich 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]-----+

在这里插入图片描述

    1. 复制公钥信息,到代码仓库的SSH中

在这里插入图片描述

    1. 点击添加SSH KEY:在这里插入图片描述
    1. 将公钥信息复制进去,其中Name自定义在这里插入图片描述在这里插入图片描述

在这里插入图片描述

3.2 使用SSH获取Git权限

    1. 添加成功SSH信息之后,利用SSH连接就可以直接获取Git权限了
R7000@DESKTOP-30AFPHP MINGW64 /d/Workspace/Py/gitest
$ git clone [email protected]: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 github ssh

本文转载自: https://blog.csdn.net/qq_42727752/article/details/128607807
版权归原作者 爬楼的猪 所有, 如有侵权,请联系我们删除。

“获取Git权限的三种方式”的评论:

还没有评论