Git命令与Git-cli

1. 安装Git-cli

winget install --id GitHub.cli

注:以上命令用于在Windows平台CMD执行快捷安装,若需安装其他平台的Git-cli及详细内容,win安装文件一般下载amd64 installer的版本,请访问:https://github.com/cli/cli

 

 

2. 使用Git-cli进行身份验证

执行下列命令会进行身份验证并将本地gh工具与远程GitHub账户进行关联

gh auth login

注:执行该命令后会有以下提示

选择平台,这里我选GitHub,并回车
Where do you use GitHub?  [Use arrows to move, type to filter]

>GitHub.com

  Other

选择Git操作的协议,这里我选HTTPS,并回车

What is your preferred protocol for Git operations on this host?  [Use arrows to move, type to filter]

>HTTPS

  SSH

输入Y或n,确认是否使用GitHub凭证对Git进行身份验证,这里输入Y并回车

Authenticate Git with your GitHub credentials? (Y/n)

选择认证方式,使用何种方式进行认证,使用浏览器进行认证?还是使用Token进行认证?这里我选Login with a web browser,并回车

How would you like to authenticate GitHub CLI?  [Use arrows to move, type to filter]

>Login with a web browser

  Paste an authentication token

回车后会提示两条信息:
First copy your one-time code: B543-72FB

Press Enter to open https://github.com/login/device in your browser...

此时按回车,并按照浏览器的提示点击continue,然后将上述的验证码输入浏览器

验证码输入完成后,等待几秒,然后点击Authorize github即可完成验证(若开启了手机GitHub验证,需在手机进行授权)

操作完成后,返回CMD或Git-Bash观察,等待几秒会提示下列信息表示身份认证完成:

Authentication complete.
gh config set -h github.com git_protocol https
Configured git protocol
Logged in as xxxx

 

 

3. 使用Git-cli在GitHub上直接创建远程仓库并初始化

先在电脑上任意位置创建一个目录,这里举例为D:\Git_Repo
此时,我先进入Git_Repo目录,打开CMD执行以下命令(习惯Linux命令操作的可使用Git-Bash操作)

gh repo create myproject --private -l MIT -g "C++" --add-readme

注:执行后会输出远程仓库名如:https://github.com/xxxxxxx/myproject,此时GitHub中的远程仓库已经完成创建并初始化完成

myproject    远程仓库名

--private      设置为私有仓库

--add-readme   设置README文件

-l          设置License

-g         设置.gitignore

 

 

4. 使用Git-cli克隆GitHub中初始化完成的远程仓库

gh repo clone myproject

注:此时查看当前目录,远程仓库已经克隆到本地了

 

 

5. 进入克隆下来的仓库并进行相关操作

cd myproject
touch demo.cpp
echo "#include<iostream>" > demo.cpp
git add .
git commit -m "add a demo file"
git push origin main

 至此基本的安装、认证和仓库创建操作已经结束,第5步以后的操作,按需操作即可

 

 

6. 查看本地仓库默认主分支

若未配置本地仓库默认主分支,而又先在本地创建仓库,再使用git init命令初始化仓库时,此时的本地仓库默认主分支为master

git config --global init.defaultBranch

注:若执行后无输出,则表示未配置,此时本地仓库的默认主分支可能为master,而不是main,与GitHub远程仓库的默认主分支不同步。因2020年GitHub响应社区呼吁宣布将默认分支名改为main

 

 

7. 配置本地仓库默认主分支为main

git config --global init.defaultBranch main

注:此时再执行git init初始化本地仓库,然后将本地创建的仓库推送到远程仓库时,本地仓库的默认主分支与远程仓库的默认主分支一致为main

 

 

8. 使用Git-cli删除已创建的远程仓库

gh repo delete myproject

注:在删除仓库时,会提示下列信息,此时等待你的输入,确认要删除此仓库,在冒号后输入your-username/myproject以确认删除,然后回车即可:
输入前提示:Type your-username/myproject to confirm deletion: 

输入后提示:Type your-username/myproject to confirm deletion: your-username/myproject

 

执行删除仓库的命令时,可能会无法删除成功,并提示:

HTTP 403: Must have admin rights to Repository. (https://api.github.com/repos/Rinato-C417/myproject)
This API operation needs the "delete_repo" scope. To request it, run: gh auth refresh -h github.com -s delete_repo

提示的信息大意是:该删除操作需要delete_repo权限


此时需要执行以下命令来获取该权限:

gh auth refresh -h github.com -s delete_repo

作用是更新认证信息并请求删除仓库的delete_repo的权限,执行后需经过GitHub授权后才能继续操作

授权完成后,再次执行第8步的gh repo delete myproject删除仓库操作时,仓库此时即可删除成功

 

 

9. 使用Git-cli列出所有仓库

gh repo list

 

posted @ 2025-04-11 11:49  Rinato  阅读(91)  评论(0)    收藏  举报