python 访问git仓库_python操作git

"""

基于Python实现对git仓库进行操作,使用前需要安装模块:gitpython

pip3 install gitpython

"""

# ############## 1. clone下载代码 ##############

"""

import os

from git.repo import Repo

download_path = os.path.join('code', 'fuck')

Repo.clone_from('https://gitee.com/wupeiqi/fuck.git', to_path=download_path, branch='master')

"""

# ############## 2. pull最新代码 ##############

"""

import os

from git.repo import Repo

local_path = os.path.join('code', 'fuck')

repo = Repo(local_path)

repo.git.pull()

"""

# ############## 3. 获取所有分支 ##############

"""

import os

from git.repo import Repo

local_path = os.path.join('code', 'fuck')

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('code', 'fuck')

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('code', 'fuck')

repo = Repo(local_path)

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('code', 'fuck')

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('code', 'fuck.tar'), 'wb') as fp:

repo.archive(fp)

"""

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值