17

I've created a Laravel "boilerplate" project which contains my commonly used features/libraries.

What's the correct git process for utilising this boilerplate as a basis for new projects? I can think of two options:

  1. Fork boilerplate to newproject and begin. I don't have any intention of pulling newproject changes back into boilerplate.
  2. Clone boilerplate and update the remote origin to a new clean repo.

I'm leaning towards the 2nd option, as it feels like a nice clean split from the boilerplate code. Will I be losing any cool git possibilities by doing this?

Matt Stone
  • 3,705
  • 4
  • 23
  • 40
  • 6 years later, GitHub does propose that feature! "Repository templates" are a reality. See [my edited answer below](https://stackoverflow.com/a/14890314/6309). – VonC Jun 06 '19 at 20:24

7 Answers7

20

Update June 2019, 6 years later:

See "Generate new repositories with repository templates "

Sharing boilerplate code across codebases is a constant pattern in software development.
Bootstrapping a new project with our favorite tools and directory structures helps programmers go from idea to “Hello world!” more efficiently and with less manual configuration.

Today, we’re excited to introduce repository templates to make boilerplate code management and distribution a first-class citizen on GitHub.

To get started, all you need to do is mark a repository as a template, and you’ll immediately be able to use it to generate new repositories with all of the template repository’s files and folders.

https://github.blog/wp-content/uploads/2019/06/repository-template.gif

Every template repository gets a new URL endpoint called /generate that allows you to distribute your template more efficiently


Original answer 2013:

Considering you will be storing your project on Github from one of the templates included in boilerplate GitHub project, I would:

  • Create a new empty repo on GitHub (no link with boilerplate, no need for a fork)
  • clone it
  • clone boilerplate in a separate location on your disk
  • copy the right template from the local boilerplate clone to your new locally cloned Github repo
  • (git add, git commit) Push that specific template back to your new Github repo.
  • start working from there
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 21
    another option would be to clone `boilerplate`, delete `.git`, then `git init`, `git add .`, `git commit` – aragaer Feb 15 '13 at 08:43
  • @aragaer don't forget that `boilerplate` could contain many templates for you to choose from (that may not be the case here), plus you would need to reset the remote `origin`url. I would argue my way is easier. – VonC Feb 15 '13 at 08:44
  • Deleting `.git` resets everything git. But I agree about having several templates. – aragaer Feb 15 '13 at 09:19
3

"Fork" isn't a Git concept. Are you confusing Git with Github? If you mean forking the project on Github, then it's the same as cloning the repo, and cloning sounds like what you're looking for. You'd just never push changes back to the "boilerplate" repo. You could, however use merging or rebasing to pull in updates to the boilerplate repo into the projects that spin off of it. If nothing like that is desirable, then why even do it with Git? Just make a recursive copy of the directory, and start from there.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • This requires you to manually set the upstream-- lest you push your changes to your boilerplate repo! https://help.github.com/articles/changing-a-remote-s-url/ – Brandon Kuczenski Dec 29 '16 at 08:02
3

Nothing wrong with @VonC's answer, but you can alternatively use hub to create a one-liner that effectively inits a new repo (locally and remotely) based on your seed repo:

clonefork() {
    hub clone "$1"
    cd "${1##*/}"
    hub fork
}

In your shell:

$ clonefork user/seed-repo

By "cloneforking" your seed repo, you also get a network graph showing all repos generated off of it.

joyrexus
  • 390
  • 1
  • 3
  • 8
2

Following @VonC's indications, I created a function for my terminal and pasted it in my alias.

Every time I start a new project, I create a repo on GitHub, grab the exact project name and then just type on my terminal 'bp newProject'

bp () {
# Clone the boilerplate project from GitHub
git clone https://github.com/username/boilerplate.git;

# Rename the boilerplate folder to newProject
mv boilerplate "$@";

# Access the newProject folder
cd "$@";

# Delet the .git folder
rm -rf .git;

# Initialize git
git init;

# Add;
git add .;

# Commit using 'initial setup; as  message;
git commit -m 'initial setup';

# And push it 
git remote add origin https://github.com/username/"$@".git;
git push -u origin master
}
Mingo
  • 103
  • 1
  • 9
1

I just published the CLI tool, thats creating new project based on existing repo and even remotely creating your own on Github. https://github.com/theJacobDev/binit

NotAJohnDoe
  • 125
  • 12
1

It's not exactly what you're asking, but it may also be helpful to solve similar issues.

There is a tool to bootstrap Github repository from yaml config file https://github.com/g4s8/gitstrap

It can:

  1. Create new repository on Github
  2. Sync with local directory
  3. Apply templates (such as README with badges, CI configs, LICENSE stuff, etc)
  4. Configure webhooks for Github repo
  5. Invite collaborators

so you can write templates for similar projects instead of creating boilerplate repo, and apply them using configuration file when creating new Github repo with that tool.

Kirill
  • 7,580
  • 6
  • 44
  • 95
0

I, also, created a tool to solve this kind of problem. I developed a concept with dictators that dictates parts of the filesystem.

A dictator is configured with the template/boilerplate -project. Like this example: https://github.com/tomasbjerre/dictator-react-boilerplate

The dictator configures dictatables, like:

{
  "message": "Copy react-boilerplate",
  "actions": [
    {
      "beSupersetOfJsonFile": "react-boilerplate/package.json",
      "target": "package.json"
    },
    {
      "copyFrom": "react-boilerplate",
      "target": "."
    },
    {
      "haveLineContaining": ["*.tgz"],
      "target": ".gitignore"
    }
  ]
}

This dictator can be run as a command line tool:

npx dictator-react-boilerplate

Or have it run with your build tool, perhaps like:

{
  "name": "react-boilerplate-example",
  "version": "1.2.3",
  "description": "asdasasd",
  "scripts": {
    "prepare": "dictator-react-boilerplate"
  },
  "devDependencies": {
    "dictator-react-boilerplate": "0.0.8"
  }
}
Tomas Bjerre
  • 3,270
  • 22
  • 27