Using git to create a template project for repeated use

twitter logo github logo ・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.

twitter logo DISCUSS (5)
markdown guide
 

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!

 

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
 

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.

 

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.

 

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 )

Classic DEV Post from Mar 26

Have you ever quit a job without anything else lined up?

If so, what lead to this decision? ...

DeChamp profile image
Just a coder and a dad. I love my family and I love to code!!!! started coding at 11, so I have 25 years under my belt. Still love learning about it every day. Follow me on instagram @codeFiend

Customize your DEV feed to keep up with the technologies you care about.

Get Started