gitpython安装以及操作

一、下载安装

本文章参考原文链接:https://www.cnblogs.com/baiyangcao/p/gitpython.html
https://www.cnblogs.com/kai-/p/12705522.html
https://blog.csdn.net/lwcaiCSDN/article/details/89382242

必须Git (1.7.x or newer)
Python >= 3.7

1.1在线安装

pip3 install gitpython

1.2离线安装

[root@localhost gitpython]# cat requirements.txt 
gitdb-4.0.7-py3-none-any.whl
smmap-4.0.0-py2.py3-none-any.whl
GitPython-3.1.20-py3-none-any.whl
typing_extensions-3.10.0.0-py3-none-any.whl

1.外网机器下载安装包(Windows和Linux都可以使用)
pip3 download -d /gitpython GitPython
或pip3 download -d /gitpython -r requirements.txt
2.将包放到内网安装(Windows:--find-links=D:\gitpython\)
pip3 install --no-index --find-links=/opt/gitpython/ GitPython
或pip install --no-index --find-links=/opt/gitpython/ -r requirements.txt
3.卸载
pip3 uninstall -r requirements.txt

二、gitpython使用注意事项

这里要注意一点原生的git命令会有两种参数形式,一种是:命令 --参数关键字 参数;一种是:*命令 --参数关键字=参数;这两种转化时要用如下形式,总之空格要用逗号区分,并且注意顺序:

# 第一种情况
repo.git.reset("--hard", "3ab65we")
 
# 第二中情况
repo.git.log("--date=short", "--pretty=format:%H:%h:%an:%ad:%cd:%cn:%s")

三、gitpython使用

3.1 克隆仓库到本地

登录操作台
python3
import os
from git.repo import Repo

方法一:
#定义下载代码的路径
#/opt/test/:表示路径 timerovers:表示目录名,新建一个以这个目录名的目录并克隆到该目录
download_path = os.path.join('/opt/test/','timerovers')
#克隆仓库并指定分支
Repo.clone_from('https://账号:密码@gitee.com/USER/timerovers.git',to_path=download_path,branch='master')

方法二:(没验证)
# 创建remote:先创建远程url
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()

方法三:
获取repo对象
GitPython的所有git操作都是通过Repo对象来操作的,获取该对象的方式有三种
# 选择已有仓库
repo = git.Repo('仓库地址') 
 
# 在文件夹里新建一个仓库,如果已存在git仓库也不报错不覆盖没问题
repo = git.Repo.init(path='文件夹地址')
 
# 克隆仓库
repo = git.Repo.clone_from(url='git@github.com:USER/REPO.git', to_path='../new')

3.2 添加邮箱账号

git.config("--global","user.name","timerovers")
git.config("--global","user.email","timerovers@com.cn")

3.3 git操作

add

见:目录克隆到本地--方法三
如果仓库已经克隆到本地
repo = Repo(dir_path) #获取repo对象
git = repo.git # 通过repo对象获取git对象 
git.add('test1.txt') # git add test1.txt  添加所有git.add('.')

commit

git.commit('-m', 'this is a test') # git commit -m 'this is a test'

push

repo = Repo(dir_path) #获取repo对象
remote = repo.remote() #通过repo对象获取remote对象
remote.push()

log

log_msg=git.log()
print (log_msg)

索引/暂存区对象 - Index

Git 术语中,index 表示暂存区,为下次将要提交到版本库里的文件,
GitPython 提供 Repo.Index 来操作暂存区,如添加、提交操作

index = repo.index
index.add(['new.txt'])
index.remove(['old.txt'])
index.commit('this is a test')

远程版本库操作 - Remotes

Remotes 用于操作远程版本库,可以通过 Repo.remote 方法获取远程版本库,
Repo.Remotes 属性获取远程版本库列表

# 获取默认版本库 origin
remote = repo.remote()
# 从远程版本库拉取分支
remote.pull()
# 推送本地分支到远程版本库
remote.push()
# 重命名远程分支
# remote.rename('new_origin')
remote.fetch()

Python脚本使用gitpython

#-*- coding:UTF-8 -*-

import os
import git.cmd
from git.repo import Repo

#定义克隆远程仓库URL及本地目录
REMOTE_URL = "http://账号:密码@192.168.10.8:7990/scm/bambooscript/test.git"
dir_path = os.path.join("D:\/test-dir","code")

git_user = "bamboo"
git_email = "bamboo@ampthon.com"

#判断仓库本地目录是否存在
def is_dir(path):
  result = os.path.isdir(path + os.sep + ".git")
  if not result:
    print ("路径:%s \n是否存在:" %s %(path,result))
    return result
  else:
    print ("路径:%s \n是否存在: %s" %(path,result))
    return result
 
#获取对象。path是仓库到本地的地址
def git_object(path):
  #获取repo对象。本地仓库地址
  repo = Repo(path)
  #通过Repo对象获取git对象
  git = repo.git
  #通过repo对象获取remote对象
  remote = repo.remote()
  return git,remote

#克隆远程仓库到本地
def git_clone(remote_url,local_path):
  dir_msg = is_dir(dir_path)
  if not dit_msg:
    Repo.clone_from(remote_url, to_path=local_path)
    print ("已克隆到本地")

#添加提交用户 邮箱
def git_user(user,user_email):
  repo = Repo(dir_path)
  git = repo.git()
  git.config("--global", "user.name", "%s" %(user))
  git.config("--global", "user.email", "%s" %(user_email))

#pull-add-commit-push。 commit_msg表示提交信息
def git_push(path,commit_msg):
  git, remote = git_object(path)
  remote.pull()
  git.add('.')
  git.commit('-m', '%s' %(commit_msg))
  remote.push()

#git_push(dir_path,msg)
#git,remote=git_object(dir_path)
#log_msg=git.log()
#print (log_msg)
#克隆到本地
git_clone(REMOTE_URL,dir_path)
  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装gitpython库,可以通过以下步骤进行操作: 1. 首,你可以通过PyPi官网下载gitpython及其依赖包,如gitdb,gitpython,smmap,typing_extensions。确保下载的包与你本地的Python版本兼容。 2. 如果你下载的是whl包,可以找到whl包的路径,然后在命令行中执行以下命令进行安装: ``` python -m pip install xxx.whl ``` 3. 如果你下载的是tar包,可以解压zip包,然后进入解压后的文件夹,找到setup.py文件,执行以下命令进行安装: ``` python setup.py install ``` 4. 如果以上方法不起作用,你可以尝试更新pip库,执行以下命令: ``` python -m pip install --upgrade pip ``` 5. 如果你熟悉使用git,你也可以通过git安装库。在github或者gitee上下载gitpython的文件,执行以下命令: ``` git clone https://github.com/gitpython-developers/gitpython ``` 这些步骤可以帮助你安装gitpython库。希望对你有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [Gitpython实战(第一弹)](https://blog.csdn.net/songyuyang88/article/details/126980023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【安装】conda\pip\清华源\git安装python(gym为例)的方法(持更)](https://blog.csdn.net/panbaoran913/article/details/127266395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值