以下操作基于macOS,其他系统安装请参阅官方文档cli/cli: GitHub’s official command line tool。
1. 安装github cli
brew install gh
2. 登录
以下操作步骤基本上就是上下方向键进行选择,然后回车。Token需要访问这里手动创建并粘贴。Token的权限应该至少包括repo
, read:org
, admin:public_key
.
# 输入登录命令
gh auth login
# 选择登录网站GitHub.com
? What account do you want to log into? -> GitHub.com
# 选择协议,我选SSH
? What is your preferred protocol for Git operations? -> SSH
# 选择并上传上传SSH public key
? Upload your SSH public key to your GitHub account? -> /Users/simonliu/.ssh/id_rsa.pub
# 设置key的title
? Title for your SSH key: -> GitHub CLI
# 选择认证方式,我这里选token
? How would you like to authenticate GitHub CLI? ->Paste an authentication token
Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
The minimum required scopes are 'repo', 'read:org', 'admin:public_key'.
? Paste your authentication token: ****************************************
- gh config set -h github.com git_protocol ssh
✓ Configured git protocol
✓ Authentication complete.
3. 在本地仓库文件夹创建远程仓库并推送到服务器
基本上就是输入 gh repo create
然后一路根据提示操作即可。
# 3.1 创建远程 repository
gh repo create
# 3.2 输入本地仓库路径,默认是当前路径 .
? Path to local repository -> .
# 3.3 选择行为:推送本地仓库至github
? What would you like to do? -> Push an existing local repository to GitHub
# 3.4 设置仓库名称,注意不要使用中文,否则中文字符会被替换为英文破折号
? Repository name -> epub_convert_tool
# 3.5 输入描述
? Description -> epub转换工具
# 3.5 选择可见度
? Visibility -> Private
✓ Created repository simonliu009/epub_convert_tool on GitHub
# 3.6 为本地仓库增加remote
? Add a remote? -> Yes
# 3.7 设置remote名称
? What should the new remote be called? -> origin
✓ Added remote git@github.com:simonliu009/epub_convert_tool.git
# 3.8 是否将当前分支的提交推送到origin,选择Yes
? Would you like to push commits from the current branch to "origin"? ->Yes
Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 3.61 KiB | 3.61 MiB/s, done.
Total 5 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To github.com:simonliu009/epub_convert_tool.git
* [new branch] HEAD -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
✓ Pushed commits to git@github.com:simonliu009/epub_convert_tool.git
4. 其他gh命令
gh repo --help
Work with GitHub repositories.
USAGE
gh repo <command> [flags]
GENERAL COMMANDS
create: Create a new repository
list: List repositories owned by user or organization
TARGETED COMMANDS
archive: Archive a repository
clone: Clone a repository locally
delete: Delete a repository
deploy-key: Manage deploy keys in a repository
edit: Edit repository settings
fork: Create a fork of a repository
rename: Rename a repository
set-default: Configure default repository for this directory
sync: Sync a repository
unarchive: Unarchive a repository
view: View a repository
INHERITED FLAGS
--help Show help for command
ARGUMENTS
A repository can be supplied as an argument in any of the following formats:
- "OWNER/REPO"
- by URL, e.g. "https://github.com/OWNER/REPO"
EXAMPLES
$ gh repo create
$ gh repo clone cli/cli
$ gh repo view --web
LEARN MORE
Use 'gh <command> <subcommand> --help' for more information about a command.
Read the manual at https://cli.github.com/manual