Git Partial Commits

Cover Image for Git Partial Commits

Learning to use partial git commits may be for you if one or more of the following apply:

  • You prefer command line keystrokes to mouse clicks in a Git GUI and want to stage individual chunks of code across multiple files
  • You contend with deep intrinsic desires to contribute discrete change-sets to a git repository using contextually relevant commits
  • Your refusal to clean up the thousands of local branches you've accumulated through years of contributions to a large repository causes your Git GUI of choice to crawl to a crash
  • You're a developer with a penchant for working software and maintainable code bases

What is a partial commit?

Git partial commits allow contributors to encapsulate a single hunk* in a single file, disparate hunks in a single file, or many disparate hunks across many files into a single commit. This is useful for developers with ambitions to contribute discrete yet working change-sets in a single commit.

*Tangential aside -> a hunk in the land of Git has nothing to do with beefcakes or Fabio. It instead refers to a "piece of change." Git assumes your code conforms to the likeness of shirtless males sporting muscular physics. Unfortunately, code hunks are rare in the wild; most of us are far more likely to encounter chunks wedged amongst chunks in our repos.

How to git commit, partially

  1. Select a small story from your agile sprint
  2. Refactor completely unrelated areas of the code base
  3. Implement the original, small story
  4. Recognize and meditate on scope creep
  5. Leverage partial git commits to contribute a small change that everyone agrees has value
  6. Create a new story, a new branch, and force your team to review a massive change that you've decided adds value

Putting aside more developer humor provided in jest, using partial git commits is a straightforward process.

Make some changes, and type

git add -p

Depending on your machine and environment, you should see some chunks and something akin to

Stage this hunk [y,n,q,a,d,e,?]?

Type ? to see the options in detail

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 hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
e - manually edit the current hunk
? - print help

Select your desired option, continue following the prompts, and craft a wonderful commit 💫