How can I use git for projects templates?

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.


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!

Need Your Help

What is a “memory stomp”?

c++ c memory memory-management

I just came across this blog post which mentions “stomping memory”: