22

I have this code in Python (using "import git"):

repo = git.Repo("my_repository")
repo.git.add("bla.txt")
repo.git.commit("my commit description")

Now I want to push this commit. I've tried a lot with no success. The Python command should be similar to this Bash command:

git push origin HEAD:refs/for/master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amigo
  • 221
  • 1
  • 2
  • 3

5 Answers5

31

Following is the code to git add, git commit and then git push using GitPython.

Install GitPython using pip install gitpython.

from git import Repo

PATH_OF_GIT_REPO = r'path\to\your\project\folder\.git'  # make sure .git folder is properly configured
COMMIT_MESSAGE = 'comment from python script'

def git_push():
    try:
        repo = Repo(PATH_OF_GIT_REPO)
        repo.git.add(update=True)
        repo.index.commit(COMMIT_MESSAGE)
        origin = repo.remote(name='origin')
        origin.push()
    except:
        print('Some error occured while pushing the code')    

git_push()
Gulzar
  • 23,452
  • 27
  • 113
  • 201
BlackBeard
  • 10,246
  • 7
  • 52
  • 62
  • Is there a way to enter the password via python? – nerdguy Dec 10 '19 at 15:32
  • 2
    @nerdguy TIPS: do a little search on git ssh. will make life a lot easier and secure. no need to use password and http :) – aimme Apr 25 '20 at 19:19
  • configure git using git ssh environment setup to work from command line without user inputting username / password. keep passwords out of code. – CodingMatters May 17 '20 at 02:25
  • I get empty pushes to the repo? – James Sep 09 '20 at 14:43
  • [the api for git pull](https://gitpython.readthedocs.io/en/stable/reference.html#git.remote.Remote.pull) says `kwargs – Additional arguments to be passed to git-pull`. So can I directly use `repo.git.pull()` without using [`remote()`](https://gitpython.readthedocs.io/en/stable/reference.html#git.repo.base.Repo.remote) to specify `the remote` branch? I see Shaghaji answer, that is not clear to me regarding the positional parameters. I cannot see it in the doks. – Timo Jul 17 '21 at 18:12
5

You can try the following. It may have your problem solved...

repo.git.pull('origin', new_branch)
repo.git.push('origin', new_branch)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shahaji
  • 175
  • 1
  • 4
  • 14
4

This can be achieved by using Index (documented a little bit here) like so:


from git import Repo
repo = Repo('path/to/git/repo')  # if repo is CWD just do '.'

repo.index.add(['bla.txt'])
repo.index.commit('my commit description')
origin = repo.remote('origin')
origin.push()
Marc
  • 4,820
  • 3
  • 38
  • 36
1

Looking at the documentation page of gitpython http://gitpython.readthedocs.io/en/stable/tutorial.html. You have to define a remote repo with something like origin = repo.create_remote('origin', repo.remotes.origin.url)

then origin.pull()

I would look at the whole example in the documentation in the section "Handling Remotes"

Here is the full example from the documentation

empty_repo = git.Repo.init(osp.join(rw_dir, 'empty'))
origin = empty_repo.create_remote('origin', repo.remotes.origin.url)
assert origin.exists()
assert origin == empty_repo.remotes.origin == empty_repo.remotes['origin']
origin.fetch()                  # assure we actually have data. fetch() returns useful information
# Setup a local tracking branch of a remote branch
empty_repo.create_head('master', origin.refs.master)  # create local branch "master" from remote "master"
empty_repo.heads.master.set_tracking_branch(origin.refs.master)  # set local "master" to track remote "master
empty_repo.heads.master.checkout()  # checkout local "master" to working tree
# Three above commands in one:
empty_repo.create_head('master', origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
# rename remotes
origin.rename('new_origin')
# push and pull behaves similarly to `git push|pull`
origin.pull()
origin.push()
# assert not empty_repo.delete_remote(origin).exists()     # create and delete remotes
somesingsomsing
  • 3,182
  • 4
  • 29
  • 46
  • 2
    Thanks, I've seen this example but it still didn't work for me... There is an error in the first line origin = repo.create_remote('origin', repo.remotes.origin.url): "git remote add origin ssh:........ returned exit status 128: fatal: remote origin already exists. Any suggestions? – amigo Jan 25 '17 at 16:41
  • if someone can help I'll so appreciate it :) – amigo Feb 23 '17 at 08:54
  • This example makes no sense. `repo.create_remote('origin', repo.remotes.origin.url)` is creating the 'origin' remote by referencing the url of the 'origin' remote??? If `repo.remotes.origin` is not None then you already have the origin remote and can just simply do `repo.remotes.origin.push()` – StFS Oct 30 '22 at 01:52
1

I had the same problem. I solved it by calling

repo.git.push("origin", "HEAD:refs/for/master")
imolit
  • 7,743
  • 3
  • 25
  • 29