赞
踩
本文章参考原文链接:https://www.cnblogs.com/baiyangcao/p/gitpython.html
https://www.cnblogs.com/kai-/p/12705522.html
https://blog.csdn.net/lwcaiCSDN/article/details/89382242
必须Git (1.7.x or newer)
Python >= 3.7
pip3 install gitpython
[root@localhost gitpython]# cat requirements.txt
gitdb-4.0.7-py3-none-any.whl
smmap-4.0.0-py2.py3-none-any.whl
GitPython-3.1.20-py3-none-any.whl
typing_extensions-3.10.0.0-py3-none-any.whl
1.外网机器下载安装包(Windows和Linux都可以使用)
pip3 download -d /gitpython GitPython
或pip3 download -d /gitpython -r requirements.txt
2.将包放到内网安装(Windows:--find-links=D:\gitpython\)
pip3 install --no-index --find-links=/opt/gitpython/ GitPython
或pip install --no-index --find-links=/opt/gitpython/ -r requirements.txt
3.卸载
pip3 uninstall -r requirements.txt
这里要注意一点原生的git命令会有两种参数形式,一种是:命令 --参数关键字 参数;一种是:*命令 --参数关键字=参数;这两种转化时要用如下形式,总之空格要用逗号区分,并且注意顺序:
# 第一种情况
repo.git.reset("--hard", "3ab65we")
# 第二中情况
repo.git.log("--date=short", "--pretty=format:%H:%h:%an:%ad:%cd:%cn:%s")
登录操作台 python3 import os from git.repo import Repo 方法一: #定义下载代码的路径 #/opt/test/:表示路径 timerovers:表示目录名,新建一个以这个目录名的目录并克隆到该目录 download_path = os.path.join('/opt/test/','timerovers') #克隆仓库并指定分支 Repo.clone_from('https://账号:密码@gitee.com/USER/timerovers.git',to_path=download_path,branch='master') 方法二:(没验证) # 创建remote:先创建远程url remote = repo.create_remote(name='gitlab', url='git@gitlab.com:USER/REPO.git') # 如果是通过clone下载的项目可直接通过repo.remote()创建remote对象 remote = repo.clone_from(git_url, to_path).remote() 方法三: 获取repo对象 GitPython的所有git操作都是通过Repo对象来操作的,获取该对象的方式有三种 # 选择已有仓库 repo = git.Repo('仓库地址') # 在文件夹里新建一个仓库,如果已存在git仓库也不报错不覆盖没问题 repo = git.Repo.init(path='文件夹地址') # 克隆仓库 repo = git.Repo.clone_from(url='git@github.com:USER/REPO.git', to_path='../new')
git.config("--global","user.name","timerovers")
git.config("--global","user.email","timerovers@com.cn")
见:目录克隆到本地--方法三
如果仓库已经克隆到本地
repo = Repo(dir_path) #获取repo对象
git = repo.git # 通过repo对象获取git对象
git.add('test1.txt') # git add test1.txt 添加所有git.add('.')
git.commit('-m', 'this is a test') # git commit -m 'this is a test'
repo = Repo(dir_path) #获取repo对象
remote = repo.remote() #通过repo对象获取remote对象
remote.push()
log_msg=git.log()
print (log_msg)
Git 术语中,index 表示暂存区,为下次将要提交到版本库里的文件,
GitPython 提供 Repo.Index 来操作暂存区,如添加、提交操作
index = repo.index
index.add(['new.txt'])
index.remove(['old.txt'])
index.commit('this is a test')
Remotes 用于操作远程版本库,可以通过 Repo.remote 方法获取远程版本库,
Repo.Remotes 属性获取远程版本库列表
# 获取默认版本库 origin
remote = repo.remote()
# 从远程版本库拉取分支
remote.pull()
# 推送本地分支到远程版本库
remote.push()
# 重命名远程分支
# remote.rename('new_origin')
remote.fetch()
#-*- coding:UTF-8 -*- import os import git.cmd from git.repo import Repo #定义克隆远程仓库URL及本地目录 REMOTE_URL = "http://账号:密码@192.168.10.8:7990/scm/bambooscript/test.git" dir_path = os.path.join("D:\/test-dir","code") git_user = "bamboo" git_email = "bamboo@ampthon.com" #判断仓库本地目录是否存在 def is_dir(path): result = os.path.isdir(path + os.sep + ".git") if not result: print ("路径:%s \n是否存在:" %s %(path,result)) return result else: print ("路径:%s \n是否存在: %s" %(path,result)) return result #获取对象。path是仓库到本地的地址 def git_object(path): #获取repo对象。本地仓库地址 repo = Repo(path) #通过Repo对象获取git对象 git = repo.git #通过repo对象获取remote对象 remote = repo.remote() return git,remote #克隆远程仓库到本地 def git_clone(remote_url,local_path): dir_msg = is_dir(dir_path) if not dit_msg: Repo.clone_from(remote_url, to_path=local_path) print ("已克隆到本地") #添加提交用户 邮箱 def git_user(user,user_email): repo = Repo(dir_path) git = repo.git() git.config("--global", "user.name", "%s" %(user)) git.config("--global", "user.email", "%s" %(user_email)) #pull-add-commit-push。 commit_msg表示提交信息 def git_push(path,commit_msg): git, remote = git_object(path) remote.pull() git.add('.') git.commit('-m', '%s' %(commit_msg)) remote.push() #git_push(dir_path,msg) #git,remote=git_object(dir_path) #log_msg=git.log() #print (log_msg) #克隆到本地 git_clone(REMOTE_URL,dir_path)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。