VSCode SSH 配置 Git + 本地直接创建远程仓库

配置Git

下载并安装Git

Git下载地址
在这里插入图片描述
在这里插入图片描述

我这里之前安装过,可能不一样,如果没什么特殊要求,一路点下去就完事儿。

查看安装状态

Win+R,输入cmd

Win+R,输入cmd
输入命令git,如果内容如下,安装成功…
在这里插入图片描述

配置Git路径

新增git配置和默认终端为bash

git.path :Git安装位置bin目录下的 git.exe
terminal.integrated.shell.windows: Git安装位置bin目录下的 bash.exe

方式1:Ctrl+Shift+P 搜索Setting,选择Open Settings

在这里插入图片描述
输入一下配置

"git.path": "C:/Program Files/Git/bin/git.exe",
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"

方式2:打开File(文件) - Preferences(首选项) - Settings(设置)
搜索 git.path 然后点击 Edit
在这里插入图片描述
搜索terminal.integrated.shell.windows 然后点击 Edit
在这里插入图片描述
配置如图:
在这里插入图片描述

特别要注意 " \ " 转义

配置git用户名和邮箱

Ctrl + ~ 打开终端,输入全局配置命令

git config --global user.name "username" //( "username" 账户名)
git config --global user.email "username@email.com"  //("username@email.com" 邮箱)

验证配置

git config --global --list

这里也可以直接打开所在文件查看 C:/Users/YourName/.gitconfig

配置SSH

生成SSH

 ssh-keygen -t rsa -C "youremail"

默认位置C:\Users\YourName\.ssh
在这里插入图片描述

在github中配置SSH

在这里插入图片描述

验证SSH

ssh -T git@github.com 

在这里插入图片描述

出现 The authenticity of host ‘github.com (13.250.xx.xx)’ can’t be established.是因为本地ssh没有 known_hosts 文件,连接后会自动生成

本地创建远程仓库

申请token

此步骤需要在github上申请token
勾选repo和gist选项
生成token(只第一次可见,后面需要Update token)
在这里插入图片描述
在这里插入图片描述

创建远程仓库

$username(用户名) $repo_name(仓库名) $token(token)

命令行方式

创建
 $curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}'
初始化
git init
关联远程仓库
git remote add origin git@github.com:$username/$repo_name.git
推送
git push -u origin master //第一次将本地仓库推送到远程仓库
查看
git remote -v

bash函数方式

git配置token
git config --global user.token "4d7f8fxxxxxxx5c9d3xxxx"

在.bashrc或者.zshrc文件中定义函数

简版

Windows : C:\Program Files\Git\etc\bash.bashrc
Linux: ~/.bashrc

# create remote repository
git-create() 
{
	username=`git config user.name`
	token=`git config user.token`
    if [ $1 ]
    then
        repo_name=$1
    else
        repo_name=`basename $(pwd)`
        echo "notice: set the name for remote repository as ${repo_name}"
    fi 
	
	curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}'
	echo "# ${repo_name}" >> README.md
    git init
    git add README.md
    git commit -m "first commit"
	git remote add origin git@github.com:$username/$repo_name.git
	git push -u origin master 
	
}
健壮版

源代码地址

git-create() {
  repo_name=$1

  dir_name=`basename $(pwd)`
  invalid_credentials=0

  if [ "$repo_name" = "" ]; then
    echo "  Repo name (hit enter to use '$dir_name')?"
    read repo_name
  fi

  if [ "$repo_name" = "" ]; then
    repo_name=$dir_name
  fi

  username=`git config github.user`
  if [ "$username" = "" ]; then
    echo "  Could not find username, run 'git config --global github.user <username>'"
    invalid_credentials=1
  fi

  token=`git config github.token`
  if [ "$token" = "" ]; then
    echo "  Could not find token, run 'git config --global github.token <token>'"
    invalid_credentials=1
  fi

  type=`git config github.tokentype`
  if [ "$type" = "ssh" ]; then
    conn_string="git@github.com:$username/$repo_name.git"
  elif [ "$type" = "http" ]; then
    conn_string="https://github.com/$username/$repo_name.git"
  else
    echo "  Either token type was not enterred correctly or is empty.\n  It must be one of 'ssh' or 'http'.\n  Run git config --global github.tokentype <ssh|http>"
    invalid_credentials=1
  fi

  if [ "$invalid_credentials" -eq "1" ]; then
    return 1
  fi

  echo -n "  Creating Github repository '$repo_name' ..."
  curl -u "$username:$token" https://api.github.com/user/repos -d '{"name":"'$repo_name'"}' > /dev/null 2>&1
  echo " done."

  echo -n "  Pushing local code to remote ..."
  git remote add origin $conn_string > /dev/null 2>&1
  git push -u origin master > /dev/null 2>&1
  echo " done."
}
验证

在这里插入图片描述

在这里插入图片描述

遇到的问题

fatal: unable to access ‘https://github.com/xxx/test.git/’: OpenSSL SSL_connect: Connection was reset in connection to github.com:443

原因:一般是因为设置了代理
解决方案:

git config --global http.sslVerify false

或者

git config --global --unset http.proxy
git config --global --unset https.proxy

VSCode中管理仓库

在这里插入图片描述
以上不作赘述…

参考文档

https://www.jianshu.com/p/4ada8e44750d
https://blog.csdn.net/qq_43053280/article/details/107855771
https://www.viget.com/articles/create-a-github-repo-from-the-command-line/
https://www.cnblogs.com/zhoudayang/p/5510729.html

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值