python 操作 Git
安装模块
pip3 install gitpython
基本使用
- import os
- from Git.repo import Repo
- # 创建本地路径用来存放远程仓库下载的代码
- download_path = os.path.join('NB')
- # 拉取代码
- Repo.clone_from('https://github.com/DominicJi/TeachTest.git',to_path=download_path,branch='master')
其他常见操作
- # ############## 2. pull 最新代码 ##############
- import os
- from Git.repo import Repo
- local_path = os.path.join('NB')
- repo = Repo(local_path)
- repo.Git.pull()
- # ############## 3. 获取所有分支 ##############
- import os
- from Git.repo import Repo
- local_path = os.path.join('NB')
- repo = Repo(local_path)
- branches = repo.remote().refs
- for item in branches:
- print(item.remote_head)
- # ############## 4. 获取所有版本 ##############
- import os
- from Git.repo import Repo
- local_path = os.path.join('NB')
- repo = Repo(local_path)
- for tag in repo.tags:
- print(tag.name)
- # ############## 5. 获取所有 commit ##############
- import os
- from Git.repo import Repo
- local_path = os.path.join('NB')
- repo = Repo(local_path)
- # 将所有提交记录结果格式成 JSON 格式字符串 方便后续反序列化操作
- commit_log = repo.Git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,
- date='format:%Y-%m-%d %H:%M')
- log_list = commit_log.split("\n")
- real_log_list = [eval(item) for item in log_list]
- print(real_log_list)
- # ############## 6. 切换分支 ##############
- import os
- from Git.repo import Repo
- local_path = os.path.join('NB')
- repo = Repo(local_path)
- before = repo.Git.branch()
- print(before)
- repo.Git.checkout('master')
- after = repo.Git.branch()
- print(after)
- repo.Git.reset('--hard', '854ead2e82dc73b634cbd5afcf1414f5b30e94a8')
- # ############## 7. 打包代码 ##############
- import os
- from Git.repo import Repo
- local_path = os.path.join(NB')
- repo = Repo(local_path)
- with open(os.path.join('NB.tar'), 'wb') as fp:
- repo.archive(fp)
将上述所有的方法封装到类中以便后续的调用 (后续如果你想要操作 Git 直接拷贝使用即可)
- import os
- from Git.repo import Repo
- from Git.repo.fun import is_git_dir
- class GitRepository(object):
- """
- git 仓库管理
- """ def __init__(self, local_path, repo_url, branch='master'):
- self.local_path = local_path
- self.repo_url = repo_url
- self.repo = None
- self.initial(repo_url, branch)
- def initial(self, repo_url, branch):
- """
- 初始化 git 仓库
- :param repo_url:
- :param branch:
- :return:
- """
- if not os.path.exists(self.local_path):
- os.makedirs(self.local_path)
- git_local_path = os.path.join(self.local_path, '.git')
- if not is_git_dir(git_local_path):
- self.repo = Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)
- else:
- self.repo = Repo(self.local_path)
- def pull(self):
- """
- 从线上拉最新代码
- :return:
- """
- self.repo.Git.pull()
- def branches(self):
- """
- 获取所有分支
- :return:
- """
- branches = self.repo.remote().refs
- return [item.remote_head for item in branches if item.remote_head not in ['HEAD', ]]
- def commits(self):
- """
- 获取所有提交记录
- :return:
- """ commit_log = self.repo.Git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',
- max_count=50,
- date='format:%Y-%m-%d %H:%M')
- log_list = commit_log.split("\n")
- return [eval(item) for item in log_list]
- def tags(self):
- """
- 获取所有 tag
- :return:
- """
- return [tag.name for tag in self.repo.tags]
- def change_to_branch(self, branch):
- """
- 切换分值
- :param branch:
- :return:
- """
- self.repo.Git.checkout(branch)
- def change_to_commit(self, branch, commit):
- """
- 切换 commit
- :param branch:
- :param commit:
- :return:
- """
- self.change_to_branch(branch=branch)
- self.repo.Git.reset('--hard', commit)
- def change_to_tag(self, tag):
- """
- 切换 tag
- :param tag:
- :return:
- """
- self.repo.Git.checkout(tag)
- if __name__ == '__main__':
- local_path = os.path.join('codes', 'luffycity')
- repo = GitRepository(local_path,remote_path)
- branch_list = repo.branches()
- print(branch_list)
- repo.change_to_branch('dev')
- repo.pull()
来源: https://www.cnblogs.com/guapitomjoy/p/12382605.html