PhpStorm: Tasks & Contexts with Your Git Branches

PhpStorm

Switching context is a pain.

Not just because you need to mentally switch the complex web of ideas in your head. Think about all the physical files on different git branches you have to remember in order to answer a “quick question about task #123”. The controller, the view that holds the front-end template, the other templates, the data factory that makes the ajax call to the other controller, which uses a service class…

PhpStorm has a lot of great context links and shortcuts to help you navigate among all these, but it is still a royal pain whenever you need to put one set of files aside and work in a different area of the codebase. Particularly if you’re new on an existing project and still finding your way around, you can easily spend as much time opening all the correct files as you do working on the small request assigned to you.

I recently started working with the folks at Zamapps and am in this exact boat. We have a very well-organized system that makes things a lot easier to get up to speed, but I still spend more time than I like tracking down the files I need for a given task. We integrate tightly with Toggl and GitHub, and each task is standalone and on its own branch, but I was missing that “last mile” — linking a task with the files I use to work on it. When I finished something and pushed it up for review if there was even a small request to change a default or label I had to reopen the branch and track down the correct files where the work was done. How could I turn that all into a single, easy step?

Enter Contexts and Tasks!

These are two separate features but work well hand-in-hand. A Context is simply a group of open files with a name. You can find this under the Tools menu:

Tools | Tasks & Contexts | Save Context…

Save Context… will give you a popup where you can name it. Load Context… will popup a list for you to chose. You can try this out very easily — open a couple of files and save as a context. Now use Clear Context… to close them, then load it again from the list. One other little trick is to merge contexts by opening one, selecting a second one from the list and hitting SHIFT.

That find all by itself thrilled me, but it gets better! I mentioned that we use single-purpose git branches, meaning that to work on a task I need not just the files, but also to switch to a matching branch. This is easy with Tasks.

Tasks can be both tracker tasks and local tasks. The difference is primarily whether or not you are integrated with a task tracker application, but in both cases you are working with the local task, so we will focus on that part.

Back in the same Tools menu we see:

Tools | Tasks & Contexts | Open Tasks

When you are about to start a new ticket, use Open Task… to get this popup:

From Open Task…

There are several menus in this area, but they are all quite easy to understand. The Clear current context let’s you decide if you want to include the context files with this task; leave it unchecked if you want these to open each time you open this task. If you are starting a new task use the Create branch and specify what your work branch is (in my case, ‘master’ but you’ll probably have ‘develop’ or similar.) This will NOT fetch or pull first — you need to make sure to do that manually at the start of your work day. If instead you want to set this up to work with an existing branch, use the appropriate area below.

That’s very nice, but the real beauty of this comes later when you switch tasks:

Need to flip over to an earlier task to make that small change? Select the task and it will checkout your branch and load your files all in one! Close the active task when you’re finished and it give you the options to commit or merge your work!

With Close Active Task…

People always talk about how “that 3 second task done thousands of times over your career…” in terms of how much time it saves you, but for me it is more a question of how much annoying repetition I am able to cut out of my life. This is really just the tip of the iceberg — if you look at the things you have to do all day long just to keep your code and work organized and then search through what is possible in PhpStorm, you’ll find dozens more helpful bits like this.

Hope that helped!