gitpython安装以及操作
- 一、下载安装
- 1.1在线安装
- 1.2离线安装
- 二、gitpython使用注意事项
- 三、gitpython使用
- 3.1 克隆仓库到本地
- 3.2 添加邮箱账号
- 3.3 git操作
- add
- commit
- push
- log
- 索引/暂存区对象 - Index
- 远程版本库操作 - Remotes
- Python脚本使用gitpython
一、下载安装
本文章参考原文链接:/baiyangcao/p/
/kai-/p/
/lwcai****/article/details/89382242
必须Git (1. or newer)
Python >= 3.7
1.1在线安装
pip3 install gitpython
1.2离线安装
[root@localhost gitpython]# cat
gitdb-4.0.
smmap-4.0.0-py2.
GitPython-3.1.
typing_extensions-3.10.0.
1.外网机器下载安装包(Windows和Linux都可以使用)
pip3 download -d /gitpython GitPython
或pip3 download -d /gitpython -r
2.将包放到内网安装(Windows:--find-links=D:\gitpython\)
pip3 install --no-index --find-links=/opt/gitpython/ GitPython
或pip install --no-index --find-links=/opt/gitpython/ -r
3.卸载
pip3 uninstall -r
二、gitpython使用注意事项
这里要注意一点原生的git命令会有两种参数形式,一种是:命令 --参数关键字 参数;一种是:*命令 --参数关键字=参数;这两种转化时要用如下形式,总之空格要用逗号区分,并且注意顺序:
# 第一种情况
repo.git.reset("--hard", "3ab65we")
# 第二中情况
repo.git.log("--date=short", "--pretty=format:%H:%h:%an:%ad:%cd:%cn:%s")
三、gitpython使用
3.1 克隆仓库到本地
登录操作台
python3
import os
from import Repo
方法一:
#定义下载代码的路径
#/opt/test/:表示路径 timerovers:表示目录名,新建一个以这个目录名的目录并克隆到该目录
download_path = ('/opt/test/','timerovers')
#克隆仓库并指定分支
Repo.clone_from('https://账号:密码@/USER/',to_path=download_path,branch='master')
方法二:(没验证)
# 创建remote:先创建远程url
remote = repo.create_remote(name='gitlab', url='git@:USER/')
# 如果是通过clone下载的项目可直接通过()创建remote对象
remote = repo.clone_from(git_url, to_path).remote()
方法三:
获取repo对象
GitPython的所有git操作都是通过Repo对象来操作的,获取该对象的方式有三种
# 选择已有仓库
repo = ('仓库地址')
# 在文件夹里新建一个仓库,如果已存在git仓库也不报错不覆盖没问题
repo = (path='文件夹地址')
# 克隆仓库
repo = .clone_from(url='git@:USER/', to_path='../new')
3.2 添加邮箱账号
git.config("--global","","timerovers")
git.config("--global","","timerovers@")
3.3 git操作
add
见:目录克隆到本地--方法三
如果仓库已经克隆到本地
repo = Repo(dir_path) #获取repo对象
git = repo.git # 通过repo对象获取git对象
git.add('') # git add 添加所有('.')
commit
git.commit('-m', 'this is a test') # git commit -m 'this is a test'
push
repo = Repo(dir_path) #获取repo对象
remote = repo.remote() #通过repo对象获取remote对象
remote.push()
log
log_msg=git.log()
print (log_msg)
索引/暂存区对象 - Index
Git 术语中,index 表示暂存区,为下次将要提交到版本库里的文件,
GitPython 提供 来操作暂存区,如添加、提交操作
index = repo.index
index.add([''])
index.remove([''])
index.commit('this is a test')
远程版本库操作 - Remotes
Remotes 用于操作远程版本库,可以通过 方法获取远程版本库,
属性获取远程版本库列表
# 获取默认版本库 origin
remote = repo.remote()
# 从远程版本库拉取分支
remote.pull()
# 推送本地分支到远程版本库
remote.push()
# 重命名远程分支
# ('new_origin')
remote.fetch()
Python脚本使用gitpython
#-*- coding:UTF-8 -*-
import os
import git.cmd
from git.repo import Repo
#定义克隆远程仓库URL及本地目录
REMOTE_URL = "http://账号:密码@192.168.10.8:7990/scm/bambooscript/"
dir_path = os.path.join("D:\/test-dir","code")
git_user = "bamboo"
git_email = "bamboo@"
#判断仓库本地目录是否存在
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", "", "%s" %(user))
git.config("--global", "", "%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=()
#print (log_msg)
#克隆到本地
git_clone(REMOTE_URL,dir_path)