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

397

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

25个回答

2
我发现了这个我喜欢的解决方案:https://medium.com/@jakehasler/how-to-create-a-remote-git-repo-from-the-command-line-2d6857f49564 首先需要创建一个Github个人访问令牌 在您最喜欢的文本编辑器中打开~/.bash_profile或~/.bashrc文件。将以下行添加到文件顶部,即其他导出变量所在的位置: export GITHUB_API_TOKEN=<your-token-here> 在下面某个地方,您可以粘贴类似于以下内容的东西:
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用户账户上创建一个新的公共远程仓库。


0

由于声誉原因,我无法将此添加为评论(它可能更适合与bennedich's answer一起),但对于Windows命令行,以下是正确的语法:

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

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


对于Windows用户来说,这是一个好消息。不,用户名不能包含空格(在https://github.com/的注册表单中指出:“用户名只能包含字母数字字符或单个连字符,并且不能以连字符开头或结尾”)。因此,不需要对用户名进行双引号括起来。 - mklement0
Github PowerShell在Windows上不接受使用curl的-u参数 :( - JuliandotNut

0

答案是:

> # 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="git@github.com:$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脚本,只要问一下,我会在这里添加: 它不会有太大的区别。

-1

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

1
在这种情况下,Hello-World存储库应该在GitHub上可用,但这并没有解决问题。 - JuliandotNut

-6

在命令行上创建一个新的代码库

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

1
这个不起作用,它没有创建远程仓库。 - Matthew Read

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接