zoukankan      html  css  js  c++  java
  • python操作git

    GitPython 是一个用于操作 Git 版本库的 python 包,它提供了一系列的对象模型(库 - Repo、树 - Tree、提交 - Commit等),用于操作版本库中的相应对象。

    模块安装

    pip install gitpython

    初始化

    from git import Repo
    repo =Repo("D:s17c.py") #git文件的路径

    操作

    from git import Repo
    repo =Repo("F:git") #git文件的路径
    
    #获取当前所在的分支 git branch
    print(repo.active_branch)
    
    #添加到缓存区 git add命令
    repo.index.add(["test.txt"])
    
    #提交到版本库 git commit -m
    repo.index.commit("创建test文件")
    
    #添加版本号  git tag -a
    repo.create_tag("v0.1")
    
    #创建分支 git branch dev
    repo.create_head("dev")
    
    #回退到某个版本 git reset --hard hash
    repo.index.reset(commit="哈希地址",head=True)
    
    #获取所有分支
    print([str(b) for b in repo.branches])
    
    #查看标签
    print(repo.tags)
    import git
    #git clone
    clone = git.Repo.clone_from("url","to_path")
    
    #git pull
    clone.remote().pull()
    
    #git push
    clone.remote().push()

    gitpython还可以直接操作git命令

    g = git.Git("PATH")
    g.add()
    g.commit("-m  ")
  • 相关阅读:
    HDU 1098 Ignatius's puzzle 也不大懂
    HDU 1099 Lottery
    图算法-Prime
    并查集
    CSS笔记2
    css笔记1
    HDU 5019 Revenge of GCD
    POJ 2255 Tree Recovery
    判断两条线段是否相交
    PAT 数列求和-加强版   (20分)(简单模拟)
  • 原文地址:https://www.cnblogs.com/wanglan/p/10718876.html
Copyright © 2011-2022 走看看