Git如何和Pycharm关联管理项目
声明:
1.下面输入命令都是在Pycharm的Terminal中操作
2.博主机器为Mac OS系统
3.需要先自己在本机上安装好git环境
4.有自己的github账户
具体的操作步骤如下:
1.输入git init
输出了下面的信息:
1 2 | $ git init Initialized empty Git repository in / Users / * * * / PycharmProjects / AutoTesting / .git / |
如果报错的话,检察一下pycharm里是否安装git的插件:
2.然后输入ls -al看一下,显示出来了【.git】的目录,这个目录里放的是一些git的配置:
1 2 3 4 5 6 7 8 9 10 | $ ls - al total 0 drwxr - xr - x 8 liyang staff 256 Nov 13 11 : 06 . drwxr - xr - x 29 liyang staff 928 Nov 13 10 : 31 .. drwxr - xr - x 9 liyang staff 288 Nov 13 11 : 06 .git drwxr - xr - x 8 liyang staff 256 Nov 13 11 : 04 .idea drwxr - xr - x 3 liyang staff 96 Nov 13 10 : 35 app drwxr - xr - x 3 liyang staff 96 Nov 13 10 : 35 http drwxr - xr - x 6 liyang staff 192 Nov 13 10 : 31 venv drwxr - xr - x 3 liyang staff 96 Nov 13 10 : 33 web |
这是Pycharm右上角会显示git相关的按钮,如果没有的话就像下图1这样没有出现的话关闭pycharm重启就有了(下图2)
这时可以看到CSV这个菜单下有了很多Git相关的菜单:
3.在github上建一个要关联到的代码仓库
ps:venv虚拟环境的文件夹我们是不提交到仓库的,如果选择了忽略的是python的话,会自动忽略venv文件夹的
4.pycharm中往远程仓库里推送代码,有两种方式:一种是https,一种是SSH
HTTPS的方式的话,我们继续在Terminal中输入
1 | $git remote add origin https: / / github.com / ComeonBug / AutoTesting.git |
【git remote add】就帮我们先关联起来了,然后在输入:
1 2 3 | $ git push - u origin master error: src refspec master does not match any error: failed to push some refs to 'https://github.com/ComeonBug/AutoTesting.git' |
这是因为没有add和commit,所以我们先在pycharm里建一个py文件,比如:test.py
然后先add一下这个文件,这里既可以在Terminal里通过命令【git add test.py】来添加,也可以直接在py文件中右键【Git】->【+ Add】
我这里演示Terminal:
1 | $ git add test.py |
输入上面的命令回车不报错就表示Add成功了
然后我们进行commit,同样既可以使用Terminal也可以在这个要commit的py文件中右键,或者还可以在pycharm工具右上角点击【✅】按钮
我这里演示Terminal,输入命令【git commit -m "first commit"】
1 2 3 4 5 6 7 8 9 10 | $ git commit - m "first commit" [master (root - commit) b4dfa8b] first commit 7 files changed, 41 insertions( + ) create mode 100644 .idea / .gitignore create mode 100644 .idea / AutoTesting.iml create mode 100644 .idea / inspectionProfiles / profiles_settings.xml create mode 100644 .idea / misc.xml create mode 100644 .idea / modules.xml create mode 100644 .idea / vcs.xml create mode 100644 test.py |
成功之后呢,我们再输入刚才报错的那个push命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ git push - u origin master Enumerating objects: 11 , done. Counting objects: 100 % ( 11 / 11 ), done. Delta compression using up to 4 threads Compressing objects: 100 % ( 8 / 8 ), done. Writing objects: 100 % ( 11 / 11 ), 1.42 KiB | 207.00 KiB / s, done. Total 11 (delta 0 ), reused 0 (delta 0 ) remote: remote: Create a pull request for 'master' on GitHub by visiting: remote: https: / / github.com / ComeonBug / AutoTesting / pull / new / master remote: To github.com:ComeonBug / AutoTesting.git * [new branch] master - > master Branch 'master' set up to track remote branch 'master' from 'origin' . |
可以看到,能成功提交了,然后在github的master分支上就可以看到刚刚push的代码了
但其实我们实际工作中使用常使用的是SSH的方式,但是刚刚以及remote的,在继续remote的话会报错
1 2 | $ git remote add origin git@github.com:ComeonBug / AutoTesting.git fatal: remote origin already exists. |
这里我们需要在pycharm里修改一下刚刚设置的remote:
这里编辑一下改成我们的SSH的链接URL保存就行
改为下图中SSH的URL:
通过这几步我们就配置好了,以后直接在pycharm中就能commit、push代码到仓库里了~
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步