我创建了一个新的本地Git存储库:
~$ mkdir projectname
~$ cd projectname
~$ git init
~$ touch file1
~$ git add file1
~$ git commit -m 'first commit'
有没有任何git命令来创建一个新的远程回购,并从这里将我的提交推到GitHub ?我知道打开浏览器去创建一个新的存储库并不是什么大问题,但是如果有一种方法可以从CLI实现这一点,我会很高兴。
我读了大量的文章,但没有一篇提到如何使用git命令从CLI创建远程回购。Tim Lucas的一篇不错的文章“设置一个新的远程git存储库”是我找到的最接近的文章,但是GitHub不提供shell访问。
如何使用Bash Shell快速创建远程存储库
每次创建存储库时输入完整的代码是很麻烦的
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
git远程添加源git@github.com:USER/REPO.git
Git push origin master
一个更简单的方法是:
在/home/USER_NAME/Desktop/my_scripts目录下创建一个shell脚本githubscript.sh
修改并保存以下代码到githubscript.sh文件中
#!bin/bash
curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"$1\"}";
git init;
git remote add origin git@github.com:YOUR_GITHUB_USER_NAME/$1.git;
注意:这里$1是在调用脚本时作为参数传递的存储库名称
在保存脚本之前更改YOUR_GITHUB_USER_NAME。
为脚本文件设置所需的权限
Chmod 755 githubscript.sh
在环境配置文件中包含脚本目录。
纳米~ / . profile;
导出路径= " $路径:$ HOME /桌面/ my_scripts”
还要设置一个别名来运行githubscript.sh文件。
纳米~ / . bashrc;
别名githubrepo="bash githubscript.sh"
现在在终端中重新加载.bashrc和.profile文件。
~ /来源。bashrc ~ / . profile (;
现在创建一个新的存储库,即demo:
githubrepo演示;
简单步骤(使用git + hub => GitHub):
Install Hub (GitHub).
OS X: brew install hub
having Go: go get github.com/github/hub
otherwise (having Go as well):
git clone https://github.com/github/hub.git && cd hub && ./script/build
Go to your repo or create empty one: mkdir foo && cd foo && git init.
Run: hub create, it'll ask you about GitHub credentials for the first time.
Usage: hub create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]
Example: hub create -d Description -h example.com org_name/foo_repo
Hub will prompt for GitHub username & password the first time it needs to access the API and exchange it for an OAuth token, which it saves in ~/.config/hub.
To explicitly name the new repository, pass in NAME,
optionally in ORGANIZATION/NAME form to create under an organization
you're a member of.
With -p, create a private repository, and with
-d and -h set the repository's description and homepage URL, respectively.
To avoid being prompted, use GITHUB_USER and GITHUB_PASSWORD environment variables.
Then commit and push as usual or check hub commit/hub push.
有关更多帮助,请运行:hub help。
参见:在GitHub中使用命令行导入Git存储库。
如何使用Bash Shell快速创建远程存储库
每次创建存储库时输入完整的代码是很麻烦的
curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
git远程添加源git@github.com:USER/REPO.git
Git push origin master
一个更简单的方法是:
在/home/USER_NAME/Desktop/my_scripts目录下创建一个shell脚本githubscript.sh
修改并保存以下代码到githubscript.sh文件中
#!bin/bash
curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"$1\"}";
git init;
git remote add origin git@github.com:YOUR_GITHUB_USER_NAME/$1.git;
注意:这里$1是在调用脚本时作为参数传递的存储库名称
在保存脚本之前更改YOUR_GITHUB_USER_NAME。
为脚本文件设置所需的权限
Chmod 755 githubscript.sh
在环境配置文件中包含脚本目录。
纳米~ / . profile;
导出路径= " $路径:$ HOME /桌面/ my_scripts”
还要设置一个别名来运行githubscript.sh文件。
纳米~ / . bashrc;
别名githubrepo="bash githubscript.sh"
现在在终端中重新加载.bashrc和.profile文件。
~ /来源。bashrc ~ / . profile (;
现在创建一个新的存储库,即demo:
githubrepo演示;
最后,GitHub正式宣布了他们所有核心功能的新CLI。
点击这里查看:https://cli.github.com/
通过HomeBrew安装:brew install gh用于其他方式:https://github.com/cli/cli#installation
then
gh repo create
其他可用的特性。
$ gh --help
Work seamlessly with GitHub from the command line.
USAGE
gh <command> <subcommand> [flags]
CORE COMMANDS
gist: Create gists
issue: Manage issues
pr: Manage pull requests
release: Manage GitHub releases
repo: Create, clone, fork, and view repositories
ADDITIONAL COMMANDS
alias: Create command shortcuts
api: Make an authenticated GitHub API request
auth: Login, logout, and refresh your authentication
completion: Generate shell completion scripts
config: Manage configuration for gh
help: Help about any command
FLAGS
--help Show help for command
--version Show gh version
EXAMPLES
$ gh issue create
$ gh repo clone cli/cli
$ gh pr checkout 321
ENVIRONMENT VARIABLES
See 'gh help environment' for the list of supported environment variables.
LEARN MORE
Use 'gh <command> <subcommand> --help' for more information about a command.
Read the manual at https://cli.github.com/manual
FEEDBACK
Open an issue using 'gh issue create -R cli/cli'
现在你可以在终端上创建repo了。
这是我的初始git命令(可能,这个操作发生在C:/Documents and Settings/your_username/):
mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World" in your user directory
cd ~/Hello-World
# Changes the current working directory to your newly created directory
touch blabla.html
# create a file, named blabla.html
git init
# Sets up the necessary Git files
git add blabla.html
# Stages your blabla.html file, adding it to the list of files to be committed
git commit -m 'first committttt'
# Commits your files, adding the message
git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repository
git push -u origin master
# Sends your commits in the "master" branch to GitHub