with using gitpython module, I am writing python script to check git diff --> git add of all modified files one by one. At the end I want to commit all these changes, but I didn't find the exact syntax of the command.
I'm trying with below code, 'git add' works perfectly but 'git commit' gives error.
import git
repo = git.Repo(os.getcwd())
files = repo.git.diff(None, name_only=True)
for f in files.split('\n'):
show_diff(f)
repo.git.add(f)
repo.git.commit('test commit', author='[email protected]')
Here is the error I'm seeing, it seems the something is missing in cmd arguments.
In [10]: repo.git.commit("test commit", author="[email protected]")
---------------------------------------------------------------------------
GitCommandError Traceback (most recent call last)
<ipython-input-10-b4505b7c53c2> in <module>()
----> 1 repo.git.commit("test commit", author="[email protected]")
c:\python27\lib\site-packages\git\cmd.pyc in <lambda>(*args, **kwargs)
421 if name[0] == '_':
422 return LazyMixin.__getattr__(self, name)
--> 423 return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
424
425 def set_persistent_git_options(self, **kwargs):
c:\python27\lib\site-packages\git\cmd.pyc in _call_process(self, method, *args, **kwargs)
866 call.extend(args)
867
--> 868 return self.execute(call, **_kwargs)
869
870 def _parse_object_header(self, header_line):
c:\python27\lib\site-packages\git\cmd.pyc in execute(self, command, istream, with_extended_output, with_exceptions, as_process, output_stream, stdout_as_string, kill_after_timeout, with_stdout, universal_newlines, shell, **subprocess_kwargs)
684
685 if with_exceptions and status != 0:
--> 686 raise GitCommandError(command, status, stderr_value, stdout_value)
687
688 if isinstance(stdout_value, bytes) and stdout_as_string: # could also be output_stream
GitCommandError: Cmd('git') failed due to: exit code(1)
cmdline: git commit [email protected] test commit
stderr: 'error: pathspec 'test commit' did not match any file(s) known to git.'
-
Have you read the documentation ?Tim Biegeleisen– Tim Biegeleisen2016年11月16日 13:18:25 +00:00Commented Nov 16, 2016 at 13:18
-
10@TimBiegeleisen a better response might be how to apply the documentation to the question at hand. There are levels of understanding. The documentation in its current status can be at times confusing depending on what level you're starting. I've recently adopted this library but the amount questions I see pointing straight back to the documentation probably should be more indicative that the documentation just isn't doing a good job. The currently accepted answer here is just an example of how unhelpful the documentation is as it is defaulting to the catchall command that GitPython offers.Marc– Marc2019年07月06日 15:37:25 +00:00Commented Jul 6, 2019 at 15:37
-
7here's an example of the type of interaction I view as somewhat helpful. stackoverflow.com/a/56915801/2128265 - notice that I'm not just pointing to documentation but showing how to apply it. RTFM-esque comments are toxic and only silence questions, ultimately hurting the use of the corresponding package.Marc– Marc2019年07月06日 16:23:18 +00:00Commented Jul 6, 2019 at 16:23
2 Answers 2
Resolved the issue, need to add "-m" flag in the commit command as below:
repo.git.commit('-m', 'test commit', author='[email protected]')
Comments
You can also use repo.index:
repo.index.add(relative_path)
repo.index.commit("message")
See https://gitpython.readthedocs.io/en/stable/quickstart.html#add-file-to-staging-area