Here is how you can commit only part of a file with git.
If you are using git, you are probably aware that the command
git add takes a file name argument and then stages that file for committing.
If you use the command option
--patch, then you can add only some of your changes for staging.
When you use the option
-p), git will go through all of your changes for that file and will try to split them into portions, called “hunks”.
Then, git starts an interactive session where it goes through each “hunk” and asks you if you want to commit said hunk or not.
It prompts you with this message:
Stage this hunk [y,n,a,d,/,j,J,g,e,?]?
This is asking if the hunk that is being displayed is one of the hunks that you want to “
You can reply with
y for “yes” or with
n for “no”, but you have plenty of different options.
In the screenshot below, you can see an example of the interactive prompt and me replying with “no”, because I didn't want to add that specific hunk at that point in time.
The options I use the most are:
yto add the current hunk;
nto not add the current hunk; and
sto split the current hunk even further.
But there are more options.
If you type
? in front of the interactive staging prompt, you get a short description of each option:
y - stage this hunk n - do not stage this hunk 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 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 ? - print help
If git presents you with a hunk that is too big (i.e., that contains some changes that you want to add and other changes that you don't want to add), you can use the option
s to split the current hunk into smaller hunks.
Like I said above, after the
n options, the option
s to split a hunk is the option that I use the most!
It's just too handy!
You can check the git documentation centre to read more about interactive staging.
That's it for now! Stay tuned and I'll see you around!
I hope you learned something new! If you did, consider following the footsteps of the readers who bought me a slice of pizza 🍕. Your small contribution helps me produce this content for free and without spamming you with annoying ads.