文章目录
Windows操作系统下
代码
注意,一下代码所在的文件夹需要添加到环境变量中
winPushGit.py
import sys,os
userName='你的GitHub用户名'
password='你的GitHub密码'
gitignore='''
venv
.idea
__pycache__
'''
def exeCmd(cmd):
cmds=cmd.split('\n')
for c in cmds:
os.system(c)
if __name__ == '__main__':
print('git init directory: ',sys.argv[1])
dir=sys.argv[1]
if os.path.exists(f'{dir}/.git'):#已经初始化
pass
else:
with open('.gitignore', 'w') as f:
f.write(gitignore)
url=input('input github repository URL: ')
if url=='':
exit(0)
suffix=url.replace('https://','')
https=f'https://{userName}:{password}@{suffix}.git'
cmd=f'''
git init
git remote add origin {https}
'''
exeCmd(cmd)
#公操作
cmd='''
git add -A
git commit -m "commit"
git push origin master
'''
exeCmd(cmd)
pushGit.cmd
:: @echo off
python D:\codes\PycharmProjects\tool\winPushGit.py "%cd%"
使用方法
- 在GitHub新建一个repository(已经新建知识库可以忽略这一步)
- 找到需要添加的文件夹,按shift并按S打开命令行
- 输入:pushGit
- 如果是第一次提交,需要输入repository目录(图1)
- 提交完毕