Laravel – My Journey – Part 6 – A Basic GitHub Workflow

Getting Started with Laravel and Github

Perhaps it’s just me, but I initially struggled with integrating GitHub into my Laravel workflow. I wasn’t sure whether I should create a repository on GitHub.com first, then install my Laravel application and then push it to GitHub. Or should I install my application first or what? Anyway, through trial and error, I established a VERY simple workflow that seems to work for me.

As ever, the leitmotif for this series is: take everything I say with a pinch of salt. This is a BASIC workflow and does not cover all of the possible scenarios that you may encounter in your own workflow. Don’t forget to read the disclaimer!

Previous articles in this series:
Important Note/Disclaimer
This is not a tutorial, nor an example of best practice. The techniques described in this article are not to be taken as efficient or secure. At the time of writing, I am a relative newcomer to Laravel, object-oriented programming, and the MVC paradigm. I won't be explaining every detail. I don't know all the theory. I'll probably get stuff to work and not know how I did it. I'll probably write things that are clearly untrue or, at best, inaccurate. This is me just finding my way ...

Github logo

Getting Started

Via your Terminal application, install a new Laravel application:

$ laravel new my-new-project

Create a new project on GitHub.com:

Github - Create Project
Github – Create Project

Don’t add a README.MD or license file.

Back in your Terminal application, navigate to your Laravel application directory:

$ cd my-new-project

Initialise an empty Git repository:

$ git init

You see a message like:

Initialized empty Git repository in /Users/your.user.name/Sites/my-new-project/.git/

Add your Laravel application files to the repository:

$ git add .

This will add ALL of your new files.

Commit files to the repository with a message:

$ git commit -m ‘Install Laravel’

You should see a message like:

[master (root-commit) 6a21ffa] Install Laravel
 80 files changed, 13093 insertions(+)
 create mode 100644 .env.example
 create mode 100644 .gitattributes
 create mode 100644 .gitignore
 create mode 100644 app/Console/Kernel.php
...

Initial Push to GitHub.com:

$ git remote add origin https://github.com/yourusername/my-new-project.git
$ git push -u origin master

You should see a message like:

Counting objects: 109, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (92/92), done.
Writing objects: 100% (109/109), 223.94 KiB | 5.46 MiB/s, done.
Total 109 (delta 9), reused 0 (delta 0)
remote: Resolving deltas: 100% (9/9), done.
To https://github.com/yourusername/my-new-project.git
 * [new branch] master -> master
Branch master set up to track remote branch master from origin.

Back on GitHub.com, visit your project page and you should see the Laravel application files listed:

GitHub - Project Files
GitHub – Project Files

Now you have a local repository on your computer and a remote repository on GitHub.com. Good work. But that’s just the first step.

Subsequent commits/push

You’ve made some changes to your Laravel application and now want to push them to Github.

Back in your Terminal application, ensure you are in your Laravel application directory:

$ cd my-new-project

Check the status of your changed files (optional):

$ git status

You should see a message like:

On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)

 app/Http/Controllers/PostsController.php
 app/Post.php

nothing added to commit but untracked files present (use "git add" to track)

As instructed by the above message, you can add all changed files:

$ git add .

OR individual files:

$ git add NameOfChangedFile.txt

Commit changes with a message:

$ git commit -m ‘Message’

Push changes to GitHub.com:

$ git push

Your changes have now been synchronised to your GitHub repository.

Clone an existing GitHub repository

These steps can be used to clone an existing GitHub project to your local machine. I often work on a project on more than one computer, so I can clone a repository to, for example, my laptop using the following steps.

Navigate to the project on GitHub.com and locate the ‘clone or download’ button.

Clone a repository
Clone a repository

Copy the repository URL e.g. https://github.com/yourusername/my-other-project.git

From your Terminal application, navigate to the directory where you would like the cloned directory to be created.

Enter (paste):

$ git clone https://github.com/yourusername/my-other-project.git

The repository will be created in a directory (in this case) called ‘my-other-project’.

For a Laravel application, you will need to do a composer install and php artisan migrate.

Fetching changes from a remote repository

Any changes that have been made to a remote GitHub repository by other users, or by yourself on a different computer, can be fetched by using:

$ git fetch
$ git pull

If there are no changes to pull from the remote repository, the $ git fetch command will not return any change information.

If there are changes, they will be listed, e.g.:

remote: Counting objects: 8, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 8 (delta 5), reused 8 (delta 5), pack-reused 0
Unpacking objects: 100% (8/8), done.
From https://github.com/yourusername/my-other-project
b877781..0c3d5a9  master     -> origin/master

You can then pull down the changes from the remote repository using the $ git pull command. Information regarding the changed files will be displayed, e.g.:

Updating b877781..0c3d5a9
Fast-forward
composer.json                       |  3 ++-
tests/Feature/CreateThreadsTest.php |  2 +-
tests/utilities/functions.php       | 11 +++++++++++
3 files changed, 14 insertions(+), 2 deletions(-)
create mode 100644 tests/utilities/functions.php

Your local repository will have been updated with the latest changes from the remote GitHub repository.

Why don’t I just use a nice Git application?

You can of course, but it’s nowhere near as ‘War Games’ as using the command line interface …

Leave a Reply