Oscar the owl and Vershd logo

Achieve a Git Partial Commit of a File

13th April 2020

This is part of a Git Tips series. For the complete set for beginners and intermediate Git users, see our white paper.

The Problem

You’ve added a list of your top 100 albums of all time to a file. Jane is going to look through it, and pass back her own list. In the meantime, you’ve made a couple of changes to the files for work related reasons.

The problem is that you now need to commit those two changes, without committing the top 100 albums. Apparently, this sort of list isn’t praised in the company handbook.

So how can we get a Git partial commit ? We need to tell Git to stage and then commit only some parts of the file. Each changed part is called a hunk, and may contain 1 or more lines of alterations.

Let's see how we can solve this problem. In this example, the file is called ‘package.json’.

The Solution

This tells git to start adding the file, but interactively:
git add --patch package.json
Information: The --patch command is the same as -p, and a swifter way of getting to the interactive patch mode, which you can otherwise achieve by typing git add –interactive and then selecting patch.
Git will then show the first hunk, and ask you a question with some or all of these options offered to you:
Stage this hunk [y,n,q,a,d,g,/,j,J,k,K,s,e,?]?
Select the most appropriate one, and Git will stage part of the file according to your instructions. These options mean:
y = Stage this hunk to commit next.
n = Don’t stage this hunk to commit next.
q = Quit. Don’t stage this or any other remaining hunks.
a = Stage this hunk and all later hunks to commit next.
d = Don’t stage this hunk or any other remaining to commit next.
g = Select a hunk to go to.
/ = Search for a hunk matching the given regex.
j = Leave this hunk undecided, and see the next undecided hunk.
J = Leave this hunk undecided, and see the next hunk.
k = Leave this hunk undecided, and see the previous undecided hunk.
K = Leave this hunk undecided, and see the previous hunk.
s = Split the current hunk into smaller hunks.
e = Manually edit the current hunk.
? = Print the hunk help.
This checks the file to ensure you staged the correct changes:
git diff –staged
This interactively replays the hunk staging you performed:
git reset –-patch

Pushing to the Horizon

Do you want to use Git far more easily, with no commands or parameters to remember, and with a clear overview of your repository, plus the ability to preview changes? Then make your life easier, get a free download of Vershd, the effortless Git GUI.
Designed from the ground up to prevent errors, accidental deletions, and having to write all of this morning's code. Again.

Features include:
  • Rewind is simpler and easier than using Revert or Reset. It winds the clock back on commits, letting you choose whether to keep pending files or not.
  • Advanced Context Menus. Right clicking on a commit lets you merge, cherry pick, or create a branch there and then. Files have many ways to compare them to either historic or working files on your desktop.
  • See everyone's work in the clearest way possible. Branches shows branches and their commits, where you can easily pull, push, cherry pick and more. Pending shows what you can commit or stash. Files shows commits' details. Stashes helps you apply, view and delete your stashes.
Oscar the owl and Vershd logo
 © 2020 Blightysoft
Made in England
St. George's flag of EnglandThe Union Jack, the flag of the United KingdomThe European Union flagThe United Nations flag
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram