How can I use git for projects templates?
Asked Answered
C

3

24

I have some template/starting point code that I reuse across projects. While working on the new project, I always want to add and change things in the template. Develop the template alongside the project, I guess.

Some of my additions are project-specific and should no be committed to the template. Others should.

I bet I could use git for this, but I'm not sure how. I have a git repository for the template and one for each project. I would like some, but not all, commits I make to be pushed back to the template. Can I make a subset of files that don't get committed back? Should I work on project-specific things in one branch and the template in the master?

I really appreciate any insights. My google-fu yields little.

Cracked answered 6/4, 2011 at 20:50 Comment(3)
Branch and merge a central template branch as needed?Isbell
possible duplicate of How can I get track of several projects in Git that share common code?Berga
On GitHub, since June 2019, you now have repository templates: See "Can I create a new repository out of an existing repository but rename it?".Yettie
S
15

I would recommend creating a branch specifically for any template changes you want to make. But make sure you start it from one of the earlier commits, so you don't get project-specific changes to your template. Something like git checkout -b template_changes EARLY_COMMIT_SHA. Then any template changes you have already made, you can cherry-pick in: git cherry-pick TEMPLATE_CHANGE_SHA.

Any future changes to the template you would like to make could be done on the template branch and merged into other branches of the project; but if you are far along enough, you may want to switch to the template project.

To push your template changes from the project into the template git repository, you could do git format-patch SHA_FROM_WHERE_TEMPLATE_BEGAN. This will create a bunch of patch files that you'll have to copy to the template repo and run git apply 0001-... 0002-... to apply the patches, but that's not very fun.

A more git-fu thing to do would be to add the template repo in the project repo git remote add template /path/to/template/repo. You may have to play with the -t or -m options or the command git remote set-head template template_branch:master (or something) if the template branch exists. Then you should be able to push your changes to the template repo with git push template template_branch. If the template_branch's head isn't set property, you may have to git push template template_branch:master.

Hope this works out for you. And have fun!

Senhauser answered 7/4, 2011 at 1:31 Comment(1)
Thank you. I think this points me in the right direction for some more research.Cracked
W
0

I was asking myself the same question. I'm working with Django & Flask projects mostly, which brings about endless variations (Flask/MongoDB/Vues.js etc.). I like to work with Docker as well. So I've templated the main variations that I tend to use, as Docker projects. But especially with Django, you have a few things you almost always end up doing anyways (running startproject/startapp/collectstatic).

I essentially use this answer about having different upstream for different branches of a same repo.

To start a new project, what I like to do:

  1. [create new directory locally]
  2. [create empty project repo on github]
  3. git clone [template repo url]
  4. git checkout -b work
  5. git remote add project [project repo url]
  6. git push project work
  7. git branch -u project/work work

So essentially, I clone my template repo in my new directory. That gives me a branch master that tracks my template repo (3). Then, I immediately create a work branch (which I'll actually use for the project) (4). I add a new remote (which is my empty project's repo on github) (5). And this is key - I push project work, which creates a first branch on the new (empty before that) project repo.

Then finally, to make that permanent, I set the branch work to track the branch work on my project's repo.(6)

Branch master already tracks branch master on the template repo, so you've nothing to do there.

Then if I'm on branch work and decide that I want to update my template, I can simply commit whatever I'm doing, switch branch to master, do the change & commit push to my template. It'll push on origin/master. Or I can wait & pile up a few minor changes to my templates, test them out & then only update the template.

If I'm far enough into the project (and thus unlikely to make further template changes), I can simply stop tracking origin/master & reconfigure the project to have a master branch, or just keep work as my master.

Waggish answered 28/1, 2021 at 16:48 Comment(1)
I'm pretty sure you could merge steps 6 and 7 by doing git push -u project work.Hermes
G
0

If you use yarn or npm you can have a template repository with his own commit history , and later, fork it for a new project, and with one (tricky) command, commit history is resseted in the new forked project.

i use yarn gitinit

package.json:

    "scripts": {
      "gitinit": "git checkout --orphan tmp-master && git add -A && git commit -m 'inicial' && git branch -D master && git branch -m master && git push -f origin master "
    }

If you don´t use npm or yarn,a custom script file in the template can do the same work

Geminate answered 28/9, 2021 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.