Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Have you had a chance to work with Git? If so, did you know that you can work with your git repository from within python script?

GitPython python library does exactly this job:

http://gitpython.readthedocs.io/en/stable/

#python #git #push #pull #GitPython
Few days ago we talk about gitPython to work with git inside of python. The code below is a sample that would do all routine tasks like pulling and pushing or commit.

1- Initiate git object in python by providing path:

from git import Repo
repo = Repo(repo_path)


2- If you want to pull results from git repo:

repo.git.pull('origin', 'refs/heads/dev')


3- Let's say you want to set username and email for git author:

config = repo.config_writer()
config.set_value("user", "email", author_email)
config.set_value("user", "name", author_name)


4- Now to add a specific file to staged:

index = repo.index
index.add([file_path])


5- Commit the staged file with a message:

index.commit(commit_message)


6- The final step is to push to a remote repo:

repo.git.push('origin', 'refs/heads/dev')

#python #git #gitPython #pull #push #author