相关文章推荐
开心的保温杯  ·  【质问】封禁后的“陈一发儿”仍被狂刷礼物 ...·  9 月前    · 
微醺的匕首  ·  张杰-西安外国语大学国际关系学院·  1 年前    · 
爽快的大蒜  ·  【Python】desc获取表结构创建ora ...·  1 年前    · 
有胆有识的仙人掌  ·  在 Azure Boards ...·  1 年前    · 
失恋的煎鸡蛋  ·  吉祥如意 Big Fortune ...·  1 年前    · 
Code  ›  gitpython模块——使用python操作git开发者社区
git提交代码 git python repo
https://cloud.tencent.com/developer/article/1600803
要出家的台灯
1 年前
作者头像
GH
0 篇文章

gitpython模块——使用python操作git

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > python、mysql、go知识点积累 > gitpython模块——使用python操作git

gitpython模块——使用python操作git

作者头像
GH
发布 于 2020-03-19 09:09:44
8.5K 0
发布 于 2020-03-19 09:09:44
举报

gitpython模块——使用python操作git

安装

pip3 install gitpython

基本使用:pull/clone

from git.repo import Repo
import os
# 从远程仓库下载代码到本地   pull/clone
download_path = os.path.join('test','t1')
# 从远程仓库将代码下载到上面创建的目录中
Repo.clone_from('https://github.com/ylpb/CMDB.git',to_path=download_path,branch='master')

更多操作

pull最新代码

# ############## 2. pull最新代码 ##############
import os
from git.repo import Repo
local_path = os.path.join('test','t1')
repo = Repo(local_path)
repo.git.pull()

获取所有分支

# ############## 3. 获取所有分支 ##############
import os
from git.repo import Repo
local_path = os.path.join('test','t1')
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('test','t1')
repo = Repo(local_path)
for tag in repo.tags:
    print(tag.name)

获取所有commit

# ############## 5. 获取所有commit ##############
import os
from git.repo import Repo
local_path = os.path.join('test','t1')
repo = Repo(local_path)

将所有提交记录结果格式成json格式字符串

# 将所有提交记录结果格式成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('test','t1')
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. 打包代码 ##############
with open(os.path.join('test','t1'), 'wb') as fp:
    repo.archive(fp)

封装之后的版本

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', 't1')
    repo = GitRepository(local_path,remote_path)
    branch_list = repo.branches()
 
推荐文章
开心的保温杯  ·  【质问】封禁后的“陈一发儿”仍被狂刷礼物 网友:...
9 月前
微醺的匕首  ·  张杰-西安外国语大学国际关系学院
1 年前
爽快的大蒜  ·  【Python】desc获取表结构创建oracle 建表语句-CSDN博客
1 年前
有胆有识的仙人掌  ·  在 Azure Boards 中管理项目的积压工作和面板 - Azure Boards | Microsoft Learn
1 年前
失恋的煎鸡蛋  ·  吉祥如意 Big Fortune Modern - Long size envelope packet — Joys Red Packet
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号