赞
踩
本篇文章给大家谈谈python拉取git提交的代码,以及pycharm上如何拉git代码,希望对各位有所帮助,不要忘了收藏本站喔。
GitPython 是一个用于操作 Git 版本库的 python 包,它提供了一系列的对象模型(库 - Repo
、树 - Tree
、提交 - Commit
等)用于操作版本库中的相应对象。主要通过python对远程代码进行拉取、提交等操作
安装及使用:
- pip install gitpython
-
- import gitpython
GitPython的所有git操作都是通过Repo对象来操作的,通过创建版本库对象,实现对代码仓库的操作python动态爱心代码。获取该对象的方式有三种:
-
- # 选择已有仓库
- repo = git.Repo('仓库地址')
-
- # 在文件夹里新建一个仓库,如果已存在git仓库也不报错不覆盖没问题
- repo = git.Repo.init(path='文件夹地址')
-
- # 克隆仓库
- repo = git.Repo.clone_from(url='git@github.com:USER/REPO.git', to_path='../new')
通过对repo执行命令,可对当前仓库进行操作
- # 获取当前分支
- repo.git.branch()
- repo.active_branch
-
- # 切换分支
- repo.git.checkout('branch_name')
-
- # 拉取当前分支代码
- repo.git.pull()
-
- # 版本库是否为空版本库
- repo.bare
-
- # 当前工作区是否干净
- repo.is_dirty()
-
- # 版本库中未跟踪的文件列表
- repo.untracked_files
-
- # 克隆版本库
- repo.clone('clone_path')
-
- # 压缩版本库到 tar 文件
- with open('repo.tar', 'wb') as fp:
- repo.archive(fp)
-
- # 新建分支
- repo.create_head('branchname')

- index = repo.index # 获取暂存区对象
- index.add(['new.txt']) # add操作
- index.remove(['old.txt']) # 删除暂存区对象
- index.commit('this is a test') # 提交
- # 创建remote:
- remote = repo.create_remote(name='gitlab', url='git@gitlab.com:USER/REPO.git')
-
- # 如果是通过clone下载的项目可直接通过repo.remote()创建remote对象
- remote = repo.clone_from(git_url, to_path).remote()
-
- # 远程交互:
- remote = repo.remote()
- remote.fetch()
- remote.pull()
- remote.push()
参考链接
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。