DEV Community


Using git to create a template project for repeated use

dechamp profile image DeChamp ใƒป1 min read

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 into it's own project for reuse.

I've researched quite a bit but no real clear answer on this other than doing a clone followed by removing the .git folder.

I feel there has to be a better way.

Discussion (9)

Editor guide
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
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' -d '{"name":"my repository name"}'. Github API can be very handy. Can also be wrapped in a function.

There is so much stuff!

kmehran 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.

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:

dechamp profile image
DeChamp Author

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

emgodev profile image
Michael • Edited

Why not write a custom bash command?


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.

dechamp profile image
DeChamp Author

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.

matteorigon 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 )

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.