Python操作Git库 `GitPython`

Python033

Python操作Git库 `GitPython`,第1张

参考文章

参考文章

复杂点的参考

试了一圈发现,git库的用法设置非常符合原生git命令,只不过之间加了个 . 而已。

比如原本命令行里是 git add . ,这里就是 repo.git.add('.') ,

原本是 git commit -m "信息" ,这里就是 repo.git.commit(m='信息')

可以说减少了很多学习时间,基本上我很多都是没参考文档自己猜出来的也能用。

库安装好后可以直接在python中用了。

文件夹地址可以是全路径,也可以是 . 当前文件夹、 ../ 上级文件夹等用法。

下面是一种解决方案

1 把gitbash 的路径放到系统的Path环境变量里 我的是 C:\Program Files (x86)\Git\bin

2 这时候 你在系统命令行里就可以用git了

3 在python里倒入 os 模块 然后执行

os.system('git') 就可以了

C:\Users\Administrator>python

Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win3

Type "help", "copyright", "credits" or "license" for more information.

>>> import os

>>> os.system('git')

usage: git [--version] [--help] [-C <path>] [-c name=value]

           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]

           [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]

           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]

           <command> [<args>]

The most commonly used git commands are:

   add        Add file contents to the index

   bisect     Find by binary search the change that introduced a bug

   branch     List, create, or delete branches

   checkout   Checkout a branch or paths to the working tree

   clone      Clone a repository into a new directory

   commit     Record changes to the repository

   diff       Show changes between commits, commit and working tree, etc

   fetch      Download objects and refs from another repository

   grep       Print lines matching a pattern

   init       Create an empty Git repository or reinitialize an existing one

   log        Show commit logs

   merge      Join two or more development histories together

   mv         Move or rename a file, a directory, or a symlink

   pull       Fetch from and integrate with another repository or a local branch

   push       Update remote refs along with associated objects

   rebase     Forward-port local commits to the updated upstream head

   reset      Reset current HEAD to the specified state

   rm         Remove files from the working tree and from the index

   show       Show various types of objects

   status     Show the working tree status

   tag        Create, list, delete or verify a tag object signed with GPG

'git help -a' and 'git help -g' lists available subcommands and some

concept guides. See 'git help <command>' or 'git help <concept>'

to read about a specific subcommand or concept.

1

>>>