Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Git add/stage only part of a file's changes
Say I've made a bunch of changes to a file and would like to split those changes into two or more commits. Normal git add
however stages the whole file in one go.
So how to add only some of the changes in a file?
1 answer
Git's interactive mode has a patch action. This is the shortcut for it:
git add --patch <file>
It will split the file into hunks and interactively ask which one's to add. It has a plethora of options but selecting ?
explains them nicely:
<the hunk diff is shown here>
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? ?
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
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
g - select a hunk to go to
/ - search for a hunk matching the given regex
e - manually edit the current hunk
? - print help
The free online git book has a good explanation of this too.
0 comment threads