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) """

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, 'https://gitee.com/wupeiqi/fuck.git') branch_list = repo.branches() print(branch_list) repo.change_to_branch('dev') repo.pull()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现