3

I found this Bash script, which accomplishes the same task but in Bash.

Working with GitPython to create a complete backup of a repository, can't seem to figure out how to get all the branches to show without having to manual checkout them.

Repo.clone_from(repo, folder)

I'm using this line in a loop to copy all the files from the repos.

Community
  • 1
  • 1
ghh1415
  • 43
  • 1
  • 4
  • https://gitpython.readthedocs.io/en/stable/reference.html#git.repo.base.Repo.clone_from has multi_options argument, in theory must be possible set multi_options=['--mirror'] for cloning full copy, but it not works for me and I have no time for investigation. – Alexey Shrub Oct 01 '19 at 14:34
  • @AlexeyShrub better to use kwargs: mirror=True (see my updated answer) – dlazesz Nov 06 '19 at 18:25
  • All branches can be listed using "Repo.remotes.origin.refs" – Ravi Bandoju May 12 '20 at 18:20

2 Answers2

3

I have recently stumbled upon this thread, but have not found any solution, so I have brewed my own. It is better than nothing, neither optimal nor pretty, but at least it works:

# Clone master
repo = Repo.clone_from('https://github.com/user/stuff.git', 'work_dir', branch='master')

# Clone the other branches as needed and setup them tracking the remote
for b in repo.remote().fetch():
    repo.git.checkout('-B', b.name.split('/')[1], b.name)

Explanation:

  1. Clone the master branch
  2. Fetch the remote branch names
  3. Checkout branches and create them as needed

PS: I think Gitpython has not got a built-in method to do this with clone_from.

UPDATE:

no_single_branch=True option in GitPyhton is equal to --no-single-branch in git CLI (other 'valueless' arguments can also supplied with True value)

repo = Repo.clone_from('https://github.com/user/stuff.git', 'work_dir', branch='master', no_single_branch=True)
dlazesz
  • 168
  • 17
-1
for b in Repo.clone_from(repo, folder).remote[0].fetch():
   print(b.name)
brianSan
  • 545
  • 6
  • 17
  • TypeError: 'instancemethod' object has no attribute '__getitem__' for b in Repo.clone_from(repo, folder).remote[0].fetch(): I am getting this error currently when using this code – ghh1415 Feb 12 '17 at 08:12
  • for b in Repo.clone_from(repo, folder).remotes.origin.fetch(): print b.name This will print out all the names of the branches, still having trouble copying them to the folders though. – ghh1415 Feb 12 '17 at 10:03