是否可以在不打开浏览器的情况下通过 CLI 在 GitHub 上创建远程存储库?

发布于 2024-08-24 07:34:32 字数 584 浏览 12 评论 0 原文

我创建了一个新的本地 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 访问

I created a new local Git repository:

~$ mkdir projectname
~$ cd projectname
~$ git init
~$ touch file1
~$ git add file1
~$ git commit -m 'first commit'

Is there any git command to create a new remote repo and push my commit to GitHub from here? I know it's no big deal to just fire up a browser and head over to Create a New Repository, but if there is a way to achieve this from the CLI I would be happy.

I read a vast amount of articles but none that I found mention how to create a remote repo from the CLI using git commands. Tim Lucas's nice article Setting up a new remote git repository is the closest I found, but GitHub does not provide shell access.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(25

も让我眼熟你 2024-08-31 07:34:32

github API v3 的 CLI 命令(替换所有大写关键字):

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
# Remember replace USER with your username and REPO with your repository/application name!
git remote add origin [email protected]:USER/REPO.git
git push origin master

CLI commands for github API v3 (replace all CAPS keywords):

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
# Remember replace USER with your username and REPO with your repository/application name!
git remote add origin [email protected]:USER/REPO.git
git push origin master
酒中人 2024-08-31 07:34:32

您可以使用 GitHub API 通过命令行创建 GitHub 存储库。查看存储库 API。如果向下滚动大约三分之一,您将看到标题为 "Create"< 的部分/a> 解释了如何通过 API 创建存储库(正上方的部分也解释了如何使用 API 分叉存储库)。显然,您不能使用 git 来执行此操作,但您可以使用像 curl 这样的工具通过命令行来执行此操作。

在 API 之外,无法通过命令行在 GitHub 上创建存储库。正如您所指出的,GitHub 不允许 shell 访问等,因此除了 GitHub API 之外,创建存储库的唯一方法是通过 GitHub 的 Web 界面。

You can create a GitHub repo via the command line using the GitHub API. Check out the repository API. If you scroll down about a third of the way, you'll see a section entitled "Create" that explains how to create a repo via the API (right above that is a section that explains how to fork a repo with the API, too). Obviously you can't use git to do this, but you can do it via the command line with a tool like curl.

Outside of the API, there's no way to create a repo on GitHub via the command line. As you noted, GitHub doesn't allow shell access, etc., so aside from the GitHub API, the only way to create a repo is through GitHub's web interface.

月棠 2024-08-31 07:34:32

这可以通过三个命令来完成:(

curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
git remote add origin [email protected]:nyeates/projectname.git
git push origin master

针对 v3 Github API 进行更新)

这些命令的说明...

创建 github 存储库

curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
  • curl 是一个 unix 命令(上面也适用于 mac),用于检索 URL 并与 URL 交互。它通常已经安装。
  • “-u”是一个curl参数,指定用于服务器身份验证的用户名和密码。


  • “-d”是一个curl参数,允许您随请求发送POST数据
  • “name”是唯一的 POST所需数据;我还喜欢包含“描述”
  • 我发现最好用单引号引用所有 POST 数据 ' '

定义推送位置以

git remote add origin [email protected]:nyeates/projectname.git
  • 添加 github 上已连接(远程)存储库的位置和存在的定义
  • “origin”是默认名称git 使用的源从技术上来说
  • 的来源-
  • 并非来自 github,但现在 github 存储库将成为记录“[email protected]:nyeates" 是一个 ssh 连接,假设您已经与 github 设置了受信任的 ssh 密钥对。

将本地存储库推送到 github

git push origin master
  • 从 master 本地分支推送到原始远程(github)

This can be done with three commands:

curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
git remote add origin [email protected]:nyeates/projectname.git
git push origin master

(updated for v3 Github API)

Explanation of these commands...

Create github repo

curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
  • curl is a unix command (above works on mac too) that retrieves and interacts with URLs. It is commonly already installed.
  • "-u" is a curl parameter that specifies the user name and password to use for server authentication.
    • If you just give the user name (as shown in example above) curl will prompt for a password.
    • If you do not want to have to type in the password, see githubs api documentation on Authentication
  • "-d" is a curl parameter that allows you to send POST data with the request
  • "name" is the only POST data required; I like to also include "description"
  • I found that it was good to quote all POST data with single quotes ' '

Define where to push to

git remote add origin [email protected]:nyeates/projectname.git
  • add definition for location and existance of connected (remote) repo on github
  • "origin" is a default name used by git for where the source came from
  • technically didnt come from github, but now the github repo will be the source of record
  • "[email protected]:nyeates" is a ssh connection that assumes you have already setup a trusted ssh keypair with github.

Push local repo to github

git push origin master
  • push to the origin remote (github) from the master local branch
给不了的爱 2024-08-31 07:34:32

如果您安装 defunkt 优秀的 Hub 工具,那么这就变得像

hub create

一样简单,用作者的话来说,“hub 是 git 的命令行包装器,它使得你更擅长 GitHub。

If you install defunkt's excellent Hub tool, then this becomes as easy as

hub create

In the words of the author, "hub is a command-line wrapper for git that makes you better at GitHub."

会发光的星星闪亮亮i 2024-08-31 07:34:32

使用 Github 官方新的命令行界面

gh repo create

请参阅其他详细信息和选项安装说明< /a>.


例如,要完成您的 git 工作流程:

mkdir project
cd project
git init
touch file
git add file
git commit -m 'Initial commit'
gh repo create
git push -u origin master

With Github's official new command line interface:

gh repo create

See additional details and options and installation instructions.


For instance, to complete your git workflow:

mkdir project
cd project
git init
touch file
git add file
git commit -m 'Initial commit'
gh repo create
git push -u origin master
紫竹語嫣☆ 2024-08-31 07:34:32

简单步骤(使用 git + hub => GitHub):

  1. 安装 集线器 (GitHub)。

    • OS X:brew 安装中心
    • Go去 github.com/github/hub
    • 否则(也Go):

      git 克隆 https://github.com/github/hub.git && CD集线器&& ./脚本/构建
      
  2. 转到您的存储库或创建空的存储库: mkdir foo && cd foo && git init

  3. 运行:hub create,它会第一次询问您有关 GitHub 凭据的信息。

    用法:hub create [-p] [-d 描述] [-h 主页] [名称]

    示例:hub create -d Description -h example.com org_name/foo_repo

    <块引用>

    Hub 将提示输入 GitHub 用户名和密码第一次需要访问 API 时输入密码,并将其交换为 OAuth 令牌,并将其保存在 ~/.config/hub 中。

    要明确命名新存储库,请传入NAME
    可选择以 ORGANIZATION/NAME 形式在组织下创建
    您是以下组织的成员。

    使用-p,创建一个私有存储库,并使用
    -d-h 分别设置存储库的描述和主页 URL

    为避免出现提示,请使用 GITHUB_USERGITHUB_PASSWORD 环境变量。

  4. 然后照常提交和推送或检查 hub commit/hub Push

如需更多帮助,请运行:hub help

另请参阅:使用命令行导入 Git 存储库在 GitHub 上。

Simple steps (using git + hub => GitHub):

  1. 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
      
  2. Go to your repo or create empty one: mkdir foo && cd foo && git init.

  3. 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.

  4. Then commit and push as usual or check hub commit/hub push.

For more help, run: hub help.

See also: Importing a Git repository using the command line at GitHub.

起风了 2024-08-31 07:34:32

我认为有一个 官方 github gem 可以做到这一点。我会在学习过程中尝试添加更多信息,但我才刚刚发现这个宝石,所以我还不太了解。

更新:设置 API 密钥后,我可以通过 create 命令在 github 上创建一个新的存储库,但是我无法使用 create-from-local命令,该命令应该获取当前的本地存储库并在 github 上创建相应的远程版本。

$ gh create-from-local
=> error creating repository

如果有人对此有一些见解,我很想知道我做错了什么。已有一个已提交问题

更新:我最终确实让它发挥了作用。我不太确定如何重现该问题,但我刚刚从头开始(删除了 .git 文件夹)

git init
git add .emacs
git commit -a -m "adding emacs"

现在这一行将创建远程存储库,甚至推送到它,但不幸的是我认为我不能指定我想要的存储库的名称。我希望它在 github 上被称为“dotfiles”,但 gh gem 仅使用当前文件夹的名称,即“jason”,因为我位于我的主文件夹中。 (我添加了 一张票,询问所需的行为)

gh create-from-local

此命令另一方面,确实接受一个参数来指定远程存储库的名称,但它旨在从头开始一个新项目,即在调用此命令后,您将获得一个新的远程存储库,该存储库正在跟踪本地存储库相对于当前位置新创建的子文件夹,两者的名称都指定为参数。

gh create dotfiles

There is an official github gem which, I think, does this. I'll try to add more information as I learn, but I'm only just now discovering this gem, so I don't know much yet.

UPDATE: After setting my API key, I am able to create a new repo on github via the create command, however I am not able to use the create-from-local command, which is supposed to take the current local repo and make a corresponding remote out on github.

$ gh create-from-local
=> error creating repository

If anyone has some insight on this, I'd love to know what I'm doing wrong. There's already an issue filed.

UPDATE: I did eventually get this to work. I'm not exactly sure how to re-produce the issue, but I just started from scratch (deleted the .git folder)

git init
git add .emacs
git commit -a -m "adding emacs"

Now this line will create the remote repo and even push to it, but unfortunately I don't think I can specify the name of the repo I'd like. I wanted it to be called "dotfiles" out on github, but the gh gem just used the name of the current folder, which was "jason" since I was in my home folder. (I added a ticket asking for the desired behavior)

gh create-from-local

This command, on the other hand, does accept an argument to specify the name of the remote repo, but it's intended for starting a new project from scratch, i.e. after you call this command, you get a new remote repo that's tracking a local repo in a newly-created subfolder relative to your current position, both with the name specified as the argument.

gh create dotfiles
机场等船 2024-08-31 07:34:32

使用 Bash Shell 快速创建远程存储库

每次创建存储库时都键入完整的代码很麻烦

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
git remote add origin [email protected]:USER/REPO.git
git push origin master

更简单的方法是:

  1. 在目录中创建一个 shell 脚本,即 /home/USER_NAME/Desktop/my_scripts,名为 < code>githubscript.sh
  2. 修改以下代码并保存到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 [email protected]:YOUR_GITHUB_USER_NAME/$1.git;

NB 这里 $1 是调用时作为参数传递的存储库名称 脚本
在保存脚本之前更改 YOUR_GITHUB_USER_NAME

  1. 设置 script 文件所需的权限
chmod 755 githubscript.sh
  1. 将脚本目录包含在环境配置文件中。
nano ~/.profile;
export PATH="$PATH:$HOME/Desktop/my_scripts"
  1. 还设置一个别名来运行 githubscript.sh 文件。
nano ~/.bashrc;
alias githubrepo="bash githubscript.sh"
  1. 现在在终端中重新加载 .bashrc.profile 文件。
source ~/.bashrc ~/.profile;
  1. 现在创建一个新的存储库,即 demo
githubrepo demo;

To Quickly Create the Remote Repository by Using a Bash Shell

It is cumbersome to type the complete code every time a repository is to be created

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
git remote add origin [email protected]:USER/REPO.git
git push origin master

An easier approach is:

  1. create a shell script in a directory i.e. /home/USER_NAME/Desktop/my_scripts named githubscript.sh
  2. Modify and save the following code to the githubscript.sh file
#!bin/bash
curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"$1\"}";
git init;
git remote add origin [email protected]:YOUR_GITHUB_USER_NAME/$1.git;

N.B. Here $1 is the repository name that is passed as an argument when invoking the script
Change YOUR_GITHUB_USER_NAME before saving the script.

  1. Set required permissions to the script file
chmod 755 githubscript.sh
  1. Include the scripts directory in the environment configuration file.
nano ~/.profile;
export PATH="$PATH:$HOME/Desktop/my_scripts"
  1. Also set an alias to run the githubscript.sh file.
nano ~/.bashrc;
alias githubrepo="bash githubscript.sh"
  1. Now reload the .bashrc and .profile files in the terminal.
source ~/.bashrc ~/.profile;
  1. Now to create a new repository i.e. demo:
githubrepo demo;
小清晰的声音 2024-08-31 07:34:32

接受的答案到目前为止,得票最多的答案已经过时了。密码身份验证已已弃用,并将被删除世界标准时间 2020 年 11 月 13 日 16:00。

现在使用 GitHub API 的方式是通过个人访问令牌。

您需要(替换所有大写关键字):

  1. 通过网站创建个人访问令牌。是的,您必须使用浏览器,但以后的所有访问只需使用一次。安全地存储令牌。
  2. 通过 或 创建存储库
curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos  -d '{"name":"REPO"}'

,从一开始就将其设为私有:

curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}'
  1. 添加新源并推送到它:
git remote add origin [email protected]:USER/REPO.git
git push origin master

这样做的缺点是您必须每次都键入令牌,并且它会出现在您的 bash 历史记录中。

为了避免这种情况,您可以

  1. 将标头存储在文件中(例如,HEADER_FILE
Authorization: token MY_ACCESS_TOKEN
  1. 让curl从文件中读取
curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo
  1. 为了使事情更安全,您可以将访问权限设置为400并且用户root
chmod 400 HEADER_FILE
sudo chown root:root HEADER_FILE
  1. 现在需要 sudo 来访问头文件
sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo

Both the accepted answer and the most-voted answer so far are now outdated. Password authentication is deprecated and it will be removed on November 13, 2020 at 16:00 UTC.

The way to use GitHub API now is via personal access tokens.

You need to (replace ALL CAPS keywords):

  1. Create a personal access token via the website. Yes, you have to use the browser, but it is only once for all future accesses. Store the token securely.
  2. Create the repo via
curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos  -d '{"name":"REPO"}'

or, to make it private from the start:

curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}'
  1. Add the new origin and push to it:
git remote add origin [email protected]:USER/REPO.git
git push origin master

This has the disadvantage that you have to type the token each time, and that it appears in your bash history.

To avoid this, you can

  1. Store the header in a file (let's call it, say, HEADER_FILE)
Authorization: token MY_ACCESS_TOKEN
  1. Have curl read from the file
curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo
  1. To make things more secure, you could set access permissions to 400 and the user to root
chmod 400 HEADER_FILE
sudo chown root:root HEADER_FILE
  1. Now sudo will be needed to access the header file
sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo
予囚 2024-08-31 07:34:32

基于@Mechanical Snail 的另一个答案,除了不使用 python,我发现这太过分了。将其添加到您的 ~/.gitconfig 中:

[github]
    user = "your-name-here"
[alias]
    hub-new-repo = "!REPO=$(basename $PWD) GHUSER=$(git config --get github.user); curl -u $GHUSER https://api.github.com/user/repos -d {\\\"name\\\":\\\"$REPO\\\"} --fail; git remote add origin [email protected]:$GHUSER/$REPO.git; git push origin master"

Based on the other answer by @Mechanical Snail, except without the use of python, which I found to be wildly overkill. Add this to your ~/.gitconfig:

[github]
    user = "your-name-here"
[alias]
    hub-new-repo = "!REPO=$(basename $PWD) GHUSER=$(git config --get github.user); curl -u $GHUSER https://api.github.com/user/repos -d {\\\"name\\\":\\\"$REPO\\\"} --fail; git remote add origin [email protected]:$GHUSER/$REPO.git; git push origin master"
想你的星星会说话 2024-08-31 07:34:32

更新 20200714

Github 有一个新的官方 CLI

...gh 是一个新项目,可帮助我们探索具有根本不同设计的官方 GitHub CLI 工具的外观。虽然这两个工具都将 GitHub 带到终端,但 hub 充当 git 的代理,而 gh 是一个独立的工具。

与 hub 的核心区别在于它不会覆盖现有的 git 命令。因此,您不能像使用 hub 那样将 git 别名为 gh。

我们没有将其添加到集线器中,因为我们决定以不同于 git 包装器的方式设计 GitHub CLI,即作为它自己的命令。事实证明,维护一个作为 git 代理的可执行文件很难维护,而且我们也不希望受到必须始终保持 git 兼容性的限制。我们不想在从一开始就脆弱的范例上构建 GitHub CLI。

-- mislav(中心维护者)

原始答案< /strong>

您需要的是 hub。 Hub 是 git 的命令行包装器。它已使用别名与本机 git 集成。它尝试向 git 提供 github 操作,包括创建新存储库。

→  create a repo for a new project
$ git init
$ git add . && git commit -m "It begins."
$ git create -d "My new thing"
→  (creates a new project on GitHub with the name of current directory)
$ git push origin master

Update 20200714

Github has a new official CLI.

...gh is a new project that helps us explore what an official GitHub CLI tool can look like with a fundamentally different design. While both tools bring GitHub to the terminal, hub behaves as a proxy to git, and gh is a standalone tool.

The core difference from hub is that this does not wrap over existing git commands. So, you can not alias git to gh like you can with hub.

We didn't add it to hub because we decided to design GitHub CLI differently than being a git wrapper, i.e. as its own command. It turns out, maintaining an executable that's a proxy to git is hard to maintain, and also we didn't want to be limited by having to always keep git compatibility. We didn't want to build GitHub CLI on a paradigm that is brittle from the start.

-- mislav (Maintainer of hub)

Original answer

What you need is hub. Hub is a command-line wrapper for git. It has been made to integrate with native git using alias. It tries to provide github actions into git including creating new repository.

→  create a repo for a new project
$ git init
$ git add . && git commit -m "It begins."
$ git create -d "My new thing"
→  (creates a new project on GitHub with the name of current directory)
$ git push origin master
白云不回头 2024-08-31 07:34:32

最后,GitHub 正式宣布了其所有核心功能的新 CLI。

检查此处:https://cli.github.com/

通过 HomeBrew 安装:brew install gh 其他方式:https://github.com/cli/cli#installation

然后是

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'

现在您可以从终端创建存储库。

Finally, it happened GitHub has officially announced their new CLI for all the core features.

check here: https://cli.github.com/

To install via HomeBrew: brew install gh for other Ways : https://github.com/cli/cli#installation

then

gh repo create

Other available features.

$ 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'

So now you can create repo from your terminal.

浮萍、无处依 2024-08-31 07:34:32

不,您必须至少打开一次浏览器才能在 GitHub 上创建您的用户名,创建后,您就可以利用GitHub API 从命令行创建存储库,遵循以下命令:

curl -u 'github-username' https://api.github.com/user/repos -d '{"name":"repo-name"}'

例如:

curl -u 'arpitaggarwal' https://api.github.com/user/repos -d '{"name":"command-line-repo"}'

Nope, you have to open a browser atleast once to create your username on GitHub, once created, you can leverage GitHub API to create repositories from command line, following below command:

curl -u 'github-username' https://api.github.com/user/repos -d '{"name":"repo-name"}'

For example:

curl -u 'arpitaggarwal' https://api.github.com/user/repos -d '{"name":"command-line-repo"}'
静谧 2024-08-31 07:34:32

对于使用双因素身份验证的用户,您可以使用 bennedich 的解决方案,但只需为第一个命令添加 X-Github-OTP 标头即可。将 CODE 替换为您从双因素身份验证提供商处获取的代码。将 USER 和 REPO 替换为存储库的用户名和名称,就像在他的解决方案中一样。

curl -u 'USER' -H "X-GitHub-OTP: CODE" -d '{"name":"REPO"}' https://api.github.com/user/repos
git remote add origin [email protected]:USER/REPO.git
git push origin master

For users with two-factor authentication, you can use bennedich's solution, but you just need to add the X-Github-OTP header for the first command. Replace CODE with the code that you get from the two-factor authentication provider. Replace USER and REPO with the username and name of the repository, as you would in his solution.

curl -u 'USER' -H "X-GitHub-OTP: CODE" -d '{"name":"REPO"}' https://api.github.com/user/repos
git remote add origin [email protected]:USER/REPO.git
git push origin master
老娘不死你永远是小三 2024-08-31 07:34:32

我使用 GitHub 和 BitBucket 的 REST API 为此编写了一个名为 Gitter 的漂亮脚本:

https: //github.com/dderiso/gitter

BitBucket:

gitter -c -r b -l javascript -n node_app

GitHub:

gitter -c -r g -l javascript -n node_app
  • -c = 创建新仓库
  • -r< /code> = 存储库提供者(g = GitHub、b = BitBucket)
  • -n = 命名存储库
  • -l = (可选)设置存储库中应用程序的语言

I wrote a nifty script for this called Gitter using the REST APIs for GitHub and BitBucket:

https://github.com/dderiso/gitter

BitBucket:

gitter -c -r b -l javascript -n node_app

GitHub:

gitter -c -r g -l javascript -n node_app
  • -c = create new repo
  • -r = repo provider (g = GitHub, b = BitBucket)
  • -n = name the repo
  • -l = (optional) set the language of the app in the repo
三五鸿雁 2024-08-31 07:34:32

对于 Rubyists:

gem install githubrepo
githubrepo create *reponame*

根据提示输入用户名和密码

git remote add origin *ctrl v*
git push origin master

来源:Elikem Adadevoh

For Rubyists:

gem install githubrepo
githubrepo create *reponame*

enter username and pw as prompted

git remote add origin *ctrl v*
git push origin master

Source: Elikem Adadevoh

久光 2024-08-31 07:34:32

对于所有 Python 2.7.* 用户。目前版本 3 上有一个围绕 Github API 的 Python 包装器,称为 GitPython。只需使用 easy_install PyGithubpip install PyGithub 进行安装即可。

from github import Github
g = Github(your-email-addr, your-passwd)
repo = g.get_user().user.create_repo("your-new-repos-name")

# Make use of Repository object (repo)

Repository 对象文档位于此处

For all the Python 2.7.* users. There is a Python wrapper around the Github API that is currently on Version 3, called GitPython. Simply install using easy_install PyGithub or pip install PyGithub.

from github import Github
g = Github(your-email-addr, your-passwd)
repo = g.get_user().user.create_repo("your-new-repos-name")

# Make use of Repository object (repo)

The Repository object docs are here.

零崎曲识 2024-08-31 07:34:32

我创建了一个 Git 别名来执行此操作,基于 Bennedich 的回答。将以下内容添加到您的 ~/.gitconfig 中:

[github]
    user = "your_github_username"
[alias]
    ; Creates a new Github repo under the account specified by github.user.
    ; The remote repo name is taken from the local repo's directory name.
    ; Note: Referring to the current directory works because Git executes "!" shell commands in the repo root directory.
    hub-new-repo = "!python3 -c 'from subprocess import *; import os; from os.path import *; user = check_output([\"git\", \"config\", \"--get\", \"github.user\"]).decode(\"utf8\").strip(); repo = splitext(basename(os.getcwd()))[0]; check_call([\"curl\", \"-u\", user, \"https://api.github.com/user/repos\", \"-d\", \"{{\\\"name\\\": \\\"{0}\\\"}}\".format(repo), \"--fail\"]); check_call([\"git\", \"remote\", \"add\", \"origin\", \"[email protected]:{0}/{1}.git\".format(user, repo)]); check_call([\"git\", \"push\", \"origin\", \"master\"])'"

要使用它,请

$ git hub-new-repo

从本地存储库内的任何位置运行,并在出现提示时输入您的 Github 密码。

I've created a Git alias to do this, based on Bennedich's answer. Add the following to your ~/.gitconfig:

[github]
    user = "your_github_username"
[alias]
    ; Creates a new Github repo under the account specified by github.user.
    ; The remote repo name is taken from the local repo's directory name.
    ; Note: Referring to the current directory works because Git executes "!" shell commands in the repo root directory.
    hub-new-repo = "!python3 -c 'from subprocess import *; import os; from os.path import *; user = check_output([\"git\", \"config\", \"--get\", \"github.user\"]).decode(\"utf8\").strip(); repo = splitext(basename(os.getcwd()))[0]; check_call([\"curl\", \"-u\", user, \"https://api.github.com/user/repos\", \"-d\", \"{{\\\"name\\\": \\\"{0}\\\"}}\".format(repo), \"--fail\"]); check_call([\"git\", \"remote\", \"add\", \"origin\", \"[email protected]:{0}/{1}.git\".format(user, repo)]); check_call([\"git\", \"push\", \"origin\", \"master\"])'"

To use it, run

$ git hub-new-repo

from anywhere inside the local repository, and enter your Github password when prompted.

榕城若虚 2024-08-31 07:34:32

有关创建令牌的说明,请访问此处< /a> 这是您将输入的命令(截至本答案之日。(替换所有大写关键字):

curl -u 'YOUR_USERNAME' -d '{"scopes":["repo"],"note":"YOUR_NOTE"}' https://api.github.com/authorizations

输入密码后,您将看到以下包含您的令牌的内容。

{
  "app": {
    "name": "YOUR_NOTE (API)",
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  },
  "note_url": null,
  "note": "YOUR_NOTE",
  "scopes": [
    "repo"
  ],
  "created_at": "2012-10-04T14:17:20Z",
  "token": "xxxxx",
  "updated_at": "2012-10-04T14:17:20Z",
  "id": xxxxx,
  "url": "https://api.github.com/authorizations/697577"
}

您可以随时通过< a href="https://github.com/settings/applications" rel="nofollow noreferrer">此处

For directions on creating a token, go here This is the command you will type (as of the date of this answer. (replace all CAPS keywords):

curl -u 'YOUR_USERNAME' -d '{"scopes":["repo"],"note":"YOUR_NOTE"}' https://api.github.com/authorizations

Once you enter your password you will see the following which contains your token.

{
  "app": {
    "name": "YOUR_NOTE (API)",
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  },
  "note_url": null,
  "note": "YOUR_NOTE",
  "scopes": [
    "repo"
  ],
  "created_at": "2012-10-04T14:17:20Z",
  "token": "xxxxx",
  "updated_at": "2012-10-04T14:17:20Z",
  "id": xxxxx,
  "url": "https://api.github.com/authorizations/697577"
}

You can revoke your token anytime by going here

伪心 2024-08-31 07:34:32

免责声明:我是开源项目的作者

此功能由以下支持:https://github.com/chrissound/Human-Friendly-Commands 本质上是这个脚本:

#!/usr/bin/env bash

# Create a repo named by the current directory
# Accepts 1 STRING parameter for the repo description
# Depends on bin: jq
# Depends on env: GITHUB_USER, GITHUB_API_TOKEN
github_createRepo() {
  projName="$(basename "$PWD")"
  json=$(jq -n \
    --arg name "$projName" \
    --arg description "$1" \
    '{"name":$name, "description":$description}')

  curl -u "$GITHUB_USER":"$GITHUB_API_TOKEN" https://api.github.com/user/repos -d "$json"
  git init
  git remote add origin [email protected]:"$GITHUB_USER"/"$projName".git
  git push origin master
};

Disclamier: I'm the author of the open source project

This functionality is supported by: https://github.com/chrissound/Human-Friendly-Commands essentially it is this script:

#!/usr/bin/env bash

# Create a repo named by the current directory
# Accepts 1 STRING parameter for the repo description
# Depends on bin: jq
# Depends on env: GITHUB_USER, GITHUB_API_TOKEN
github_createRepo() {
  projName="$(basename "$PWD")"
  json=$(jq -n \
    --arg name "$projName" \
    --arg description "$1" \
    '{"name":$name, "description":$description}')

  curl -u "$GITHUB_USER":"$GITHUB_API_TOKEN" https://api.github.com/user/repos -d "$json"
  git init
  git remote add origin [email protected]:"$GITHUB_USER"/"$projName".git
  git push origin master
};
野の 2024-08-31 07:34:32

找到了我喜欢的解决方案: https://medium.com/@jakehasler/how-to-create-a-remote-git-repo-from-the-command-line-2d6857f49564

您首先需要创建一个 Github 个人访问令牌

打开在您最喜欢的文本编辑器中创建 ~/.bash_profile 或 ~/.bashrc 。在文件顶部附近添加以下行,其中导出的变量的其余部分是:

export GITHUB_API_TOKEN=

在下面的某个位置,通过其他 bash 函数,您可以可以粘贴类似以下内容:

function new-git() {
    curl -X POST https://api.github.com/user/repos -u <your-username>:$GITHUB_API_TOKEN -d '{"name":"'$1'"}'
}

现在,每当您创建新项目时,您都可以运行命令 $ new-git Awesome-repo 在您的 Github 用户帐户上创建新的公共远程存储库。

Found this solution which I liked: https://medium.com/@jakehasler/how-to-create-a-remote-git-repo-from-the-command-line-2d6857f49564

You first need to create a Github Personal Access Token

Open up your ~/.bash_profile or ~/.bashrc in your favorite text editor. Add the following line near the top of your file, where the rest of the export ‘ed variables are:

export GITHUB_API_TOKEN=<your-token-here>

Somewhere below, by your other bash functions, you can paste something similar to the following:

function new-git() {
    curl -X POST https://api.github.com/user/repos -u <your-username>:$GITHUB_API_TOKEN -d '{"name":"'$1'"}'
}

Now, whenever you’re creating a new project, you can run the command $ new-git awesome-repo to create a new public remote repository on your Github user account.

梦亿 2024-08-31 07:34:32

答案是:

> # Install gh
> brew install gh
> gh auth login # will take you to the browser but there are other options for non-interactive
> gh repo create repoName --private --source=. --remote=origin
> git remote -v # will show the local is connected
> git push -u origin main # will push to that connection

对于那些想要 bash 脚本的人,这是我的 MACOS zsh 脚本
在项目创建过程的末尾添加此内容,以将项目放入 github

它假设所有安装均已完成或包含在您的脚本中,并且您已获得授权:

# Prompt the user for confirmation to push to GitHub
read -p "Do you want to create repo and push to GitHub? (y/N): " choice
choice=${choice:-N} # Default to 'N' if Enter is pressed

if [[ "$choice" =~ ^[Yy]$ ]]; then

  #adds custom ssh keys as per your configuration
  ssh-add --apple-load-keychain

  read -p "Enter the GitHub repository name: " repo_name
  read -p "Enter the GitHub organization/user (or leave it empty for your user): " repo_user

  if [ -z "$repo_user" ]; then
    repo_user="$(git config user.username)" # Use your GitHub username from git config
  fi

  repo_url="[email protected]:$repo_user/$repo_name.git"

  # Add all changes to the repo
  git add -A
  git commit -m "First commit"

  #create remote Repo
  gh repo create $repo_name --private --source=. --remote=origin
  # Process the project out to a new main branch
  git push -u origin main
  # Create a new dev branch
  git checkout -b dev
  git push -u origin dev

  echo "GitHub push completed."
else
  echo "No action taken."
fi

如果有人想要 PowerShell 脚本,只需询问,我将在此处添加:.. 。
不会有太大不同

The answer is:

> # Install gh
> brew install gh
> gh auth login # will take you to the browser but there are other options for non-interactive
> gh repo create repoName --private --source=. --remote=origin
> git remote -v # will show the local is connected
> git push -u origin main # will push to that connection

For those who want a bash script here is my MACOS zsh script
add this at the end of your project create process to put your project into github

It assumes all installs are completed or included in your script and you are auth'd:

# Prompt the user for confirmation to push to GitHub
read -p "Do you want to create repo and push to GitHub? (y/N): " choice
choice=${choice:-N} # Default to 'N' if Enter is pressed

if [[ "$choice" =~ ^[Yy]$ ]]; then

  #adds custom ssh keys as per your configuration
  ssh-add --apple-load-keychain

  read -p "Enter the GitHub repository name: " repo_name
  read -p "Enter the GitHub organization/user (or leave it empty for your user): " repo_user

  if [ -z "$repo_user" ]; then
    repo_user="$(git config user.username)" # Use your GitHub username from git config
  fi

  repo_url="[email protected]:$repo_user/$repo_name.git"

  # Add all changes to the repo
  git add -A
  git commit -m "First commit"

  #create remote Repo
  gh repo create $repo_name --private --source=. --remote=origin
  # Process the project out to a new main branch
  git push -u origin main
  # Create a new dev branch
  git checkout -b dev
  git push -u origin dev

  echo "GitHub push completed."
else
  echo "No action taken."
fi

If anyone wants a PowerShell script just ask and I will add here:...
it wont be much different

冷血 2024-08-31 07:34:32

出于代表原因,我无法将其添加为评论(最好与 bennedich's answer),但对于 Windows 命令行,正确的语法如下:

curl -u YOUR_USERNAME https:// api.github.com/user/repos -d "{\"name\":\"YOUR_REPO_NAME\"}"

基本形式相同,但必须使用双引号 (") 而不是单引号,并用反斜杠转义 POST 参数中发送的双引号(在 -d 标志之后)。我还删除了用户名周围的单引号,但如果您的用户名有空格(可能?),则可能需要双引号。

For rep reasons, I can't add this as a comment (where it would better go with bennedich's answer), but for Windows command line, here is the correct syntax:

curl -u YOUR_USERNAME https://api.github.com/user/repos -d "{\"name\":\"YOUR_REPO_NAME\"}"

It's the same basic form, but you have to use double quotes (") instead of single, and escape the double quotes sent in the POST parameters (after the -d flag) with backslashes. I also removed the single quotes around my username, but if your username had a space (possible?) it would probably need double quotes.

九歌凝 2024-08-31 07:34:32

这是我最初的 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

here is my initial git commands (possibly, this action takes place in 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
尛丟丟 2024-08-31 07:34:32

在命令行上创建新存储库

echo "# <RepositoryName>" >> README.md

git init

git add README.md

git commit -m "first commit"

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master

从命令行推送现有存储库

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master

create a new repository on the command line

echo "# <RepositoryName>" >> README.md

git init

git add README.md

git commit -m "first commit"

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master

push an existing repository from the command line

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文