0

I'm trying to solve an easy task with git in python. Actually I just want something like this

git clome remote-repo
git pull
edit file
git commit file
git push
rm -rf remote-repo

So just change a file and commit it and push it right away. But I have my problems get my head arround the documentation http://gitpython.readthedocs.org/en/stable/tutorial.html This is what I got so far

import git
import shutil
import os

repo_url = '[email protected]:users/me/mytest'
repo_dir = '/tmp/test/repo'
work_file_name = 'testFile'
work_file = os.path.join(repo_dir, work_file_name)
if os.path.isdir(repo_dir):
   shutil.rmtree(repo_dir)
repo = git.Repo.clone_from(repo_url, repo_dir)

git = repo.git
git.pull()
new_file_path = os.path.join(repo.working_tree_dir, work_file_name)
f = open(new_file_path, 'w')
f.write("just some test")
f.close()
repo.index.commit("test commit")

git.push()
shutil.rmtree(repo_dir)

It actually creates a commit and pushes it to the server, but it contains no changes, so the file I tried to write is empty. Any idea what I'm missing?

1 Answer 1

3

You have to stage the file/change before the commit:

repo.index.add([new_file_path])

As a side note: The following line overrides your import and might lead to errors if you ever expand your script:

git = repo.git
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I already tried it with index.add before, but forgot the brackets and got an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.