LoginSignup
5
5

More than 5 years have passed since last update.

GitPythonで特定のブランチ/タグを指定してcloneする

Posted at

目的

表題の通り。
pythonで以下のコマンドと等価な操作を実行したい。

git clone <git_repo> -b <branch>

解決策

GitPythonを利用して実現する場合以下の通り実行する。

git.Repo.clone_from(<git_repo>, <dest>, branch=<branch>)

ブランチの指定

GitPythonの’gh-pages'ブランチを'/home/ser/GitPython'にクローンする:

python
#!/usr/bin/env python

import git

git.Repo.clone_from('https://github.com/gitpython-developers/GitPython',
                    '/home/user/GitPython',
                    branch='gh-pages')

タグの指定

GitPythonの’1.0.1'タグを'/home/user/GitPython'にクローンする:

python
#!/usr/bin/env python

import git

git.Repo.clone_from('https://github.com/gitpython-developers/GitPython',
                    '/home/user/GitPython',
                    branch='1.0.1')
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5