使用git建立本地仓库并连接远程仓库上传文件

2023-05-16

新建本地仓库

  1. 进入本地文件夹,右击git bash here进入git命令行

  2. 第一次使用,需要先初始化本地仓库:$ git init

  3. 将文件夹下的文件,添加到本地仓库中:
    添加指定文件:$ git add <文件名>
    添加全部文件:$ git add . (注意add和.之间有空格)
    查看当前状态:$ git status

新建远程仓库

  1. 在github页面上选择new,然后按照要求填入仓库信息即可。
    在这里插入图片描述

提交到远程仓库

  1. 如果没配置过git和github的连接,可参考:https://cloud.tencent.com/developer/article/1504684,先创建ssh key进行配置。

  2. 配置好后,将本地仓库连接远程仓库:
    https协议连接:$ git remote add origin https://github.com/<用户名>/<远程仓库名>.git
    也可改用ssh连接:$ git remote set-url origin git@github.com:<用户名>/<远程仓库名>.git
    删除连接:$ git remote rm origin

  3. 把本地仓库的项目,提交到本地缓冲区:$ git commit -m "first commit"

  4. 把本地缓冲区的更新,提交到远程仓库的master分支(若不存在则会新建分支):
    对新建的空仓库:$ git push -u origin master
    对已有内容的仓库:$ git push origin master
    注意,如果新建仓库时往里面添加了README.md,则需要先把它pull到本地:$ git pull --rebase origin master,再上传。
    在这里插入图片描述

  5. 对于大文件(>100MB)上传,需要另外采用git lfs上传,可参考:https://blog.csdn.net/Kevin1906/article/details/123786735
    将文件放到仓库中,然后在git命令行输入:

$ git lfs install
$ git lfs track <文件路径>
$ git add .gitattributes
$ git add <文件路径>
$ git commit -m "push big file by lfs" 
$ git config lfs.https://github.com/<用户名>/<仓库名>.git/info/lfs.locksverify false
$ git push origin master

即可成功上传:
在这里插入图片描述

常见问题

  1. fatal: unable to access ‘https://github.com/xxx/xxxxx.git/’:
    OpenSSL SSL_read: Connection was reset, errno 10054
    或:
    fatal: unable to access ‘https://github.com/xxx/xxxxx.git/’:
    Failed to connect to github.com port 443: Timed out

参考:https://blog.csdn.net/good_good_xiu/article/details/118567249,修改代理:

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

建议能科学上网还是科学上网,很多时候是网速不稳定导致的上传失败。

  1. error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8)

1)改用ssh连接:

$ git remote set-url origin git@github.com:<用户名>/<远程仓库名>.git

2)缓冲区大小不够,可重新设置上传github的缓冲区大小为500M:

$ git config http.postBuffer 524288000
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用git建立本地仓库并连接远程仓库上传文件 的相关文章

随机推荐