Tell Git Your Intentions

What’s your Git workflow?

I’ll show you what mine was for almost 5 years, and then a few weeks ago, it all changed.

To keep things simple, let’s assume I’ve got some unstaged changes in my repo and all of the changes are good to go for my next commit. I always start with git add -p to review all changes one small chunk at a time:

Old Git workflow — new files made things ‘complicated’

Take a look at my flow when there’s just one new file involved. Did you notice how things get a lot more awkward? I also lose the benefits of partially adding changes with new files — I’m left with scanning over all of the changes after I’ve added them. The flowchart above assumes the happy path. Often, I’ll want to only commit some things and not others, so I have to go back and check out some of the added changes.

A few weeks ago, I was reviewing a cached diff after adding some new files, and I realised that I’d paginated through the changes without carefully reading them. I was disappointed in myself for letting the ball slip and frustrated with the workflow. This wasn’t the first time I’d caught myself doing that.

People often talk about how powerful Git is. Given that, this workflow seemed unnecessarily awkward. There must be a better way. So, after 5 years of using that same workflow, I decided to turn to Google to look for that better way. I typed in “partially adding new files git”, and that’s when I discovered my new best friend:

-N, — intent-to-add Record only the fact that the path will be added later. An entry for the path is placed in the index with no content. This is useful for, among other things, showing the unstaged content of such files with git diff and committing them with git commit -a.

This means that if I had a new file ready to be committed, I could type in git add -N <new_file> and then do git add -p against that file.

So, here’s what my new Git workflow looks like:

A streamlined new Git workflow

Isn’t that much better?

With the --intent-to-add option, all of my files are now subject to VIP (that’s Very Important Partial add) treatment, meaning fewer errors and better crafted commits.

What do you think? Had you heard of the ‘intent-to-add’ option before?

If you’re looking at my workflow and thinking of a new way to step it up even more, then please get in touch with me. I’m @nodunayo on Twitter.