准备工作
- 下载git工具;
可以到官网下载,我下载的是2.18.0 - 注册GitHub账号;
安装git工具
![img_db95481483ac4fff0ede927afaf3c445.png](https://yqfile.alicdn.com/img_db95481483ac4fff0ede927afaf3c445.png?x-oss-process=image/resize,w_1400/format,webp)
![img_a1a77b3a03fc1a052ad6668acb9098e2.png](https://yqfile.alicdn.com/img_a1a77b3a03fc1a052ad6668acb9098e2.png?x-oss-process=image/resize,w_1400/format,webp)
![img_5833adff972265809988d40e27476e30.png](https://yqfile.alicdn.com/img_5833adff972265809988d40e27476e30.png?x-oss-process=image/resize,w_1400/format,webp)
![img_d091dda042da2ca3f3eac33e16cf7201.png](https://yqfile.alicdn.com/img_d091dda042da2ca3f3eac33e16cf7201.png?x-oss-process=image/resize,w_1400/format,webp)
![img_2025bee5aba1ef4fbcf6a70916e502a2.png](https://yqfile.alicdn.com/img_2025bee5aba1ef4fbcf6a70916e502a2.png?x-oss-process=image/resize,w_1400/format,webp)
![img_5ff7d5802c2461703d5ec3fc885c0153.png](https://yqfile.alicdn.com/img_5ff7d5802c2461703d5ec3fc885c0153.png?x-oss-process=image/resize,w_1400/format,webp)
![img_c12e7289d44e685f7980d75e8d148424.png](https://yqfile.alicdn.com/img_c12e7289d44e685f7980d75e8d148424.png?x-oss-process=image/resize,w_1400/format,webp)
![img_6ccabb1f68fdb268ab8af8c08f94b2ef.png](https://yqfile.alicdn.com/img_6ccabb1f68fdb268ab8af8c08f94b2ef.png?x-oss-process=image/resize,w_1400/format,webp)
![img_aa969f3b384a6a305e4c4d09b94f66a1.png](https://yqfile.alicdn.com/img_aa969f3b384a6a305e4c4d09b94f66a1.png?x-oss-process=image/resize,w_1400/format,webp)
![img_f4814df7fca5072adfc8f0f337c6f4fc.png](https://yqfile.alicdn.com/img_f4814df7fca5072adfc8f0f337c6f4fc.png?x-oss-process=image/resize,w_1400/format,webp)
![img_1eb3925386076e02688749bc80b445e3.png](https://yqfile.alicdn.com/img_1eb3925386076e02688749bc80b445e3.png?x-oss-process=image/resize,w_1400/format,webp)
安装完成。
设置git工具
在开始菜单中找到Git文件夹,点击【Git Bash】:
![img_d84dcf11ba8e59e8db967a26b736c1ab.png](https://yqfile.alicdn.com/img_d84dcf11ba8e59e8db967a26b736c1ab.png?x-oss-process=image/resize,w_1400/format,webp)
弹出如下界面
![img_21cd51f628542dc00c6b3f3e65728b78.png](https://yqfile.alicdn.com/img_21cd51f628542dc00c6b3f3e65728b78.png?x-oss-process=image/resize,w_1400/format,webp)
先填写一下用户名和邮箱用作标识
git config --global user.name "xxx"
git config --global user.email "xxx"
![img_8e0fe4312428fc65116f219e390135a1.png](https://yqfile.alicdn.com/img_8e0fe4312428fc65116f219e390135a1.png?x-oss-process=image/resize,w_1400/format,webp)
连接GitHub
因为本地的git仓库和GitHub之间是通过SSH或HTTPS连接的,我这里采用SSH方式。
- 创建SSH密钥,在用户主目录下,查看是否存在.ssh文件,如果存在,再看文件下有没有id_rsa和id_rsa.pub这两个文件,如果都存在,可直接到下一步。如果没有,单击右键然后选择【Git Bash Here】创建SSH Key
![img_7291da8831c3272985b5e214832e2944.png](https://yqfile.alicdn.com/img_7291da8831c3272985b5e214832e2944.png?x-oss-process=image/resize,w_1400/format,webp)
使用命令创建
ssh-keygen -t rsa -C "注册GitHub的邮箱"
然后一路回车
![img_1b300260305a0ebc31116fed01cd4480.png](https://yqfile.alicdn.com/img_1b300260305a0ebc31116fed01cd4480.png?x-oss-process=image/resize,w_1400/format,webp)
创建完成后可以看到用户目录下生成了【.ssh】文件夹,里面有公钥和私钥文件
![img_8cc9a7e408bfdc36cc02c402c8d3210a.png](https://yqfile.alicdn.com/img_8cc9a7e408bfdc36cc02c402c8d3210a.png?x-oss-process=image/resize,w_1400/format,webp)
- 在GitHub上设置密钥
选择用户下来菜单【Settings】-【SSH and GPG keys】-【New SSH key】
![img_fb04c743acb864c11b955ed3b47bee5e.png](https://yqfile.alicdn.com/img_fb04c743acb864c11b955ed3b47bee5e.png?x-oss-process=image/resize,w_1400/format,webp)
将本地生成的公钥复制过来
![img_72bf725acc12c625b43f7cb04d67a211.png](https://yqfile.alicdn.com/img_72bf725acc12c625b43f7cb04d67a211.png?x-oss-process=image/resize,w_1400/format,webp)
测试是否能连接成功,在git bush界面输入
ssh -T git@github.com
![img_fa4e47e4407a6af6c5d0c4f8cfebbd5a.png](https://yqfile.alicdn.com/img_fa4e47e4407a6af6c5d0c4f8cfebbd5a.png?x-oss-process=image/resize,w_1400/format,webp)
看到【Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.】就证明密钥设置成功,同时【.ssh】文件夹下会多出一个【known_hosts】文件。
![img_b60f0324de2a417195cf08ef10471b8c.png](https://yqfile.alicdn.com/img_b60f0324de2a417195cf08ef10471b8c.png?x-oss-process=image/resize,w_1400/format,webp)
- 关联远程仓库和本地仓库
在本地项目所在文件夹鼠标右键单击运行【Git Bash Here】,然后运行如下命令:
git remote add origin https://github.com/y3231057/kubernetes-library.git
【注意】:如果这里碰到错误【fatal: not a git repository (or any of the parent directories): .git】,这是因为在当前目录下找不到【.git】文件夹,只需要运行下面命令即可解决
git init
![img_bdc82c8b57f3a879f5ee2f0130c19d3a.png](https://yqfile.alicdn.com/img_bdc82c8b57f3a879f5ee2f0130c19d3a.png?x-oss-process=image/resize,w_1400/format,webp)
- 提交本地仓库内容到远程仓库
在连接到远程仓库后运行如下命令:
git push -u origin master
这里会弹出输入用户名和密码的页面,直接输入即可
![img_234bc7e8b0029effaef9af413d34fa2a.png](https://yqfile.alicdn.com/img_234bc7e8b0029effaef9af413d34fa2a.png?x-oss-process=image/resize,w_1400/format,webp)
这是第一次推送master分支的所有内容命令,以后每次本地提交可以使用如下命令
git push origin master
如果工程目录下存在已有文件夹也需要提交,在运行push命令前需要使用命令添加当前目录下所有文件。
git add .
然后使用下面的命令将【git add .】存入暂存区的修改内容提交至本地仓库中,若文件未添加至暂存区,则提交时不会提交任何修改。
git commit -m "描述"
最后再运行push命令提交到远程仓库,然后在GitHub上就可以看到刚刚上传的文件。
![img_3ad8e4a4362b83e968d1f6b589a7489b.png](https://yqfile.alicdn.com/img_3ad8e4a4362b83e968d1f6b589a7489b.png?x-oss-process=image/resize,w_1400/format,webp)
![img_249c1cd369d6f24086333d9fb7bb06d5.png](https://yqfile.alicdn.com/img_249c1cd369d6f24086333d9fb7bb06d5.png?x-oss-process=image/resize,w_1400/format,webp)
【注意】:如果这里碰到错误【error: src refspec master does not match any.】,这是因为本地仓库和远程仓库的内容不一致造成的,只需要把远程仓库的内容同步到本地仓库就可以解决。
git pull --rebase origin master
![img_e0422a271d3e0dc2af67780237969f3d.png](https://yqfile.alicdn.com/img_e0422a271d3e0dc2af67780237969f3d.png?x-oss-process=image/resize,w_1400/format,webp)
git其他命令
删除文件
git rm {文件名}
删除文件夹
git rm -r {文件夹}