zoukankan      html  css  js  c++  java
  • python--用python操作Git

    用python操作Git

    使用第三方模块gitpython

    安装

    pip install gitpython
    

    使用

    from git import Repo
    
    r = Repo("C:\Users\robert\Desktop\test") # 创建一个操作对象
    
    # git add 添加测试.txt
    r.index.add([r'C:Users
    obertDesktop	est添加测试.txt'])  
    
    # git commit -m 'python 操作git'
    r.index.commit("python 操作git")
    
    # git branch
    r.branches  # [<git.Head "refs/heads/dev">, <git.Head "refs/heads/list">]
    print([str(b) for b in r.branches]) # ['dev', 'list', 'master']
    
    # git tag
    r.tags
    print([ str(t) for t in r.tags]) # ['v0.0.1', 'v0.1']
    
    # 当前分支
    r.active_branch
    
    # 创建分支 git branch 分支名
    r.create_head('dev')
    
    # 克隆 git clone
    r.clone_from(url,to_path)
    
    # git tag -a v1.3 创建tag
    r.create_tag("v1.3")
    
    # git log
    r.iter_commits()
    print([str(i.message) for i in r.iter_commits()]) # 获取提交信息
    print([str(i.message) for i in r.iter_commits()]) # 查看提交的hash值
    
    # git push origin master
    r.remote().push('master')
    
    # git pull
    r.remote().pull()
    

    执行Git原生语句的方法

    from git import Git
    
    r = Git("C:\Users\robert\Desktop\test")
    
    # 执行原生语句
    r.execute('git log') 
    print(r.execute('git log'))
    
    # 提交记录
    r.commit('-m 提交记录')
    
    # 切换分支
    r.checkout('master')
    
  • 相关阅读:
    sort
    Sicily--17956. Maximum Multiple
    代码1005
    487-3279的解法实例
    487-3279另一种解法
    487-3279
    人工智能--识别句子
    1003-Hangover
    推荐书单(转自GITHUB)
    转自微信号:测试那点事
  • 原文地址:https://www.cnblogs.com/robertx/p/10889030.html
Copyright © 2011-2022 走看看