Over the past months we've had many people ask us about how to integrate and work with the CakeDC Git Workflow as we do. So, here are few tips and tricks on how to use our workflow for the git version control system, to help you keep a clean commit history on every CakePHP project.
Using ticket numbers in commit messages
One thing which becomes pretty obvious early on is how handy it can be to have commits tied to your issue tracker. The easiest way to do this is to use the issue or ticket ID in your commit messages.
$ git commit -m "#1234 changed this and that"
Following this pattern (starting the commit message with #ID), with a bit of git rebase sauce, makes the lines of your updates read like a story of your code.
Automation with use of commit-msg hook
It's possible that you may not using a git bash prompt, so you don't see the name of the current branch in console all the time. Alternatively, the dialog window of your preferred UI for git may not clearly disclose the name of the branch you're working on. Or, maybe you're just too lazy to write ticket number every time you commit something to the repository.
Using our workflow, where non-permanent branches are named after the related ticket number, you can create an executable file .git/hooks/commit-msg in the project's root directory with following content:
#!/bin/sh TICKET=$(git rev-parse --abbrev-ref HEAD | awk -F '/' '{print $2}') if [ -n "$TICKET" ]; then echo "#$TICKET `cat $1`" > $1 fi
From now on, the message of every commit to any branch with a forward slash in the name (in our case, its always the ID of the related feature/issue/hot-fix ticket), will start with a hash and the ticket number.
Also, lets assume that you're using our workflow for all of your projects. In that case, it could be better to install the git hook globally:
$ mkdir -p ~/.git_template/hooks ... add global hook(s) to this directory ... $ git config --global init.templatedir '~/.git_template'
After setting this up, every created or cloned repository will use this template directory (will make copy of its content). Existing repositories could be reinitialized with the new template by running git init in their root folders.
Temporary local branches
With the previous commit-msg hook in place, we can now go crazy with temporary local branches, while still maintaining a readable project timeline with references to tickets. Changes don't have to be committed directly to published branches, and here are some examples how to do that.
Local branch for debugging
Debugging sometimes takes a lot of code to be written, and sometimes a few of the changes made during debugging need to be merged and propagated.
$ git checkout -t origin/feature/1234 $ git checkout -b debug/1234
Now we have local branch debug/1234 based on feature/1234. Lets do some work in loop, like adding tests, debugging code, applying fixes... everything in commits as small as possible, with meaningful commit messages:
$ git commit -m "Added debug code for debugging this and that" $ git commit -m "Added test case proving this and that" $ git commit -m "Tried to fix this and that"
The history of the debug/1234 branch will look like the following:
#1234 Added debug code for debugging this and that #1234 Added test case proving this and that #1234 Tried to fix this and that
We'll still have the feature branch free of debugging related code. You can then merge to this branch to keep it updated with progress from feature branch, but you can also commit fixes to it that you'll want to publish, and then use cherry-pick for including them elsewhere.
And if you changed something in your debug/1234 branch, that you'll want to still see in the feature/1234 branch, then simply perform the following:
$ git checkout feature/1234 ... merge, cherry-pick, rebase ... $ git push
Then, finally some clean up, like so:
$ git branch -D debug/1234
We hope you found something useful here, to make working with the workflow more engaging, and help you keep building awesome applications with CakePHP.