DEV Community

Using git to create a template project for repeated use

DeChamp on February 12, 2019

I am fairly good at using git, but I need some help setting up a pattern to make it easy to build a base project, that I can clone and then make in...
Collapse
 
jonathanhiggs profile image
Jonathan Higgs

Why not just clone the repo and delete the origin remote and push to a new remote that will be the actual project repo. Assuming you have template.git with the template and project.git as a bare repo:

$ git clone .../template.git project-name
$ cd project-name
$ git remote remove origin
$ git remote add origin .../project.git
$ git push origin master -u
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mkrl profile image
Mikhail Korolev

Depends on what do you mean by a "base project".

If you want to have a certain file structure covered, just git init then init an application template with whatever your stack is (something like npm init).

Or have the folder/file tree ready somewhere else and wrap it into a shell alias cp -r /home/templates/stuff/* . (also a cool opportunity to extend it to the function that inits your base project depending on an argument).

If you want to have a blank project with a remote ready to go, you can also either use hub or create a new, let's say, GitHub repo with curl -u 'username' https://api.github.com/user/repos -d '{"name":"my repository name"}'. Github API can be very handy. Can also be wrapped in a function.

There is so much stuff!

Collapse
 
kmehran1106 profile image
Mehran Kader

I think github currently has a feature which enables a dev to use a repo as a template for his other repo/projects.

Collapse
 
dorelljames profile image
Dorell James • Edited

Yes. I was hoping there’s an API we can use for this one but as of the time of writing, I can’t find it.

Spoke too soon, here it is folks: developer.github.com/v4/mutation/c...

Collapse
 
dechamp profile image
DeChamp

sweet! i'll have to take a look. Thank you

Collapse
 
emgodev profile image
Michael • Edited

Why not write a custom bash command?

or

From my understanding git deletes 'headless' branches, so rather than deleting .git/, you can just delete the branches and start a new branch & head. The old ones should just wither away, along with the data. Git man page says after 30 days it deletes that stuff.

Collapse
 
dechamp profile image
DeChamp

So I actually write quite a few bash scripts, enough where I keep a repo of them. I just was really hoping that git, has a native command.

Collapse
 
reegodev profile image
Matteo Rigon

Most frameworks create a CLI script that downloads the zip version of the repo, extract it in the specified directory and run needed commands ( git init, npm install, etc )

Collapse
 
dylanbstorey profile image
Dylan Storey

I wrote something to help me with this, it has some added sugar to make common tasks automated by default as well.

angreal.gitlab.io/