Skip to content

Commit e395ac9

Browse files
committed
Added tutorial about initializing a repository.
Additionally, for this and future examples, there is a test_doc.py suite to contain all code mentioned in the docs. That way, we know if things stop working. Fixes gitpython-developers#236
1 parent 048ffa5 commit e395ac9

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

doc/source/tutorial.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,22 @@ The previous approach would brutally overwrite the user's changes in the working
415415
repo.heads.master.checkout() # checkout the branch using git-checkout
416416
repo.heads.other_branch.checkout()
417417

418+
Initializing a repository
419+
*************************
420+
421+
In this example, we will initialize an empty repository, add an empty file to the index, and commit the change::
422+
423+
repo_dir = 'my-new-repo'
424+
file_name = os.path.join(repo_dir, 'new-file')
425+
426+
r = git.Repo.init(repo_dir)
427+
# This function just creates an empty file ...
428+
touch(file_name)
429+
r.index.add([file_name])
430+
r.index.commit("initial commit")
431+
432+
Please have a look at the individual methods as they usually support a vast amount of arguments to customize their behavior.
433+
418434
Using git directly
419435
******************
420436
In case you are missing functionality as it has not been wrapped, you may conveniently use the git command directly. It is owned by each repository instance::

git/test/test_docs.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#-*-coding:utf-8-*-
2+
# test_git.py
3+
# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
4+
#
5+
# This module is part of GitPython and is released under
6+
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
7+
import os
8+
9+
import git
10+
from git.test.lib import TestBase
11+
from gitdb.test.lib import with_rw_directory
12+
from git.repo.fun import touch
13+
14+
15+
class TestGit(TestBase):
16+
17+
@with_rw_directory
18+
def test_add_file_and_commit(self, rw_dir):
19+
repo_dir = os.path.join(rw_dir, 'my-new-repo')
20+
file_name = os.path.join(repo_dir, 'new-file')
21+
22+
r = git.Repo.init(repo_dir)
23+
# This function just creates an empty file ...
24+
touch(file_name)
25+
r.index.add([file_name])
26+
r.index.commit("initial commit")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy