Automate Git Operations Using Python
Automate your git operations using GitPython.
Today we are going to automate our day-to-day Git operations using a python package called GitPython.
What is GitPython? GitPython is a python library used to interact with git repositories. It is a module in python used to access our git repositories.
It provides abstractions of git objects for easy access of repository data, and additionally allows you to access the git repository more directly using pure python implementation.
Installation: GitPython can be installed easily using pip
pip install GitPython
Initializing and opening a local repository:
Initialize a new repository:
from git import Repo new_repo = Repo.init('/path/to/new/repo_directory')
Running above code snippet is equal to running
git init on your terminal. It will create a new directory in the given path with the given repo name.
Open a existing local repository:
from git import Repo existing_repo = Repo('path/to/existing/repo')
Clone a remote repository:
A remote repository can be clones using Repo.clone_from() which takes two arguments url of the remote repository and path where you want to clone the repository.
from git import Repo Repo.clone_from('https://username:firstname.lastname@example.org/username/repository.git', '/destination/directory')
Working with branches:
The below code snippet will show you
- How to list branches.
- Hot checkout to a branch
- How to create a new branch
from git import Repo repo = Repo('/path/to/a/local/repo') # List all branches for branch in repo.branches: print(branch) # Create a new branch repo.git.branch('my_new_branch') # To checkout to a branch: repo.git.checkout('branch_name')
Get file changes using diff:
To get the differences on your current repo and see what has changed since the last commit, you can use
Repo.git.diff() and pass it the tree of your last (HEAD) commit with
from git import Repo repo = Repo('my_repo') # Check differences between current files and last commit diff = repo.git.diff(repo.head.commit.tree) print(diff)
Add and commit files:
Once you have determined that a repo has changes and what files have changed, you will want to add and then commit the files to the repository.
from git import Repo repo = Repo('/path/to/local/repo') # Add files. Accepts a list of files repo.index.add(['path/to/file_1', '/path/to/file_2',.......,'/path/to/file_n']) # Commit repo.index.commit('your commit message')
Get all commits done in current branch:
Use the below code snippet to retrieve all the commits done in the current branch.
from git import Repo local_repo = Repo('/path/to/local/repo') #Iterate 50 commits, and if you need paging, you can specify a number of commits to skip. commits = local_repo.iter_commits('master', max_count=50) for commit in commits: print(commit.author) # author name print(commit.commiter) # name of the committer print(commit.message) # will print the commit message print(commit.committed_date) # will print the date in which the commit made. print(commit.committed_date) # will print the date and time in which the commit made. # you can get more attributes of the commit object using dir(commit) # this will return commits 21-30 from the commit list as traversed backwards master # ten_commits_past_twenty = list(repo.iter_commits('master', max_count=10, skip=20))
Hope you enjoyed it.
Thank you for reading.
Please give your valuable feedback and suggestions.