Author avatar

Vivek Kumar

Working with Git and Boilerplate

Vivek Kumar

  • Nov 26, 2019
  • 8 Min read
  • 22 Views
  • Nov 26, 2019
  • 8 Min read
  • 22 Views
Web Development
React

Introduction

For people working online, the creation of new projects with limited resources is always a challenge. One way to maximize time and resources is to use contracts, statements, and computer codes in the form of written texts that can be used again in new applications and contexts without bringing much change to the original. These texts are called boilerplates.

Boilerplate projects are comprised of commonly used libraries and features. In this guide, we will learn the right Git process in order to utilize boilerplates as the foundation of a new project, as well as how to switch a repository to a boilerplate.

When you use Git to create new projects from an existing boilerplate repo, there are two main options to consider.

  • Forking boilerplate to a new project and then initiating the project. Note that we don’t have the option to pull the new project changes back into the boilerplate.

  • Cloning out the boilerplate and then updating the new remote origin in the form of a new, clean repo.

We’ll learn about both of these topics.

Switching a Repo to Boilerplate

Consider a scenario where you are working on a project within a git repository, rep1, and you use one of the many available boilerplate projects, rep2, to help you further build configuration.

Problem Statement

Your task is to merge these two repositories in such a way that you retain all the history of rep1 without losing the ability to, later on, pull or merge updates from rep2.

Solution

You can merge the repos using these steps:

  1. Use the rep2 as a remote to rep1. git remote add rep2 rep2.git
  2. Pul the files from your new remote. git pull rep2 master --allow-unrelated-histories
  3. Resolve all the merge conflicts. git mergetool
  4. Now, make sure you have reset the HEAD to prevent obtaining the full history ofrep2.git reset HEAD`
  5. Add all changes to be staged for the next commit. git add *
  6. Commit the changes. git commit -m "Pull rep2 into your rep1"
  7. Lastly, push the branch. git push

These steps complete your task when a boilerplate and an additional repo are involved as per the problem statement. You can also start with step two for future updates.

Using an Existing Boilerplate Repo

Sharing boilerplate code through codebases is a relentless practice in software development. The process of bootstrapping the latest project with preferred tools and directory structures helps programmers move towards more efficient and less manual configuration.

Problem Statement

How can you create new projects from an existing boilerplate repository in GitHub?

Best Solution

Now we’ll talk about the repository templates that can help you with boilerplate code management as well as distribution. To begin, just mark a repository as an ideal template, and you are instantly ready to produce new repositories consisting of all the files and folders associated with the template.

Note that each temple repository acquires a new URL endpoint termed as /generate, allowing the distribution of your template in a more efficient manner. You can further refer to the GitHub blog for more explanation of generating new repositories with repository templates.

Below are a few alternative ways of achieving the same thing.

Alternative Solution 1

By taking into account the fact that your project is to be stored on GitHub (in a specific template in the boilerplate GitHub project), you can:

  1. Build a new empty repo on GitHub with no link to a boilerplate or need for a fork.
  2. Create a clone of it.
  3. Process the cloning of the boilerplate in a distinct location of the disk.
  4. Copy the template via local boilerplate clone into the GitHub repo, which is cloned locally.
  5. Use (git commit, git add) to push the particular template back to the new GitHub repo.
  6. Commence work on your project from that point.

It is worth mentioning that boilerplates might contain multiple templates to pick from. In this particular case, we are talking about a particular template where there might not be a requirement to reset a remote origin URL.

Alternative Solution 2

You can also use the hub in order to build a one-liner, which will effectively impact the new repo (remotely or locally) on the basis of seed repo:

1
2
3
4
5
clonefork() {
    hub clone "$1"
    cd "${1##*/}"
    hub fork
}
bash

In the shell:

1
$ clonefork user/seed-repo
bash

Through cloneforking the seed repo, you can also get access to a network graph displaying all the repos created out of it.

Alternative Solution 3

Another solution is to create a repo for a new project in your terminal. The following is a dummy function that has been created as a demonstration. The function is created for one terminal, and whenever a new project is commenced, you just need to to build a repo on GitHub, fetch the precise project name and type bp newProject on your terminal. Here, bp stands for boilerplate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bp () {
# Clone the boilerplate project from GitHub
git clone https://github.com/username/boilerplate.git;

# Rename the boilerplate folder to newProject
mv boilerplate "[email protected]";

# Access the newProject folder
cd "[email protected]";

# 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/"[email protected]".git;
git push -u origin master
}
bash

Note: Project Forking on GitHub

Note that on GitHub, the process of forking the project is identical to Git repo cloning. Pushing changes back to the _boilerplate_ repo is not advisable in this case. However, you can go with the rebasing or merging in order to extract updates to the boilerplate repo for the projects derived out of it. If that’s not what you want, then instead of going with Git, simply create a recursive copy of the directory and start from there.

Conclusion

The processes of switching a repo to a boilerplate and creating new projects from an existing boilerplate are not complex with the correct approach. In this guide, you learned how these things can be achieved via a single template and how cloning is done for new repositories. We also threw some light on what should and should not be done in forking on Git and discussed a dummy function for starting new projects from a single terminal.

0