工程地址: https://pypi.python.org/pypi/GitPython/
需要安装先安装: gitdb https://pypi.python.org/pypi/gitdb

GitPython使用模块对象访问git配置库。
仓库操作
    初始仓库对象
        from git import *
        repo = Repo(repo_path)
        assert repo.bare == False
 
    创建裸库
        repo = Repo.init(repo_path,bare=True)
        assert repo.bare == True
 
    仓库数据高层接口可以新增/删除 heads/tags/remotes和访问仓库的配置信息
        repo.config_reader() #获得仓库中只读的配置信息
        repo.config_writer() #更新仓库中的配置信息
 
    获取活动分支、未被管理的文件和判断是否有变更
        repo.is_dirty()  #返回布尔值
        repo.untracked_files    #返回未被管理的文件列表
 
    克隆和初始化一个新的仓库
        cloned_repo = repo.clone(to/this/path)
        new_repo = repo.init(path/for/new/repo)
 
数据库对象
    repo对象的性能优于数据库对象,repo对象一般用于获取大数据和新增对象。
 
GitDB
    在操作大文件时,GitDB可以使用更少的内存,但处理速度慢2到5倍
    repo = Repo('path/to/repo',odbt=GitDB)
 
GitCmdObjectDB
    使用git-cat-file实例读取配置库信息,访问速度比较快,但内存占用比GitDB严重。
    repo = Repo('path/to/repo',odbt=GitCmdObjectDB)
 
引用操作的实例
    head操作
        heads = repo.heads
        master = heads.master #lists can be accessed by name for convenience
        master.commit #the commit pointed to by head called master
        master.rename('new_name') #rename heads
    tag(tag通常是不变的)是一个commit或tag对象的引用
        tags = repo.tags
        tagref = tags[0] #tag可以有一个tag对象,存储额外的信息
        tagref.commit #tag总是指向一个commit
        repo.delete_tag(tagref) #删除一个tag
        repo.create_tag('my_tag') #创建一个tag
    符号引用可以替代具体commit指向一个引用
        head = repo.head        #the head points to the active branch/ref
        master = head.reference #but they always point to commits
        master.commit   #from here you use it as any other reference
 
    访问reflog
        log = master.log()
        log[0]  #first reflog entry
        log[-1] #last reflog entry
    修改引用
        创建、删除各种引用和修改指向
        repo.delete_head('master') #delete an existing head
        master = repo.create_head('master')  #create a new one
        master.commit = 'HEAD~10'   #set branch to another commit without changing index or working tree
        创建、删除tags
        new_tag = repo.create_tag('my_tag','my message')
        repo.delete_tag(new_tag)
        分支直接切换
        new_branch = repo.craete_head('new_branch')
        repo.head.reference = new_branch
 
    git库的各种对象
        git的所有对象都存在git数据库中。对象包含的信息有类型、未压缩的大小、每个对象都有一个20个字节的唯一的SHA1值。
        git有四类对象Blobs、Trees、Commits and Tags
        git所有的对象都可以访问,但通常是通过引用或git仓库的方法来访问,不是直接从数据库中读取。
            hc = repo.head.commit
            hct = hc.tree
            hc != hct
            hc != repo.tags[0]
            hc == repo.head.reference.commit
        git对象基本字段有
            hct.type
            hct.size
            hct.hexsha
            hct.binsha
        索引对象可以用作git的索引,这些对象是Trees/Blobs和Submodules ,这些对象含有文件路径的信息。
            hct.path    #root tree has no path
            hct.trees[0].path #the first subdirectory has one though
            hct.mode    #trees have the mode of a linux directory
            hct.blobs[0].mode #blobs have a specific mode though compareable to a standard linux fs
        使用stream访问blob数据或者其他对象数据  
            hct.blobs[0].data_stream.read() #stream object to read data from
            hct.blobs[0].stream_data(open("blob_data","w")) #write data to given stream
 
    Commit对象
        commit对象包含固定commit的信息。通过引用或者指定版本可以获取到commit对象
            repo.commit('master')
            repo.commit('v0.1')
            repo.commit('HEAD~10')
        获取100指定引用上100commit
            repo.iter_commits('master',max_count=100)
        分页显示
            显示21-30的记录
            repo.iter_commits('master',max_count=10,skip=20)
 
            headcommit = repo.head.commit
            headcommit.hexsha
            headcommit.parents
            headcommit.author
            headcommit.tree
            headcommit.committer
            headcommit.committed_date
            headcommit.message
        时间格式化
            import time
            time.asctime(time.gmtime(headcommit.committed_date))  #'Web May 7 05:56:02 2013'
            tiem.strftime("%a,%d %b %Y %H:%M",time.gmtime(headcommit.committed_date)) #'Web,7 May 2013 05:56'
        访问commit祖先
            headcommit.parents[0].parents[0].parents[0].parents[0]
            等价于master^^^^ 或者master~4
 
    Tree对象
        tree对象指向当前目录的内容。获取master分支最新提交的根tree对象
            tree = repo.heads.master.commit.tree
        通过tree对象可以获取的内容有
            tree.trees  #trees are subdirectories
            tree.blobs  #blobs are files
        可以通过名称获取tree对象
            tree[0] = tree['dir']  #access by index and by sub-path
            blob = tree[0][0]
            blob.name
            blob.path
            blob.abspath
        有简便的方法通过子目录名称就可以获取对象
            tree/"lib"
            tree/"dir/file" == blob
        如果指定tree对象的名称也可以直接从git数据库中读取
            repo.tree() #返回<git.Tree "master">
            repo.tree("c1c7214dde86...")
            repo.tree('0.1.6')
        遍历tree对象
            tree.traverse()
            for entry in tree.traverse():do_something_with(entry)
        如果tree对象返回的是子模块对象,默认为是当前head的commit
 
    索引对象
        git的索引对象包含了commit变更和合并信息。通过索引对象可以获得更复杂的信息
            index = repo.index
        读取、添加、删除实例,Commit变更:
            for stage,blob in index.iter_blobs():do_something(...)  #Access blob object
            for (path,stage),entry in index.entries.iteritems: pass #Access the entries directly
            index.add(['my_new_file'])   #add a new file to the index
            index.remove(['dir/existing_file'])
            new_commit = index.commit("my commit message")
         通过tree或者merge创建新索引
            tmp_index = Index.from_tree(repo,'HEAD~1') #load a tree into a temporary index
            merge_index = Index.from_tree(repo,'base','HEAD','some_branch') #merge two trees three-way
            merge_index.write('merged_index')
    远程仓库
        远程名称作为外部从仓库的别名,可以通过它push和fetch数据
            test_remote = repo.create_remote('test','git@server:repo.git')
            repo.delete_remote(test_remote) # create and delete remotes
            origin = repo.remotes.origin #get default remote by name
            origin.refs  #local remote reference
            o = origin.rename('new_origin') #rename remotes
            o.fetch()   #fetch,pull and push from and to the remote
            o.pull()
            o.push()
        远程库的配置信息
            o.url
        修改配置信息
            o.config_writer.set('pushurl','other_url')
    子模块
 
    对象比较
        可以比较index和Trees或者Index和working tree 或者trees和trees以及trees和working copy
            hcommit = repo.head.commit
            idiff = hcommit.diff()  #diff tree against index
            tdiff = hcommit.diff('HEAD~1')  #diff tree against previous tree
            wdiff = hcommit.diff(None)  #diff tree against working tree
 
            index = repo.index
            index.diff() #diff index agginst itself yielding empty idff
            index.diff(None) #diff index against working copy
            index.diff('HEAD') #diff index against current HEAD tree
        比较返回的比较索引本质上是一个Diff对象列表,通过额外的过滤方法你可以找到你想要的内容
        for diff_added in wdiff.iter_change_type('A'): do_something_with(diff_added)
 
    分支切换
        想切换分支,你需要设置HEAD指向新分支,重置index和工作区
            repo.head.reference = repo.heads.other_branch
            repo.head.reset(index=True,working_tree=True)
        上面的方法会覆盖掉工作区中所有修改未提交的边更新,下面的方法则不会
            repo.heads.master.checkout() #checkout the branch using git-checkout
            repo.heads.other_branch.checkout()
    直接使用git库
        通过git实例使用git命令
            git = repo.git
            git.checkout('head',b='my_new_branch')  #default command
            git.for_each_ref()  #'-' becomes '_' when calling it
 

GitPython git python 的开发库的更多相关文章

  1. Python测试 ——开发工具库

    Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. mechanize- Python中有状态的程序化Web浏 ...

  2. Python集成开发环境Pycharm+Git+Gitee(码云)

    ********************************************************************* 本文主要介绍集成开发环境的配置过程,方便多人协作办公.代码版 ...

  3. python测试开发工具库汇总(转载)

    Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. mechanize- Python中有状态的程序化Web浏 ...

  4. sklearn:Python语言开发的通用机器学习库

    引言:深入理解机器学习并全然看懂sklearn文档,须要较深厚的理论基础.可是.要将sklearn应用于实际的项目中,仅仅须要对机器学习理论有一个主要的掌握,就能够直接调用其API来完毕各种机器学习问 ...

  5. 《Python测试开发技术栈—巴哥职场进化记》—软件测试工程师“兵器库”

    上文<Python测试开发技术栈-巴哥职场进化记>-初来乍到,请多关照 我们介绍了巴哥入职后见到了自己的导师华哥,第一次参加团队站会,认识了团队中的开发小哥哥和产品小姐姐以及吃到了公司的加 ...

  6. 【转】windows和linux中搭建python集成开发环境IDE

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  7. 【转】linux和windows下安装python集成开发环境及其python包

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  8. Python后端开发要求

    关于Python后端开发要求 一.对Python有兴趣,熟悉Python(标准库) 最好阅读过源码 了解Python的优化(熟悉pypy更佳) 二.至少至少一门语言(不说"精通") ...

  9. Pycharm中进行Python远程开发

    http://blog.csdn.net/pipisorry/article/details/52269952 PyCharm提供两种远程调试(Remote Debugging)的方式:    配置远 ...

随机推荐

  1. hdu 3007 Buried memory 最远点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with ...

  2. 【CodeForces】【#285】Div.2

    生平第一场Codeforce……纪念一下,虽然跪的跟渣渣似的……啊不就是跪成渣渣了…… A.B暴力过去的……不知道会不会超时……C我犯了个2B错误,让输出总共多少条边,我都求出来边集E了……直接输出E ...

  3. C# Winform常见的Editor及其它经验

    1.新建一个自定义Editor,继承自.NET自带的Editor,override某些方法,再附加到属性中: public class MyCollectionEditor : CollectionE ...

  4. WSDL

    WSDL 彻底理解webservice SOAP WSDL WSDL 详解 http://www.cnblogs.com/hujian/p/3494064.html http://www.cnblog ...

  5. 重构:CSS也面向对象

    最初接触到面向对象的CSS还是因为项目中的CSS已经超过八千行,缺乏约束和管理,在近期或者是不远的将来,有迫切的要求需要重构.在前端重构中,我们已经讨论过了JavaScript面向对象的重构,在这个时 ...

  6. MySQL注入load_file常用路径

    WINDOWS下: c:/boot.ini //查看系统版本 c:/windows/php.ini //php配置信息 c:/windows/my.ini //MYSQL配置文件,记录管理员登陆过的M ...

  7. C#&java重学笔记(泛型)

    C#部分: 1.泛型的出现主要用于解决类.接口.委托.方法的通用性,通过定义泛型类.接口.委托.方法,可以让不同类型的数据使用相同运算规则处理数据,方便了开发. 2.利用System.Nullable ...

  8. PC端模拟手机浏览网页

    很多网站都通过User-Agent来判断浏览器类型,如果是3G手机,显示手机页面内容,如果是普通浏览器,显示普通网页内容. 谷歌Chrome浏览器,可以很方便地用来当3G手机模拟器.在Windows的 ...

  9. P==NP??

    注:基础知识见下方 下面是关于P==NP ???  一些讨论,挺好玩的. 1. 首先强调一下数学上还没有证明这个问题!但是我们看看其他角度来看这个问题. 其次,心理上来说,要是可以证明P==NP那么早 ...

  10. VMware 使用

    1.客户操作系统被禁用: BIOS中开启VT(Virtual Technology)