Why `git add -p` Should Be Your New Favourite Git Command

Why `git add -p` Should Be Your New Favourite Git Command

For those of us who live and breathe code, we know that the devil is in the details. Whether it's developing a new feature, fixing a bug, or refining an algorithm, the smallest changes can have the greatest impact. As a cybersecurity expert boffin who occasionally moonlights as a programmer, I can't overstate the importance of careful, intentional code commits. This is where git add -p comes into play.

I recall being introduced to git add -p by another student during my honours year at university which involved writing vehicle traffic simulations in Matlab. Since that moment, the way I have approached code commits has drastically changed for the better, resulting in clean & logically grouped code commits and making it easier to provide a relevant commit comment (because we've all picked up our code months later and have forgotten what one or two functions do 😉).

What is git add -p?

Before we delve into the magic of git add -p (and it *is* magic) let's quickly recap some Git basics. Git is a version control system, widely used in software development. The command git add is used to stage changes for the next commit, effectively telling Git which changes you want to include in the next snapshot of your project.

The -p flag, short for patch, transforms this command into a powerful tool that offers a more granular approach to version control. Instead of adding all changes in a file to the staging area, git add -p allows you to interactively choose hunks of changes in the file to add.

Why is git add -p a powerful tool?

Here are three key reasons:

1. Improved Commit Clarity

git add -p promotes more meaningful, atomic commits. Instead of committing all changes made in a coding session, you can pick and choose which changes to commit. This helps create a clean, logical commit history. A commit can contain a single bug fix, a new feature, or a code refactor, making it easier to review changes, identify bugs, and understand the evolution of the project.

2. Enhanced Code Review

When changes are logically grouped, it makes the code review process smoother and more effective. Reviewers can focus on one specific change at a time, making it easier to spot potential issues. git add -p is an excellent tool for cultivating this discipline.

3. Mitigation of Security Risks

As a cybersecurity professional, this is particularly important to me. Often, developers might add sensitive information to the codebase for testing or debugging and forget to remove it before committing. With git add -p, you can review every change before it's staged, helping avoid accidental commit of sensitive information, such as passwords, API keys, or personal data.

Using git add -p in your workflow

Using git add -p is straightforward. When you've made changes to your files, run git add -p in your terminal. Git will then present you with chunks of changes and a prompt:

For each change, git will prompt on whether you want to "stage this hunk". Some common command options are listed below

  • y - stage this hunk
  • n - do not stage this hunk
  • q - quit; do not stage this hunk or any of the remaining ones
  • a - stage this and all the remaining hunks in the file
  • d - do not stage this hunk nor any of the remaining hunks in the file
  • ? - print help

Type y to stage the current hunk, n to not stage it, q to quit and leave the rest unstaged, and ? to get a help message.

Level up

Not satisfied? Level up further with these advanced git add -p options:

  • g - select a hunk to go to
  • / - search for a hunk matching the given regex
  • j - leave this hunk undecided, see next undecided hunk
  • J - leave this hunk undecided, see next hunk
  • k - leave this hunk undecided, see previous undecided hunk
  • K - leave this hunk undecided, see previous hunk
  • s - split the current hunk into smaller hunks
  • e - manually edit the current hunk

Conclusion

In conclusion, git add -p is a simple but powerful tool that can greatly improve your coding workflow and your project's health. By promoting careful, granular commits, it enhances code clarity, facilitates code review, and helps prevent security mishaps.

So next time you're about to git add ., remember the -p flag.

It could make all the difference. 😎

Credits/References: