Who are you writing that commit message for?

I read a lot of commit messages that make me wonder who the committer had in mind when they wrote it. If you don’t read commit messages yourself, I think that can make it even more difficult to think about who the audience is, or when someone would be reading those entries. Perhaps you’re writing for nobody, or work in a team that doesn’t value the metadata that a single sentence written in the moment can deliver.

Next time you write a commit message, try some of these suggestions as your imaginary audience:

  • Yourself, next week, when you finally get back to working on this thing and can’t remember where you were up to
  • Yourself, when you get a pull request review and can’t remember which commit something is in that needs to be removed
  • Yourself, debugging how this ended up like this, 6 months from now
  • Your colleague, eyeballing your work to see how you are getting on

Personally, I think of it as a note to myself. Like an alibi, if someone asks you what’s already been done, or what this commit that removes one specific line from a long config file. Yes, I worked as a git consultant for a while, the delete-a-single-line with the commit message “Fixed” is always the culprit!

Further reading: https://cbea.ms/git-commit/

Remove Accidental Content from Git Commit

When I teach git I try to show loads of good practice about how to inspect what’s staged before commit, etc etc. Good practice is good of course, but knowing how to undo a mess you created is better – and mistakes will happen. For example, accidentally including a node_modules directory in your otherwise excellent and useful commit. This post will walk you through how to fix the problem without losing any of your work. Continue reading

Counting Duplicate Commit Messages

When chatting about source control good practice the other day, I got a question about repeated git commit messages. In general, I would always advise that the same commit messages appearing multiple times in a project’s history is a definite red flag – and if I’m responsible for that repository I will probably make fun of you for doing it. Continue reading

Updating Local Git Repos When Upstream Moves

The scenario: the “main” repository of a git project has changed, either an organisation rebranded, a project got a new maintainer, or a fork became the acknowledged master. In Subversion, this was the svn switch command and git has an equivalent. It’s relatively easy in git to change your upstream – once you know how – so don’t be tempted to just delete your local repo and re-clone! We can do better than that :) Continue reading

Surviving Git Submodules

I’m a fan of submodules in git, but sometimes it seems like I’m the only one! After having worked with this approach on a few projects, I’m coming to the conclusion that, like so many other things, it’s easy when you know how! So, I tried to take my handwavy explanation of how to work with submodules, and turn it into a handy diagram for you … Continue reading

Git Won’t Check Out A Path It Autocompleted

One of my git repositories has developed a tendency to refuse to checkout a feature branch locally that exists on the remote repo. My git bash completion works, but then strange things happen! It turned out to be that I had two remotes with the same refspec, so I thought I’d write down the behaviour I saw and hopefully help someone else to fix this problem faster if they see it. Continue reading

PHP Learning Path from O’Reilly

I’m very excited to announce that some of my content is featured in the PHP Learning Path from O’Reilly. The Learning Paths are a good way to buy a bundle of content from different people on related topics, and the introductory pricing is always a good deal! Their newest offering is the PHP Learning Path, which has a video course on PHP and MySQL, my intermediate PHP Video course (they wouldn’t let me call it “all the things Lorna thinks PHP developers need to know” unfortunately!) and also my video course Git for Web Developers which has a bunch of PHP in it as well as my best git tips and tricks.

I think it’s a pretty well-rounded collection and it’s only $99 for a couple of weeks, so get the PHP Learning Path here and let me know what you think?

Git Pull Causes a Merge

If you type git pull and expect a fast-forward update, but get a merge instead, don’t panic! This usually happens when we’re collaborating on a branch with other people, and we’ve made changes on our local version of a branch, and someone else (or the other you, if you use git to sync between multiple dev platforms) has made changes to the remote version of a branch in the meantime. It also happens really frequently in teams where all commits are to the master branch … yet another reason to have a decent branching strategy.

All that’s happened is something like this:

$ git log --oneline --all --graph --decorate
* 054f163 (HEAD, branch1) Installation instructions for the application
| * 0ce808c (origin/branch1) Fixing template layout
|/  
* 927aad9 A random change of 731 to ideas2.txt

Since the last common commit, there are commits on your local branch, and the remote one. You could just let the merge go ahead but there are other options. You could also check out a new branch at this point, reset your tracking branch to the right place and then reapply your changes using cherry-pick or by rebasing and then fast-forward merging your branch. Continue reading

Count Changed Lines in Git

I have a favourite set of switches to git log, but today I wanted to answer the question “You deleted how much code today?” so I thought I’d share how I did that

git log --numstat will show you how many lines were added (first column) and removed (next column) per file, kind of a more scientific version of the --stat switch. And if you’re thinking of scripting this to gather stats, try it with --oneline as well, it’s easier to parse.