利用python代码操作git
python操作git
安装模块
pip3 install gitpython
基本使用
import os
from git.repo import Repo
# 创建本地路径用来存放远程仓库下载的代码
download_path = os.path.join('NB')
# 拉取代码
Repo.clone_from('https://github.com/DominicJi/TeachTest.git',to_path=download_path,branch='master')
其他常见操作
# ############## 2. pull最新代码 ##############
import os
from git.repo import Repo
local_path = os.path.join('NB')
repo = Repo(local_path)
repo.git.pull()
# ############## 3. 获取所有分支 ##############
import os
from git.repo import Repo
local_path = os.path.join('NB')
repo = Repo(local_path)
branches = repo.remote().refs
for item in branches:
print(item.remote_head)
# ############## 4. 获取所有版本 ##############
import os
from git.repo import Repo
local_path = os.path.join('NB')
repo = Repo(local_path)
for tag in repo.tags:
print(tag.name)
# ############## 5. 获取所有commit ##############
import os
from git.repo import Repo
local_path = os.path.join('NB')
repo = Repo(local_path)
# 将所有提交记录结果格式成json格式字符串 方便后续反序列化操作
commit_log = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,
date='format:%Y-%m-%d %H:%M')
log_list = commit_log.split("\n")
real_log_list = [eval(item) for item in log_list]
print(real_log_list)
# ############## 6. 切换分支 ##############
import os
from git.repo import Repo
local_path = os.path.join('NB')
repo = Repo(local_path)
before = repo.git.branch()
print(before)
repo.git.checkout('master')
after = repo.git.branch()
print(after)
repo.git.reset('--hard', '854ead2e82dc73b634cbd5afcf1414f5b30e94a8')
# ############## 7. 打包代码 ##############
import os
from git.repo import Repo
local_path = os.path.join(NB')
repo = Repo(local_path)
with open(os.path.join('NB.tar'), 'wb') as fp:
repo.archive(fp)
将上述所有的方法封装到类中以便后续的调用(后续如果你想要操作git直接拷贝使用即可)
import os
from git.repo import Repo
from git.repo.fun import is_git_dir
class GitRepository(object):
"""
git仓库管理
"""
def __init__(self, local_path, repo_url, branch='master'):
self.local_path = local_path
self.repo_url = repo_url
self.repo = None
self.initial(repo_url, branch)
def initial(self, repo_url, branch):
"""
初始化git仓库
:param repo_url:
:param branch:
:return:
"""
if not os.path.exists(self.local_path):
os.makedirs(self.local_path)
git_local_path = os.path.join(self.local_path, '.git')
if not is_git_dir(git_local_path):
self.repo = Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)
else:
self.repo = Repo(self.local_path)
def pull(self):
"""
从线上拉最新代码
:return:
"""
self.repo.git.pull()
def branches(self):
"""
获取所有分支
:return:
"""
branches = self.repo.remote().refs
return [item.remote_head for item in branches if item.remote_head not in ['HEAD', ]]
def commits(self):
"""
获取所有提交记录
:return:
"""
commit_log = self.repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',
max_count=50,
date='format:%Y-%m-%d %H:%M')
log_list = commit_log.split("\n")
return [eval(item) for item in log_list]
def tags(self):
"""
获取所有tag
:return:
"""
return [tag.name for tag in self.repo.tags]
def change_to_branch(self, branch):
"""
切换分值
:param branch:
:return:
"""
self.repo.git.checkout(branch)
def change_to_commit(self, branch, commit):
"""
切换commit
:param branch:
:param commit:
:return:
"""
self.change_to_branch(branch=branch)
self.repo.git.reset('--hard', commit)
def change_to_tag(self, tag):
"""
切换tag
:param tag:
:return:
"""
self.repo.git.checkout(tag)
if __name__ == '__main__':
local_path = os.path.join('codes', 'luffycity')
repo = GitRepository(local_path,remote_path)
branch_list = repo.branches()
print(branch_list)
repo.change_to_branch('dev')
repo.pull()
利用python代码操作git的更多相关文章
- 利用Python代码编写计算器小程序
import tkinter import tkinter.messagebox import math class JSQ: def __init__(self): #创建主界面 self.root ...
- 合并代码操作 | git fetch 与 git pull
前言 首先我们要说简单说git的运行机制.git分为本地仓库和远程仓库,我们一般情况都是写完代码,commit到本地仓库(生成本地仓的commit ID,代表当前提交代码的版本号),然后push到远程 ...
- day06 python代码操作MySQL
day06 python代码操作MySQL 今日内容 python代码操作MySQL 基于python与MySQL实现用户注册登录 python操作MySQL python 胶水语言.调包侠(贬义词& ...
- 字符编码和Python代码操作文件
字符编码和Python代码操作文件 读写模式之a模式 # a模式 只追加模式 # 路径不存在:自动创建 with open(r'a.txt','a',encoding='utf8') as f: pa ...
- 利用Python openpyxl操作Excel
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = ...
- 利用python代码获取文件特定的内容,并保存为文档
说明:有段时间需要读取上百个文件的单点能(sp),就写了下面的代码(计算化学狗努力转行中^-^) import os.path import re # 1 遍历指定目录,显示目录下的所有文件名 def ...
- 通过Python代码操作MySQL:
pymsql / MySQLdb pymysql支持 py2/py3 MySQLdb支持py2 ORM框架 django orm ( 自己对数据连接有优化机制 ) SQLAlchemy ( 自带数据库 ...
- 利用Python操作MySQL数据库
前言 在工作中,我们需要经常对数据库进行操作,比如 Oracle.MySQL.SQL Sever 等,今天我们就学习如何利用Python来操作 MySQL 数据库. 本人环境:Python 3.7.0 ...
- 八.python文件操作
一,初识文件操作. 引子: 现在这个世界上,如果可以操作文件的所有软件都消失了,比如word,wps等等,此时你的朋友通过qq给你发过来一个文件,文件名是:美女模特空姐护士联系方式.txt,在座的所有 ...
随机推荐
- 吴裕雄--天生自然 JAVA开发学习:包(package)
package pkg1[.pkg2[.pkg3…]]; package net.java.util; public class Something{ ... } package animals; i ...
- Tomcat远程debug配置
当我们需要定位生产环境问题,而日志又不清晰的情况下,我们可以借助Tomcat提供的远程调试,设置如下: // Linxu系统: apach/bin/startup.sh开始处中增加如下内容: decl ...
- 浮动( Floats )
浮动( Float )概述 浮动和文字环绕 浮动框就是一个框在当前行被向左或向右挪动(偏移),它不在常规流中,浮动框由浮动元素的框组成. 浮动框( 'float', 'floated' or 'flo ...
- 使用XAMPP集成开发环境安装Apache、PHP的配置说明
一.安装XAMPP 双击安装包xamppinstaller.exe,可完成XAMPP的安装,与其他软件安装并无区别. 二.配置Apache端口,使用其可以正确启动 点击Start,启动Apache时可 ...
- python常见内置函数总结
简单的内置函数 len 求长度 min 求最小值 max 求最大值 sorted 排序 reversed 反向 sum 求和 进制转换 bin 转为二进制 oct 转为八 ...
- Qt的窗口如何显示在spi屏幕上
问题:我们可以将qt移植到H3上,但是如果要将窗口通过spi屏进行显示应该怎么做呢,和framebuffer又有什么关系? 这里只说一下qt在spi屏上显示的技术原理,具体的实现还需要自己努力. 一. ...
- Pandas Series 对象的loc与iloc区别
import pandas as pd temp = pd.Series([,,,,]) loc用法: temp.loc[:] 0 1 1 2 2 3 3 4 # 输出索引为0-3的值(基于索引) t ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- 基于Redis的Service缓存实现
项目中有使用到缓存,每次需要将缓存代码和业务代码杂糅在一起,以及分散各处的key,严重影响代码的可读性.以下是使用AOP对其简单尝试.直接上代码: 1.定义缓存注解: @Target(ElementT ...
- [LC] 273. Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...