导航
配置Git
下载并安装Git
我这里之前安装过,可能不一样,如果没什么特殊要求,一路点下去就完事儿。
查看安装状态
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