Reputation: 125
I'm writing a bash script that can initialize a new repository from a GitHub template that I've already created. I'd like the script to replicate the steps laid out in this GitHub help doc, but I can't figure out how to use GitHub templates from the command line.
I'm using Ubuntu 18.04 on WSL2. I've defined a function project()
(see below) that takes one input (the name of the new project) and is supposed to do the following:
create_project.py
that creates the new project's folders locally and creates the remote repo on GitHub#!/bin/bash
function project() {
cd /home/levicrews
python3 create_project.py $1
cd /home/levicrews/$1
git init --template=</home/levicrews/template-project>
git remote add origin [email protected]:levicrews/$1.git
git add .
git commit -m "Initial commit"
git push -u origin master
}
import sys
import os
from github import Github
path = "/home/levicrews/"
username = "" #Insert your github username here
password = "" #Insert your github password here
def create_project():
folderName = str(sys.argv[1])
os.makedirs(path + str(sys.argv[1]))
user = Github(username, password).get_user()
repo = user.create_repo(sys.argv[1])
print("Succesfully created repository {}".format(sys.argv[1]))
if __name__ == "__main__":
create_project()
Unfortunately, all my template files end up in the /.git directory of the new repo (from reading the docs on git init
, I realize now that that's the expected behavior of the --template
flag). What changes can I make to my scripts to replicate the "Use this template" behavior from GitHub?
Upvotes: 2
Views: 4597
Reputation: 28076
If template-project is not a git repository, you can just copy it before you init:
#!/bin/bash
function project() {
cd /home/levicrews
cp -a /home/levicrews/template-project $1
python3 create_project.py $1
cd /home/levicrews/$1
git init
git remote add origin [email protected]:levicrews/$1.git
git add .
git commit -m "Initial commit"
git push -u origin master
}
If template-project already has a .git dir, you will need to remove it:
#!/bin/bash
function project() {
cd /home/levicrews
cp -a /home/levicrews/template-project $1
rm $1/.git
python3 create_project.py $1
cd /home/levicrews/$1
git init
git remote add origin [email protected]:levicrews/$1.git
git add .
git commit -m "Initial commit"
git push -u origin master
}
If the git history is large, copying to .git and removing the .git folder again will be inefficient. You could use the following hack:
git archive --format=tar --remote=/home/levicrews/template-project master | tar xf -
Upvotes: 2