DEV Community

Mahfuzur Rahman Saber
Mahfuzur Rahman Saber

Posted on

Blade Component and Directive for Demo App - 2 (Pull like package using Git and GitHub)

In the last blog I have explained how you can create a quick login blade component for your demo app. Also said that I will share a trick so you don't have to copy paste all codes in your every project. So let's take a look how we can achieve this.

Before push prepare structure

.
├── app
│   ├── Http
│   │   └── Controllers
│   │       └── Auth
│   │           └── LoginAsDemoLayout.php
│   └── View
│       └── Components
│           └── Demo
│               └── LoginAsController.php
└── resources
    └── views
        └── components
            └── demo
                ├── buttons
                │   └── login-as.blade.php
                └── login-as-demo-layout.blade.php
Enter fullscreen mode Exit fullscreen mode

Why structuring files is important ? Basically when we pull we actually want those specific files to add in our project, which means if your file name matches with existing project files it will not pull instead it will create git conflict. Well this blog is not in-depth about git so I am not going to discuss all. To know more about git you need to do a bit of self study.

Push code to GitHub

  • First create a repo in GitHub

repo-name

  • Initialize git, add and commit your files
git init

git add .

git commit -m 'your message'
Enter fullscreen mode Exit fullscreen mode
  • connect your git repo
git remote add origin "your git url"
Enter fullscreen mode Exit fullscreen mode

Image description

  • push your code
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Pull from GitHub

Now you can pull these files from your any application by simply adding the upstream and pull.

  • add upstream
git remote add demo-comp https://github.com/SaaberDev/demo-app-component.git
Enter fullscreen mode Exit fullscreen mode

You might have noticed I added the upstream but instead origin I named it as "demo-comp", why ? Because, we already have an upstream named origin where we are pulling.

  • After adding upstream simply use this command to pull.
git pull demo-comp main
Enter fullscreen mode Exit fullscreen mode

Sometimes you may face issue like "refusing to merge unrelated histories" to solve this add --allow-unrelated-histories

git pull demo-comp main --allow-unrelated-histories
Enter fullscreen mode Exit fullscreen mode

I hope this blog will help you to save your time. From now on you can reuse your code by simply creating a reusable repo and pulling it. This is a simple example but you can use your own technique.

Top comments (0)