自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败。最重要的原因是我习惯本地编辑,完成以后再一起上传github。看过了几个教程,总结出最适合自己的比较简单的方法。
![](https://i-blog.csdnimg.cn/blog_migrate/e7380507ec7e45e87b8b2afcfb030569.webp?x-image-process=image/format,png)
1. github在线上传文件夹
在线上传也可以上传完整的文件夹结构,直接拖拽到上传文件页面的框中即可。
1.1点击上传文件
![](https://i-blog.csdnimg.cn/blog_migrate/39406bb293f9fdc2bc7faceac3442f0f.webp?x-image-process=image/format,png)
1.2 直接拖拽
直接拖拽即可上传文件夹及文件夹里面的文件。如果点击 choose your files 就只能上传单个文件。
![](https://i-blog.csdnimg.cn/blog_migrate/b0c77d2899dbff11cb5921b98b44df8b.webp?x-image-process=image/format,png)
2. 通过git工具上传本地文件夹(本地项目)
2.1 下载git工具
![](https://i-blog.csdnimg.cn/blog_migrate/e2aee3906313cab6e993715f56cba9a9.webp?x-image-process=image/format,png)
2.2 下载完成后安装完成,注意在安装过程中可以选择创建桌面快捷方式
![](https://i-blog.csdnimg.cn/blog_migrate/de52717e31c89fd1ad15a985a012cf08.webp?x-image-process=image/format,png)
2.3 绑定用户
打开git-bash.exe(直接在桌面上点击右键,或者点击开始按钮找到Git Bash)
![](https://i-blog.csdnimg.cn/blog_migrate/4380d89e04c8b235d3da605bc122fb32.webp?x-image-process=image/format,png)
在打开的GIt Bash中输入以下命令(用户和邮箱为你github注册的账号和邮箱)
$ git config --global user.name "hanyuntao"
$ git config --global user.email "hanyuntaocn@163.com"
![](https://i-blog.csdnimg.cn/blog_migrate/bbc547f61e88f81939183f0945d545d5.webp?x-image-process=image/format,png)
2.4 设置SSH key(git中sshkey有何作用?)
2.4.1 生成ssh key
首先检查是否已生成密钥cd ~/.ssh
,如果返回的ls
有3个文件,则密钥已经生成。
![](https://i-blog.csdnimg.cn/blog_migrate/efac5e528a5121ec0ec5ccdbaed78fab.webp?x-image-process=image/format,png)
如果没有密钥,则通过
$ ssh-keygen -t rsa -C "hanyuntaocn@163.com"
生成,生成过程中一路按3次回车键就好了。(默认路径,默认没有密码登录)
生成成功后,去对应目录C:\Users\hyt.ssh里(hyt为电脑用户名,每个人不同)用记事本打开id_rsa.pub,得到ssh key公钥。
![](https://i-blog.csdnimg.cn/blog_migrate/98722488b378acc6a914bfa8121b9a1e.webp?x-image-process=image/format,png)
2.4.2 为github账号配置ssh key
切换到github,展开个人头像的小三角,点击settings,然后打开SSH keys菜单, 点击Add SSH key新增密钥,填上标题(最好跟本地仓库保持一致)。
![](https://i-blog.csdnimg.cn/blog_migrate/77b718a69b0165759d566eb3c688f6b1.webp?x-image-process=image/format,png)
接着将id_rsa.pub文件中key粘贴到此,最后Add key生成密钥吧。\
2.5 上传本地项目到github
2.5.1 创建一个本地项目
这是我自己创建的几个文件夹及文件。
![](https://i-blog.csdnimg.cn/blog_migrate/029632184ce7d0496d47c37f03259412.webp?x-image-process=image/format,png)
2.5.2 建立本地仓库
1.首先进入text文件夹
cd d:text
![](https://i-blog.csdnimg.cn/blog_migrate/4d68d4e70e5a704d4ca545b70f9a2bcf.webp?x-image-process=image/format,png)
2.执行指令:git init
![](https://i-blog.csdnimg.cn/blog_migrate/bf2a24596de11eeed318108d628268a0.webp?x-image-process=image/format,png)
初始化成功后你会发现项目里多了一个隐藏文件夹.git
![](https://i-blog.csdnimg.cn/blog_migrate/02a2a89e2d373484d6bc9deb5d6cdc2b.webp?x-image-process=image/format,png)
3.执行指令:git add .
将所有文件添加到仓库
![](https://i-blog.csdnimg.cn/blog_migrate/277d8277d07c3acbbca647480904616e.webp?x-image-process=image/format,png)
4.执行指令:git commit -m "提交文件"
双引号内是提交注释。
2.5.3 关联github仓库
1.到github text仓库复制仓库地址
![](https://i-blog.csdnimg.cn/blog_migrate/442f43ecae63930ff96c733fa0231750.webp?x-image-process=image/format,png)
2.执行指令:git remote add origin https://github.com/hanyuntao/text.git
![](https://i-blog.csdnimg.cn/blog_migrate/d6535de979aed73401c8097658ec9d6a.webp?x-image-process=image/format,png)
2.5.4 上传本地代码
执行指令:git push -u origin master
![](https://i-blog.csdnimg.cn/blog_migrate/3a620ccf9c5fcd9a37294f8c34c6c487.webp?x-image-process=image/format,png)
2.5.5完成了
可以看到我们的本地项目已经上传到了github上了。
![](https://i-blog.csdnimg.cn/blog_migrate/5c6432a27988f5deb6120eee92b89b4c.webp?x-image-process=image/format,png)
注意:git是不能管理空的文件夹的,文件夹里必须有文件才能上传。