安装Git

参考链接

电脑连接github

1、个人电脑生成密钥和公钥
打开git bash命令行窗口,输入命令:git-keygen -t rsa,指定RSA算法来生成秘钥,执行命令后敲击三次回车,之后会生成两个文件id_rsaid_rsa_pub,默认保存在 C:\Users\XXX\.ssh目录下.
2、将公钥绑定到github上
打开github->Settings->SSH and GPG keys,添加New SSH key,将id_rsa_pub里的内容复制过来。
3、验证
打开git bash命令行窗口,输入命令:ssh -T git@github.com,如果出现:Hi XXX! You've successfully authenticated, but GitHub does not provide she access.则表示连接成功。
4、电脑连接github账户
完成上面操作后可以实现从github上下载项目,但是git commit时会出现错误,应该还要打开git bash命令行窗口,输入命令:git config --global user.email "you@example.com"git config --global user.name "Your Name",执行完后即可实现项目上传。

github使用

有本地仓库

在github上新建仓库,使用push操作同步到github上

push操作

本地代码上传同步远程仓库
初始push
git init
git branch -M main
git remote add origin git@github.com:username/reponame.git
git add .
git commit -m “first commit”
git push -u origin main

之后push
git add xxx
git commit -m “first commit”
git push -u origin main

pull操作

远程仓库代码下载同步到本地
git pull origin main

有时候需要(git clean -d -fx)
注意
若在github中直接删除或增加文件,则需要:
git pull —rebase origin master
先将git库中的变化同步到本地,再进行push

无本地仓库

直接从github上克隆下来即可
在需要存放仓库的文件夹上一级目录下打开git bash,输入命令git clone git@github.com:username/reponame.git
之后即和之前push、pull操作一致

注意:记得每一次在本地修改文件时记得一定要先pull再修改,改完后再push,防止之后有问题

PowerShell使用

在PowerShell和vscode终端想使用git命令,最开始会报错“无法加载文件,因为在此系统上禁止运行脚本。”,此时需要以管理员身份运行Windows PowerShell,输入命令:set-ExecutionPolicy RemoteSigned,最后使用get-executionpolicy来查看是否更改成功,显示RomoteSigned则表示更改成功,之后则可以再vscode、Pycharm终端中直接使用PowerShell命令行窗口。