Keeping the Framework for Your Application Up to Date with Git
I recently started building a web application using the Laravel Framework. But, since I was building the application in my spare time, and a new version of Laravel was being released each week, I knew the version of the framework I started with would not be the same when I finished. Conundrum: I needed a simple way to stay on top of any security, bug, or feature related updates, without having to download and drag and drop folders.
To solve this problem, I'll be harnessing the power of Git. In a nutshell, we'll have two branches: master and framework. The master branch will be our application. The framework branch will be the Laravel framework that is powering our app. (Laravel's repository can be found here) When an update is made to Laravel, we check out the framework branch, pull in the most recent version of the framework, then merge those changes with master. Pretty straightforward.
A few quick notes: I'll be using the Laravel Framework in this example. It can just as easily be applied to CodeIgniter, Fuel, or another non-PHP Framework. It's good to remember that even though the framework repository may be hosted on Github, your application's repository does not have to be. There are plenty of online repository hosting services (Beanstalk and Bitbucket to name two). As long it's Git, it will work. Lastly, I added the framework repository a month into starting the project. This means I was already a few versions behind the current release. The only conflicts I ran into when merging was with the Readme and a single Controller file. These were very easy fixes because I knew what to do. If you're not sure, check the forums or IRC for the framework you're working with.
Adding the Framework
First, let's add the framework's repository (In Laravel's case, simply git://github.com/laravel/laravel.git), which will give us the ability to reference that repository when running our git pull/push commands.
tommy in ~/Sites/starter-app on master ± git remote add laravel git://github.com/laravel/laravel.git
You'll notice if you run git remote -v you can see which we have two repositories you are able to pull from and push to.
tommy in ~/Sites/starter-app on master ± git remote -v laravel git://github.com/laravel/laravel.git (fetch) laravel git://github.com/laravel/laravel.git (push) origin email@example.com:tommymarshall/starter-app.git (fetch) origin firstname.lastname@example.org:tommymarshall/starter-app.git (push)
Next we want to create a separate branch named framework that matches and pulls in the most recent version of Laravel. However, simply doing a git pull framework master will throw an error. We'll first need to gather some information from the framework repository (Basically just gathering all the commits and meta information) so that when we do run git checkout -b framework laravel/master, git will actually know what to pull.
tommy in ~/Sites/starter-app on master ± git fetch laravel
Now we can safely run:
tommy in ~/Sites/starter-app on master ± git checkout -b framework laravel/master
This creates and checks out the framework branch, and pulls in the master branch from the laravel repository.
Now let's go back to our master branch and merge framework (Laravel) with master.1
tommy in ~/Sites/starter-app on framework ± git checkout master Switched to branch 'master' tommy in ~/Sites/starter-app on master ± git merge framework
Now we need to start tracking the new framework files and get them ready to be committed to our repository. So now let's run:
tommy in ~/Sites/starter-app on master! ± git add .
Each time I update my application with the updated version of the framework, I always do so as a separate commit. That way, in case my application is not compatible with the latest release of the framework, I can always restore the previous commit.
tommy in ~/Sites/starter-app on master! ± git commit -am "Merged Laravel v3.2.7"
And finally, let's push this to our application repository:
tommy in ~/Sites/starter-app on master ± git push origin master
You've now successfully updated your application's framework to the most recent version! Now, whenever an updated version of the Laravel framework becomes available, I simply have to check out the framework branch and merge it into master.
tommy in ~/Sites/starter-app on master ± git checkout framework Switched to branch 'framework' tommy in ~/Sites/starter-app on framework ± git pull framework master From git://github.com/laravel/laravel * branch master -> FETCH_HEAD Already up-to-date.
If it's already up-to-date (Like we are above), then I can go back to working on my application. If there was an update, then I would simply need to check out the master branch and run: git merge framework
I make it a habit to check out my framework branch and do a quick git pull to make sure I am up to date. Keeping the framework powering your application secure and current are very easy using this method.
Also, there are a number of instances (especially when dealing with frameworks that interact with databases) that may require more than simply updating system files. Always consult the readme of the framework you're working with to check if there are additional changes that must be made.
1 At this point you may run into a merge conflict. This normally happens with changes in the readme file if you modified it. If it's something major, or something you're not confident in changing, hop in the framework's IRC or check the release notes for notes regarding updating. Remember, you can always reset to a previous state if you're not sure.